我想根据其他一些属性的值有条件地添加要求。
只有在“isInexperienced”时才需要“companyName”和“companyAddress”
值为假。

架构

{
  "type": "object",
  "properties": {
    "previous_employment_section": {
      "type": "array",
      "items": {
        "type": "object",
        "properties": {
          "companyAddress": {
            "type": "string"
          },
          "companyName": {
            "type": "string"
          }
        },
        "if": {
          "#/properties/isInexperienced": {
            "const": false
          }
        },
        "then": {
          "required": [
            "companyName",
            "companyAddress"
          ]
        }
      }
    },
    "isInexperienced": {
      "type": "boolean"
    }
  }
}

数据
{
  "previous_employment_section": [],
  "isInexperienced": true
}

最佳答案

我不完全理解你原始模式的意图,但是这个怎么样?

{
    "$schema": "http://json-schema.org/draft-07/schema#",
    "type": "object",
    "properties": {
        "previous_employment_section": {
            "type": "array",
            "items": {
                "type": "object",
                "properties": {
                    "companyAddress": {
                        "type": "string"
                    },
                    "companyName": {
                        "type": "string"
                    }
                }
            }
        },
        "isInexperienced": {
            "type": "boolean"
        }
    },
    "if": {
        "properties": {
            "isInexperienced": {
                "const": false
            }
        }
    },
    "then": {
        "properties": {
            "previous_employment_section": {
                "items": {
                    "required": [
                        "companyName",
                        "companyAddress"
                    ]
                },
                "minItems": 1
            }
        }
    }
}

关于javascript - JSON Schema 是否可以使用引用外部属性的 if/then/else,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/56325138/

10-10 00:51