【问题标题】:If-then condition in JSON Schema isn't workingJSON Schema 中的 If-then 条件不起作用
【发布时间】:2023-02-06 15:43:50
【问题描述】:

以下是 jsonSchema。

{
  "$schema": "http://json-schema.org/draft/2020-12/schema#",
  "type": "array",
  "items": [
    {
      "type": "object",
      "properties": {
        "isMerchant": {
          "type": "boolean"
        },
        "isAgent": {
          "type": "boolean"
        },
        "cashLoan": {
          "type": "boolean"
        },
        "personalDetail": {
          "type": "string"
        },
        "contact": {
          "type": "object",
          "properties": {
            "email": {
              "type": "string"
            },
            "mobile": {
              "type": "integer"
            },
            "area": {
              "type": "string"
            },
            "state": {
              "type": "string"
            }
          },
          "required": ["state"]}
      },
      "required": ["isMerchant","cashLoan","contact"],
      "allOf": [
        {
          "if": {
            "properties": {
              "isMerchant": {"enum": [true]}
            }
          },
          "then": {
            "required": [ "isAgent","email","mobile"]
          }
        },
        {
          "if": {
            "properties": {
              "cashLoan": {"enum": [true]}
            }
          },
          "then": {
            "required": ["personalDetail"]
          }
        }
      ]
    }
  ]
}

预期是使用此 JSON 模式验证相应的 JSON 数据。其中条件如下 -

  1. 如果“isMerchant”= true,则参数“isAgent”、“email”、“mobile”应该出现在 json 文件中。
  2. 如果“cashLoan”= true,则参数“personalDetail”应该存在。

【问题讨论】:

    标签: json jsonschema json-schema-validator


    【解决方案1】:

    对于 2020-12 草案,items 关键字的类型只能是 objectboolean

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-08-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多