本文介绍了Schema Draft v7 if语句未详细告知错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用JsonSchema中的if语句来验证必需的属性,但是它没有详细通知我该属性的错误.

I'm trying to validate a required property with if statement in JsonSchema, but it isn't informing me the property's error in detail.

验证已正确完成,但错误未指定属性以及验证失败.

The validation has been made correctly, but the error don't specify the property and which validation failed.

它适用于对象根级别的必需属性,但是当我在对象内部指定必需属性时,它只是警告json不匹配,并指定了thenelse架构的路径.

It works for required properties in the root level of an object, but when I specify a required property inside an object, it just warn that the json doesn't match and specify the then or else schema's path.

架构示例:

{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "type": "object",
  "required": [
    "name",
    "partners"
  ],
  "properties": {
    "partners": {
      "type": "array",
      "items": {
        "type": "object",
        "if": {
          "properties": {
            "juridical": {
              "type": "object"
            },
            "natural": {
              "type": "null"
            }
          }
        },
        "then": {
          "properties": {
            "juridical": {
              "required": [ "tradeName" ],
              "properties": {
                "tradeName": {
                  "type": "string"
                }
              }
            }
          }
        },
        "else": {
          "properties": {
            "natural": {
              "required": ["name"],
              "properties": {
                "name": {
                  "type": "string"
                }
              }
            }
          }
        }
      }
    }
  }
}

Json示例:

{
  "name": "Joao",
  "partners": [
    {
      "juridical": null,
      "natural": {

      }
    },
    {
      "juridical": {
        "tradeName": ""
      },
      "natural": null
    }
  ]
}

它应该警告第一位搭档必须输入姓名"(Required properties are missing from object: name.),而只能告诉我:JSON does not match schema from 'else'..

It should warns that the first partner have the "name" required (Required properties are missing from object: name.), instead it only tell me: JSON does not match schema from 'else'..

使用这样的简单模式,它可以按预期工作:

With a simple schema like this it works as expected:

架构示例:

{
    "if": { "properties": { "power": { "minimum": 9000 } } },
    "then": { "required": [ "disbelief" ] },
    "else": { "required": [ "confidence" ] }
}

Json示例:

{ "power": 10000 }

我正在使用 JsonSchemaValidator.net 来验证结果.

I'm using JsonSchemaValidator.net to verify the results.

推荐答案

基本上,JSON文档是否根据JSON模式进行验证.这种逻辑贯穿所有子模式和条件.

Basically, JSON document either validates against JSON schema or not. This logic goes down through all sub-schemas and conditionals.

错误消息的内容取决于JSON Schema验证程序的特定实现.您使用的是来自特定提供商的.正如Relequestal所指出的那样,除非提供者文档中有描述,否则您不能指望特定实现中的特定类型的错误消息处理.

The content of error messages depends on specific implementation of JSON Schema validator. The one you use comes from a specific provider. As pointed by Relequestal, you cannot expect specific type of error message handling from specific implementation, unless it's what the provider documentation describes.

如何向验证者的作者提出建议,以扩展if-then-else案例的信息,并为您的案例提供信息?

How about filing a suggestion to authors of the validator you use about extending messages for if-then-else case, feeding in your case?

替代方法:据我了解,您的目标是使用此特定验证器获取尽可能多的特定错误信息.就是这样,因此替代方案可能适合目标.由于JSON Schema本身是JSON文档,因此您可以考虑以下解决方法:以某种一致的方式在Schema中命名节点,并使用逻辑运算符("anyOf"(逻辑OR),"allOf"(逻辑AND),"oneOf" (逻辑XOR)),而不是 if-then-else .

Alternative approach: As I understand, your goal is to get as much specific error information as possible with this specific validator. It is what it is, so an alternative schema might fit the goal. As JSON Schema itself is a JSON document, thus you may consider a workaround by naming nodes in Schema in some consistent manner and using one of logical operators ("anyOf" (logical OR), "allOf" (logical AND), "oneOf" (logical XOR) ) instead of if-then-else.

请注意:基于架构的验证器,如果"allOf","anyOf","oneOf"应在所有架构中运行,直到满足逻辑条件.

Please note: schema based validator, in case of "allOf", "anyOf", "oneOf" is expected to run through all schemas until the logical condition is satisfied.

  • "allOf"-将始终检查JSON文档是否针对所有架构进行验证(与)

  • "allOf" - will check always if JSON doc validates against all schemas(AND)

"anyOf"-将检查JSON文档是否至少针对1个架构进行了验证(或,因此某些验证器实现可能会在之后停止检查第一个阳性结果,因为足以评估对照"anyOf"改为 true ),

"anyOf" - will check if JSON doc validates at least against 1 schema(OR, so some validator implementations might stop checking afterfirst positive result as it's sufficient to evaluate check against"anyOf" to true),

"oneOf"-将始终检查JSON文档是否针对以下内容进行了完全验证:通过检查所有架构(XOR)来获得其中一种架构

"oneOf" - will check always if JSON doc validates exactly againstone of enlisted schemas by checking against all of them (XOR)

(请参阅: https: //json-schema.org/draft-07/json-schema-validation.html#rfc.section.6.7.1 )

因此,如果在上述情况下经过验证的实例与模式不匹配,则验证器实现可能会在错误消息方面产生一些误报",因为它会列出与所有模式相比遇到的问题.它根本无法读懂我们的想法,无法猜测提供特定的JSON文档的含义,因此它全由我们来判断和决定.

Thus if validated instance doesn't match schemas in above cases, the validator implementation may produce some 'false positives' in terms of error messages, as it will enlist issues encountered vs all schemas. It simply can't read our minds and guess what we meant by providing specific JSON doc, so it throws all on us to judge and decide.

许多解决方案之一可能是定义司法"和自然"的变体,然后按照您的期望,将它们逻辑地组合成模式:

One of many solutions could be to define variants of "juridical" and "natural" and combine them logically into schemas as in your case it seems you expect:

替代模式(请注意包含一些测试JSON文档的示例"部分):

Alternative schema (please note the "examples" section containing some test JSON documents):

{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "type": "object",
  "required": [
    "name",
    "partners"
  ],
  "properties": {
    "partners": {
      "type": "array",
      "items": {
        "type": "object",
        "anyOf" : [
          {"$ref" : "#/definitions/juridical-is-object-and-natural-is-null"},
          {"$ref" : "#/definitions/juridical-is-not-an-object-and-natural-is-an-object"}
        ],
        "required" : ["natural","juridical"]
      }
    }
  },
  "examples" : [
    {
      "name": "Joao",
      "partners": [
        {
          "juridical": null,
          "natural": {

          }
        },
        {
          "juridical": {
            "tradeName": ""
          },
          "natural": null
        }
      ]
    },
    {
      "name": "Joao",
      "partners": [
        {
          "juridical": null,
          "natural": {
            "name" : ""
          }
        },
        {
          "juridical": {
            "tradeName": ""
          },
          "natural": null
        }
      ]
    },
    {
      "name": "Joao",
      "partners": [
        {
          "juridical": null,
          "natural": {

          }
        },
        {
          "juridical": {
            "tradeName": ""
          },
          "natural": null
        },
        {
          "juridical" : [],
          "natural" : {}
        }
      ]
    }
  ],
  "definitions" : {
    "natural" : {
        "is-object" : {
          "type" : "object",
          "required": ["name"],
          "properties": {
            "name": {
              "type": "string"
            }
          }
        },
        "is-not-an-object" : {
          "not" : { "type" : "object" }
        },
    },
    "juridical" : {
      "is-object" : {
        "type" : "object",
        "required": ["tradeName"],
        "properties": {
          "name": {
            "type": "string"
          }
        }
      },
      "is-not-an-object" : {
        "not" : { "type" : "object" }
      },
    },
    "juridical-is-object-and-natural-is-null" : {
      "properties" : {
        "natural" : {"$ref" : "#/definitions/natural/is-not-an-object"},
        "juridical" : {"$ref" : "#/definitions/juridical/is-object"}
      },
    },
    "juridical-is-not-an-object-and-natural-is-an-object" : {
      "properties" : {
        "natural" : {"$ref" : "#/definitions/natural/is-object"},
        "juridical" : {"$ref" : "#/definitions/juridical/is-not-an-object"}
      }
    },
  }
}

注意:

"not":{ schema }错误消息可能使临时用户感到困惑,但是它符合规范: https://json-schema.org/draft-07/json-schema-validation.html#rfc.section .6.7.4

"not" : { schema } error message might be confusing for casual users, but it conforms to the spec: https://json-schema.org/draft-07/json-schema-validation.html#rfc.section.6.7.4

如注释中所述,您正在查看错误详细信息.鉴于所选工具在更复杂架构的if-then-else错误详细信息方面受到限制,您是否尝试使用其他关键字重塑架构以触发尽可能少的开销消息?

As explained in comments, you are after error details. Given the constraints of the selected tool in terms of if-then-else error details for more complex schemas, did you try to reshape schema using different keywords to trigger as less overhead messages as possible?

替代方案2

{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "type": "object",
  "required": [
    "name",
    "partners"
  ],
  "properties": {
    "partners": {
      "type": "array",
      "items" : {
        "properties" : {
          "natural" : {
            "if" : { "type" : "object" },
            "then" : { "required" : ["name"] },
            "dependencies" : {
              "name" : {
                 "properties" : {
                  "name" : {"type" : "string"}
                 }
               }
            }
          },
          "juridical" : {
            "if" : { "type" : "object" },
            "then" : { "required" : ["tradeName"] },
            "dependencies" : {
              "tradeName" : {
                 "propertyNames" : {
                   "enum" : ["tradeName"]
                 },
                 "properties" : {
                  "tradeName" : {"type" : "string"}
                 }
               }
            }
          }
        },
        "anyOf" : [
          {"$ref" : "#/definitions/natural-is-null-juridical-is-an-object"},
          {"$ref" : "#/definitions/natural-is-an-object-juridical-is-null"}
        ]
      }
    }
  },
  "definitions" : {
    "natural-is-null-juridical-is-an-object" : {
        "properties" : {
            "natural" : { "type": "null"},
            "juridical" : { "type" : "object"}
        }
    },
    "natural-is-an-object-juridical-is-null" : {
        "properties" : {
            "natural" : { "type": "object"},
            "juridical" : { "type" : "null"}
        }
    },
  },
  "examples" : [
    {
      "name": "Joao",
      "partners": [
        {
          "juridical": null,
          "natural": {

          }
        },
        {
          "juridical": {
            "tradeName": "",
          },
          "natural": null
        },
        {
          "juridical" : {},
          "natural" : {}
        },
        {
          "juridical" : null,
          "natural" : null
        }
      ]
    },
  ]
} 

这篇关于Schema Draft v7 if语句未详细告知错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 04:22