我想在 json-schema 中完成什么:当属性enabled
is true
,还需要某些其他属性。什么时候false
,这些属性应该被禁止。
这是我的 json 架构:
{
"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"]
}
}
验证使用ajv
版本 6.5,这导致了要求count
等,无论其值如何enabled
。例如,对于数据:
{ "enabled": false }
我的验证错误是:
[ { 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' } ]
我怎样才能使用 json-schema 来完成这个任务draft-7
?
请注意,此问题与以下问题类似,但要求更严格:
有条件地需要 jsonSchema 属性.