I am using Draft-04 of JSON Schema. Is it possible to set dependencies based on the existence of a sub-property, and/or depend on a sub-property? Or am I forced to use allOf
to manage these kinds of dependencies?
I have the following (you can play with it at https://repl.it/@neverendingqs/JsonSchemaNestedDependencies):
'use strict';
const Ajv = require('ajv');
const assert = require('chai').assert;
// Using ajv@5.5.1
const draft4 = require('ajv/lib/refs/json-schema-draft-04.json');
const schema = {
"$schema": "http://json-schema.org/draft-04/schema#",
"type": "object",
"properties": {
"foo1": {
"type": [ "object" ],
"properties": {
"bar1": { "type": "string" }
}
},
"foo2": {
"type": [ "object" ],
"properties": {
"bar2": { "type": "string" }
}
}
},
"dependencies": {
"foo1": ["foo2"],
// Is this possible?
"foo1/bar1": ["foo2/bar2"]
}
};
const schemaName = 'my-schema';
const ajv = new Ajv();
ajv.addMetaSchema(draft4);
ajv.addSchema(schema, schemaName);
assert.isTrue(
ajv.validate(schemaName, {
"foo1": { "bar1": "a" },
"foo2": { "bar2": "c" }
}),
ajv.errorsText(ajv.errors, { dataVar: 'event' })
);
assert.isFalse(ajv.validate(schemaName, {
"foo1": { "bar1": "a" }
}));
// Looking to cause this to pass
assert.isFalse(ajv.validate(schemaName, {
"foo1": { "bar1": "a" },
"foo2": {}
}));
I am looking for Draft-04 answers, but am also interested in answers using later specifications.
EDIT: Draft-04 refers to the specifications under http://json-schema.org/specification-links.html#draft-4. Specifically, I am using dependencies
which is defined under the Validation specification (https://datatracker.ietf.org/doc/html/draft-fge-json-schema-validation-00)