var schema = {
"id": "http://some.site.somewhere/entry-schema#",
"$schema": "http://json-schema.org/draft-04/schema#",
"description": "schema for an fstab entry",
"type": "object",
"required": [ "record" ],
"properties": {
"record": {
"type": "object",
"oneOf": [
{ "$ref": "#/definitions/student" },
{ "$ref": "#/definitions/book" }
]
}
},
"definitions": {
"student": {
"properties": {
"type": {
"enum": [ "student" ],
"bounding": true
},
"age": {
"type": "integer"
}
},
"required": [ "age" ],
"additionalProperties": false
},
"book": {
"properties": {
"type": {
"enum": [ "book" ],
"bounding": true
},
"pages": {
"type": "integer"
}
},
"required": [ "pages" ],
"additionalProperties": false
}
}
};
var data = {
"record": {
"type": "student"
}
};
var ajv = require('ajv')({allErrors: true, v5: true});
console.log(ajv.validate(schema, data));
console.log(ajv.errorsText());
var schema2 = {
"type": "object",
"required": [ "record" ],
"properties": {
"record": {
"type": "object",
"switch": [
{
"if": { "properties": { "type": { "constant": "student" } } },
"then": { "$ref": "#/definitions/student" }
},
{
"if": { "properties": { "type": { "constant": "student" } } },
"then": { "$ref": "#/definitions/book" }
},
{ "then": false }
]
}
},
"definitions": {
"student": {
"properties": {
"type": {},
"age": { "type": "integer" }
},
"required": [ "age" ],
"additionalProperties": false
},
"book": {
"properties": {
"type": {},
"pages": { "type": "integer" }
},
"required": [ "pages" ],
"additionalProperties": false
}
}
};
console.log(ajv.validate(schema2, data));
console.log(ajv.errorsText());