【问题标题】:Why is the conditional subschema (JSON Schema Draft7) validation not working?为什么条件子模式(JSON Schema Draft7)验证不起作用?
【发布时间】:2021-11-01 18:17:46
【问题描述】:

参考:https://json-schema.org/understanding-json-schema/reference/conditionals.html

- 架构 sn-p

"properties": {
    "country": {
      "default": "United States of America",
      "enum": [
        "United States of America",
        "Canada"
      ]
    },
    "address": {
      "properties": {
        "AddressLine1": {
          "type": "string"
        },
        "if": {
          "properties": {
            "country": {
              "const": "United States of America"
            }
          }
        },
        "then": {
          "postal_code": {
            "type": "integer"
          }
        },
        "else": {
          "postal_code": {
            "type": "string"
          }
        }
      },
      "required": [
        "AddressLine1",
        "postal_code"
      ]

示例数据 1 - 预期通过

{ “地址”: { "AddressLine1": "美国地址线 1", “邮政编码”:11779 } }

示例数据 2 - 预期失败

{ “地址”: { "AddressLine1": "加拿大地址线 1", “邮政编码”:“RA90 188” } }

我预计第二个样本会失败,但它通过了,没有任何问题。我正在使用 PowerShell Test-JSON 进行验证

【问题讨论】:

    标签: powershell jsonschema json-schema-validator


    【解决方案1】:

    您忘记在 "properties": { ... } 和 then 和 else 子句中包装 postal_code。

    【讨论】:

    • **尝试如下,但没有任何乐趣:( ** "if": { "properties": { "#country": { "const": "United States of America" } } } ,“那么”:{“属性”:{“邮政编码”:{“类型”:“整数”}}},“其他”:{“属性”:{“邮政编码”:{“类型”:“字符串”} }
    • 你能制作一个最小的可复制示例并分享吗?可以使用jsonschema.dev。这样,您的架构中的任何歧义或不准确(例如您的评论中的“县”中的哈希,以及您的示例数据中缺少“国家”)都不会被视为问题。
    • 这里是保存的模式和工作验证 - jsonschemavalidator.net/s/hjyp2FLf 我需要的是代码块 "conditionalstuff": {"$ref": "#/definitions/domainenums" } 支持条件切换在严格的枚举或宽松的字符串类型之间
    猜你喜欢
    • 2019-10-18
    • 1970-01-01
    • 1970-01-01
    • 2018-09-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-05
    相关资源
    最近更新 更多