我要在 json-schema 中完成的工作:当属性enabled
为时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 attribute conditionally required更严格。