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.
To achieve that, in the
if
statement you need to use theconst
keyword, so the schema will look like:This was inspired by vearutop's excellent answer. I think it might be a little shorter, and accomplishes my stated purpose.
As pointed out in the comments, this is a specific variant of the Enum strategy spelled out in this answer.
Try this schema:
If you need
"additionalProperties": false
you have to enumerate all properties in boththen
andelse
. If you can accept additional properties the schema could be simplier:I checked with
ajv
cli.Valid:
{"enabled": false}
Invalid:
{"enabled": true}
Valid:
{"enabled": true, "description":"hi", "count":1}