AJV验证-通过有条件的潜在客户添加属性,导致“不应具有其他属性”

时间:2019-09-27 15:06:40

标签: javascript validation ajv

在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错误

0 个答案:

没有答案