【问题标题】:Validation against JSON Schema sends an additional validation message针对 JSON Schema 的验证会发送额外的验证消息
【发布时间】:2022-08-16 13:45:52
【问题描述】:

我需要验证 json

[
{
    \"relatedParty\": \"tr13\",
    \"action\": \"b\"
},
{
    \"relatedParty\": \"er127\"
}

]

我想验证狭窄和属性值。所以我编写了以下 json 模式:

{
\"type\": \"array\",
\"maxItems\": 4,
\"items\": {
    \"type\": \"object\",
    \"oneOf\": [
        {
            \"properties\": {
                \"relatedParty\": {
                    \"type\": \"string\",
                    \"pattern\": \"tr\\\\d{2}\"
                },
                \"action\": {
                    \"type\": \"string\",
                    \"pattern\": \"a\"
                }
            }
        },
        {
            \"properties\": {
                \"relatedParty\": {
                    \"type\": \"string\",
                    \"pattern\": \"er\\\\d{3}\"
                }
            }
        }
    ]
}

}

但如果我有错误的价值行动我收到两者的验证消息相关方行动特性。

谁能解释为什么我收到 2 条验证消息而不是 1 条,以及如何调整我的验证模式以仅获得 1 条消息?

更新:

尝试使用以下 json-schema 验证相同的 json-object:

{
\"type\": \"array\",
\"maxItems\": 4,
\"items\": {
    \"type\": \"object\",
    \"properties\": {
        \"relatedParty\": {
            \"type\": \"string\",
            \"oneOf\": [
                {
                    \"pattern\": \"tr\\\\d{2}\"
                },
                {
                    \"pattern\": \"er\\\\d{3}\"
                }
            ]
        }
    },
    \"$defs\": {
        \"tr-requires-action\": {
            \"if\": {
                \"properties\": {
                    \"relatedParty\": {
                        \"pattern\": \"tr\\\\d{2}\"
                    }
                },
                \"required\": [
                    \"relatedParty\"
                ]
            },
            \"then\": {
                \"properties\": {
                    \"action\": {
                        \"pattern\": \"a\"
                    }
                },
                \"required\": [
                    \"action\"
                ]
            }
        }
    }
}

}

然后我\'没有发现错误。 JSON 对 schema\' 消息进行验证。但是\'action\'属性的差异仍然存在。

    标签: json jsonschema


    【解决方案1】:

    oneOf 关键字意味着一个且只有一个模式必须成功验证。 JSON Schema 验证器发现两个模式都失败并告诉您两者中的错误。尽管对于查看模式的人来说很明显它正在关闭“relatedParty”属性的值,但验证器无法凭直觉判断您的意图。

    您可以使用 if/then 关键字向验证器明确说明您的意图。

    {
      "type": "object",
      "properties": {
        "relatedParty": {
          "type": "string",
          "oneOf": [
            { "pattern": "tr\\d{2}" },
            { "pattern": "er\\d{3}" }
          ]
        }
      },
    
      "allOf": [
        { "$ref": "#/$defs/tr-requires-action" }
      ],
    
      "$defs": {
        "tr-requires-action": {
          "if": {
            "properties": {
              "relatedParty": { "pattern": "tr\\d{2}" }
            },
            "required": ["relatedParty"]
          },
          "then": {
            "properties": {
              "action": { "pattern": "a" }
            },
            "required": ["action"]
          }
        }
      }
    }
    

    (我不得不对您的数据模型做出一些假设,但希望您能理解并调整示例。)

    【讨论】:

    • Jason Desrosiers,谢谢你的回答。我已尝试修改您的草稿,但现在我收到“未发现错误”。 JSON 根据架构的消息进行验证。好消息,但我的 json 仍然有 1 条不匹配,我需要 1 条关于它的消息。
    • 我很乐意帮助您解决其他问题,但如果不能看到您正在使用的架构,我将无能为力。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-24
    • 2021-10-21
    • 2015-10-24
    • 2020-12-01
    • 2021-11-04
    • 2011-01-24
    相关资源
    最近更新 更多