在AJV中,我正在努力实现这一目标:
properties: {
structure_type: {
type: "string",
enum: ['A','B','C']
},
}
if: {
properties: {
structure_type: {
const: 'A'
}
}
},
then: {
properties: {
floors: {'$ref': "#/definitions/floors"}
},
required: ['floors']
},
required: ["structure_type"],
additionalProperties: false,
仅当floors
属性为structure_type
时,我才需要属性A
。 但是当floors
属性是structure_type
以外的任何内容时,我也不想允许属性A
由于某种原因,我收到此错误:
{
"keyword": "additionalProperties",
"dataPath": "",
"schemaPath": "#/additionalProperties",
"params": {
"additionalProperty": "floors"
},
"message": "should NOT have additional properties"
}
输入:
{
structure_type: 'A',
floors: [*CONTENT_NOT_IMPORTANT*]
}
似乎AJV认为floors
属性未定义,但实际上是! (在then
中)
编辑:
我当时在考虑not: {required: ["floors"]}
,但是即使这样做有效,也会破坏我的错误处理。 (我收到的是additionalProperties
错误,而不是not
错误