json - 我如何需要一个字段或另一个字段或(其他两个字段之一)但不是全部?

标签 json validation jsonschema

我无法提出一个 JSON 模式来验证 JSON 是否包含:

  • 只有一个字段
  • 仅限其他字段
  • 仅(其他两个字段之一)

但当存在多个时不匹配。

具体来说,我想要一个

  • 全部复制
  • 文件名
  • 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"]}}]}
    ]
} ;

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

{"copyAll": true, "unrelatedA":"xxx"}
{"fileNames": ["aab", "cab"], "unrelatedA":"xxx"}
{"matchesFiles": ["a*"], "unrelatedA":"xxx"}
{"doesntMatchFiles": ["a*"], "unrelatedA":"xxx"}
{"matchesFiles": ["a*"], "doesntMatchFiles": ["*b"], "unrelatedA":"xxx"}

但不匹配:

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

我猜我缺少一些明显的东西 - 我想知道它是什么。

最佳答案

问题在于“非”语义。 “不需要”并不意味着“禁止包含”。这只是意味着您不必为了验证该架构而添加它。

但是,您可以使用“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"
                    ]
                }
            ]
        }
    ]
}

关于json - 我如何需要一个字段或另一个字段或(其他两个字段之一)但不是全部?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24023536/

相关文章:

java - ClassCastException : org. json.simple.JSONArray 无法转换为 org.json.simple.JSONObject

javascript - 将内容 append 到 Div,然后尝试向下滑动

java - 如何向需要查询实体以执行验证的 JPA 实体添加验证

python - 检测 json schema 是否具有 oneOf 类型 schema

java - 从 pojo : how do I add "description" automatically? 生成 JsonSchema

javascript - Fastify JSON 架构默认值 `null`

c# - 使用 C# 反序列化 JSON

python - 为来自客户端的请求和来自服务器的响应(REST API)在 Django 中压缩 JSON 有效负载。

python - 使用 Python 在 Postgres 中搜索 json 编码的字符串

ruby-on-rails - Rails 有标准的正则表达式验证器吗?