JSON模式,如果/然后/其他条件行为异常

问题描述 投票:0回答:1

我在JSON模式中看到了一些奇怪的行为,似乎无法弄清楚出了什么问题。用简单的英语来说,我想要的行为是“如果useColumnForKey为真,key也必须为真。

模式:

{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "type": "object",
  "properties": {
    "name": {
      "type": "string"
    },
    "description": {
      "type": "string"
    },
    "fields": {
      "type": "array",
      "items": {
        "$ref": "#/definitions/field"
      },
      "contains": {
        "type": "object",
        "properties": {
          "key": {
            "const": true
          }
        },
        "required": [
          "key"
        ]
      },
      "minItems": 1
    }
  },
  "additionalProperties": false,
  "required": [
    "name",
    "fields"
  ],
  "definitions": {
    "field": {
      "type": "object",
      "properties": {
        "name": {
          "description": "The field name",
          "type": "string"
        },
        "description": {
          "description": "The field description",
          "type": "string"
        },
        "key": {
          "description": "Whether or not this field is the primary key or part of the composite primary key for the model",
          "type": "boolean"
        },
        "useColumnNameForKey": {
          "description": "Whether or not to use the exact column name as part of the key. If false, the global version of the column will be used. If this property is preset the key property must be true.",
          "type": "boolean"
        }
      },
      "if": {
        "properties": {
          "useColumnNameForKey": {
            "const": true
          }
        }
      },
      "then": {
        "properties": {
          "key": {
            "const": true
          }
        },
        "required": [
          "key"
        ]
      },
      "additionalProperties": false,
      "required": [
        "name",
        "description"
      ]
    }
  }
}

与此相对应的YAML文件(带有注释指示什么是什么:]:

name: SomeThing
fields:

# No errors here, as you'd expect
  - name: field1
    description: description1
    key: true

# Error: missing property "key". What the heck, why?
  - name: field2
    description: description2.

# Error: missing property "key". This is the intended functionality.
  - name: field3
    description: description3.
    useColumnNameForKey: true

# Error: Property "key" must have value "true". This is the intended functionality.
  - name: field4
    description: description4.
    key: false
    useColumnNameForKey: true

# No errors here, also as expected.
  - name: field5
    description: description5.
    # key: true
    useColumnNameForKey: false

我认为正在发生的是"if"条件在不应该被评估为true时……我只是不知道为什么!感谢您的帮助。

json yaml jsonschema json-schema-validator
1个回答
0
投票

发布后马上想出来。我猜是橡皮鸭。

结果,您还需要为要作为条件的任何属性定义"required":[...]。摘录:

        {
          "if": {
            "properties": {
              "useColumnNameForKey": {
                "const": true
              }
            },
            "required": [
              "useColumnNameForKey" // Here's what I was missing!
            ]
          },
          "then": {
            "properties": {
              "key": {
                "const": true
              }
            },
            "required": [
              "key"
            ]
          }
        }
© www.soinside.com 2019 - 2024. All rights reserved.