What I'm trying to accomplish in json-schema: when the property enabled
is true
, certain other properties should be required. When false
, those properties should be disallowed.
Here's my json-schema:
{
"type": "object",
"properties": {
"enabled": { "type": "boolean" }
},
"required" : ["enabled"],
"additionalProperties" : false,
"if": {
"properties": {
"enabled": true
}
},
"then": {
"properties": {
"description" : { "type" : "string" },
"count": { "type": "number" }
},
"required" : ["description", "count"]
}
}
Validating using ajv
version 6.5, this had the result of requiring count
, etc. regardless of the value of enabled
. For instance, for data:
{ "enabled": false }
My validation errors are:
[ { keyword: 'required',
dataPath: '',
schemaPath: '#/then/required',
params: { missingProperty: 'description' },
message: 'should have required property \'description\'' },
{ keyword: 'required',
dataPath: '',
schemaPath: '#/then/required',
params: { missingProperty: 'count' },
message: 'should have required property \'count\'' },
{ keyword: 'if',
dataPath: '',
schemaPath: '#/if',
params: { failingKeyword: 'then' },
message: 'should match "then" schema' } ]
How can I accomplish this using json-schema draft-7
?
Note that this question is similar to, but has more stringent requirements than:
jsonSchema attribute conditionally required.