3 /*eslint-disable max-len*/
5 var common = require('./common');
6 var YAMLException = require('./exception');
7 var Type = require('./type');
10 function compileList(schema, name, result) {
13 schema.include.forEach(function (includedSchema) {
14 result = compileList(includedSchema, name, result);
17 schema[name].forEach(function (currentType) {
18 result.forEach(function (previousType, previousIndex) {
19 if (previousType.tag === currentType.tag && previousType.kind === currentType.kind) {
20 exclude.push(previousIndex);
24 result.push(currentType);
27 return result.filter(function (type, index) {
28 return exclude.indexOf(index) === -1;
33 function compileMap(/* lists... */) {
41 function collectType(type) {
42 result[type.kind][type.tag] = result['fallback'][type.tag] = type;
45 for (index = 0, length = arguments.length; index < length; index += 1) {
46 arguments[index].forEach(collectType);
52 function Schema(definition) {
53 this.include = definition.include || [];
54 this.implicit = definition.implicit || [];
55 this.explicit = definition.explicit || [];
57 this.implicit.forEach(function (type) {
58 if (type.loadKind && type.loadKind !== 'scalar') {
59 throw new YAMLException('There is a non-scalar type in the implicit list of a schema. Implicit resolving of such types is not supported.');
63 this.compiledImplicit = compileList(this, 'implicit', []);
64 this.compiledExplicit = compileList(this, 'explicit', []);
65 this.compiledTypeMap = compileMap(this.compiledImplicit, this.compiledExplicit);
69 Schema.DEFAULT = null;
72 Schema.create = function createSchema() {
75 switch (arguments.length) {
77 schemas = Schema.DEFAULT;
82 schemas = arguments[0];
87 throw new YAMLException('Wrong number of arguments for Schema.create function');
90 schemas = common.toArray(schemas);
91 types = common.toArray(types);
93 if (!schemas.every(function (schema) { return schema instanceof Schema; })) {
94 throw new YAMLException('Specified list of super schemas (or a single Schema object) contains a non-Schema object.');
97 if (!types.every(function (type) { return type instanceof Type; })) {
98 throw new YAMLException('Specified list of YAML types (or a single Type object) contains a non-Type object.');
108 module.exports = Schema;