【问题标题】:JSON Schema Is it possible to use if/then/else which refer to outside propertyJSON Schema 是否可以使用引用外部属性的 if/then/else
【发布时间】:2019-10-12 23:23:32
【问题描述】:

我想根据其他一些属性的值有条件地添加。 只有在“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
}

【问题讨论】:

    标签: javascript json object ajv


    【解决方案1】:

    我不完全理解您原始架构的意图,但是这个怎么样?

    {
        "$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
                }
            }
        }
    }
    

    【讨论】:

      【解决方案2】:

      这是不可能的。我们需要在更高层次上拥有“if”,才能嵌套“properties”。可以使用Leadpony的方法。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-11-11
        • 2019-08-31
        • 2022-11-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-08-20
        • 1970-01-01
        相关资源
        最近更新 更多