const Ajv = require('ajv@5.2.2');
const metaSchema = require("ajv@5.2.2/lib/refs/json-schema-draft-04.json");
const ajv = new Ajv({
meta: false,
validateSchema: false,
missingRefs: "ignore",
verbose: true
});
ajv.addMetaSchema(metaSchema);
ajv._opts.defaultMeta = metaSchema.id;
const SPACING = {
always: 'always',
never: 'never'
};
const SPACING_VALUES = [SPACING.always, SPACING.never];
var schema = {
type: 'array',
items: [
{
definitions: {
basicConfig: {
type: 'object',
properties: {
when: {
enum: SPACING_VALUES
},
allowMultiline: {
type: 'boolean'
},
spacing: {
type: 'object',
properties: {
objectLiterals: {
enum: SPACING_VALUES
}
}
}
}
},
basicConfigOrBoolean: {
oneOf: [{
$ref: '#/definitions/basicConfig'
}, {
type: 'boolean'
}]
}
},
oneOf: [{
allOf: [{
$ref: '#/definitions/basicConfig'
}, {
type: 'object',
properties: {
attributes: {
$ref: '#/definitions/basicConfigOrBoolean'
},
children: {
$ref: '#/definitions/basicConfigOrBoolean'
}
}
}]
}, {
enum: SPACING_VALUES
}]
}, {
type: 'object',
properties: {
allowMultiline: {
type: 'boolean'
},
spacing: {
type: 'object',
properties: {
objectLiterals: {
enum: SPACING_VALUES
}
}
}
},
additionalProperties: false
}
],
minItems: 0,
maxItems: 2
};
const validate = ajv.compile(schema);
console.log('#1', validate([{
when: 'never',
spacing: {objectLiterals: 'whatever'},
attributes: true
}]));
console.log('#2', validate([{
when: 'never',
spacing: {objectLiterals: 'whatever'},
attributes: true,
children: {when: 'whatever'} // <-- This was added
}]));