如果在JSON Schema验证器中使用其他条件,如何使用



我想使用JSON Schema验证器进行验证,而我使用诸如遵循的代码时,我获得错误GCODE未定义

我尝试喜欢以下代码

properties: {
        oemId: {
          'type': ['integer'],
          'minLength': 1,
          'required': true
        },
        gCode:{
          'type': ['string'],
          'minLength': 1
        },
        problemCategoryId: {
          'type': ['integer'],
          'minLength': 1
        }
      },
      if :{
        properties:{
          oemId: 1
        }
      },
      then:{
        required:[gCode]
      },
      else:{
        required: [problemCategoryId]
      }

i期望oemid = 1时,需要gcode = true否则QuardyCategoryId是try

所讨论的JSON模式的if-then-else语句是不正确的。这是正确的:

{
  "type": "object",
  "properties": {
        oemId: {
          'type': ['integer'],
          'minLength': 1,
          'required': true
        },
        gCode:{
          'type': ['string'],
          'minLength': 1
        },
        problemCategoryId: {
          'type': ['integer'],
          'minLength': 1
        }
  },
  "if": {
    "properties": {
      "oemId": { "const": 1 }
    },
    "required": ["oemId"]
  },
  "then": { "required": ["gCode"] },
  "else": { "required": ["problemCategoryId"] }
}

请注意,此if-then-else语法刚刚添加到Draft-07中的JSON架构中,此处在JSON-SCHEMA.ORG中的文档:https://json-schema.org/underting-json-json-schema/reference/reference/cormentionals。html

最新更新