我如何需要一个或另一个领域或(另外两个)中的一个但不是全部?

use*_*184 44 validation json jsonschema

我在设置JSON模式时遇到问题,该模式将验证JSON是否包含:

  • 只有一个领域
  • 另一个领域
  • (仅限于其他两个领域之一)

但是当它们的倍数存在时不匹配.

在我的具体情况下,我想要一个

  • copyAll
  • fileNames
  • matchesFiles 和/或 doesntMatchFiles

验证,但我不想接受超过那个.

这是我到目前为止所得到的:

{
    "$schema": "http://json-schema.org/draft-04/schema#",
    "type": "object",
    "required": [ "unrelatedA" ],
    "properties": {
    "unrelatedA": {
        "type": "string"
    },
    "fileNames": {
        "type": "array"
    },
    "copyAll": {
        "type": "boolean"
    },
    "matchesFiles": {
        "type": "array"
    },
    "doesntMatchFiles": {
        "type": "array"
        }
    },
    "oneOf": [
         {"required": ["copyAll"], "not":{"required":["matchesFiles"]}, "not":{"required":["doesntMatchFiles"]}, "not":{"required":["fileNames"]}},
         {"required": ["fileNames"], "not":{"required":["matchesFiles"]}, "not":{"required":["doesntMatchFiles"]}, "not":{"required":["copyAll"]}},
         {"anyOf": [
               {"required": ["matchesFiles"], "not":{"required":["copyAll"]}, "not":{"required":["fileNames"]}},
               {"required": ["doesntMatchFiles"], "not":{"required":["copyAll"]}, "not":{"required":["fileNames"]}}]}
    ]
} ;
Run Code Online (Sandbox Code Playgroud)

这比我想要的更多.我希望这匹配以下所有内容:

{"copyAll": true, "unrelatedA":"xxx"}
{"fileNames": ["aab", "cab"], "unrelatedA":"xxx"}
{"matchesFiles": ["a*"], "unrelatedA":"xxx"}
{"doesntMatchFiles": ["a*"], "unrelatedA":"xxx"}
{"matchesFiles": ["a*"], "doesntMatchFiles": ["*b"], "unrelatedA":"xxx"}
Run Code Online (Sandbox Code Playgroud)

但不匹配:

{"copyAll": true, "matchesFiles":["a*"], "unrelatedA":"xxx"}
{"fileNames": ["a"], "matchesFiles":["a*"], "unrelatedA":"xxx"}
{"copyAll": true, "doesntMatchFiles": ["*b"], "matchesFiles":["a*"], "unrelatedA":"xxx"}
{"fileNames": ["a"], "matchesFiles":["a*"], "unrelatedA":"xxx"}
{"unrelatedA":"xxx"}
Run Code Online (Sandbox Code Playgroud)

我猜我有一些显而易见的东西 - 我想知道它是什么.

jru*_*ren 89

问题是"不"语义."不要求"并不意味着"包容禁止".它只是意味着您不必添加它以验证该架构.

但是,您可以使用"oneOf"以更简单的方式满足您的规范.请记住,这意味着"只需要其中一个模式可以验证".以下模式实现了您尝试解决的属性切换:

{
    "$schema": "http://json-schema.org/draft-04/schema#",
    "type": "object",
    "required": [
        "unrelatedA"
    ],
    "properties": {
        "unrelatedA": {
            "type": "string"
        },
        "fileNames": {
            "type": "array"
        },
        "copyAll": {
            "type": "boolean"
        },
        "matchesFiles": {
            "type": "array"
        },
        "doesntMatchFiles": {
            "type": "array"
        }
    },
    "oneOf": [
        {
            "required": [
                "copyAll"
            ]
        },
        {
            "required": [
                "fileNames"
            ]
        },
        {
            "anyOf": [
                {
                    "required": [
                        "matchesFiles"
                    ]
                },
                {
                    "required": [
                        "doesntMatchFiles"
                    ]
                }
            ]
        }
    ]
}
Run Code Online (Sandbox Code Playgroud)

  • @Tomeamis“不需要”意味着“可能存在或可能不存在”,而“禁止包含”意味着“不得存在”,这是不一样的 (2认同)
  • @Mark 用简单的英语来说,你是对的。在 JSON 模式中,“not”元素内的“required”元素(其数组内有一个字符串)实际上意味着“required”的参数是被禁止的。尝试根据此模式验证此 JSON:`{"forbidden":"asd"}`:`{"$schema":"http://json-schema.org/draft-07/schema#","not" :{"required":["forbidden"]}}`,你会看到验证失败。 (2认同)