基本上,JSON 文档要么根据 JSON 模式进行验证,要么不进行验证。此逻辑贯穿所有子模式和条件。
错误信息的内容取决于 JSON Schema 验证器的具体实现。您使用的那个来自特定的提供商。正如 Relequestal 所指出的,您不能期望从特定实现中处理特定类型的错误消息,除非它是提供者文档所描述的。
如何向您使用的验证器的作者提出关于扩展 if-then-else 案例的消息的建议,以提供您的案例?
替代方法:据我了解,您的目标是使用这个特定的验证器获得尽可能多的特定错误信息。它就是这样,因此替代模式可能适合目标。由于 JSON Schema 本身是一个 JSON 文档,因此您可以考虑一种解决方法,以某种一致的方式命名 Schema 中的节点并使用逻辑运算符之一(“anyOf”(逻辑 OR)、“allOf”(逻辑 AND)、“oneOf” (逻辑异或))而不是 if-then-else。
请注意:基于模式的验证器,在“allOf”、“anyOf”、“oneOf”的情况下,预计会遍历所有模式,直到满足逻辑条件。
"allOf" - 将始终检查 JSON 文档是否针对所有模式进行验证
(与)
"anyOf" - 将检查 JSON 文档是否至少针对 1 个模式进行验证
(或者,因此某些验证器实现可能会在之后停止检查
第一个积极的结果,因为它足以评估检查
"anyOf" 到 true),
"oneOf" - 将始终检查 JSON 文档是否完全针对
通过检查所有模式(XOR)
(见:https://json-schema.org/draft-07/json-schema-validation.html#rfc.section.6.7.1)
因此,如果在上述情况下验证的实例与模式不匹配,验证器实现可能会在错误消息方面产生一些“误报”,因为它会列出遇到的问题与所有模式。它根本无法读懂我们的想法并猜测我们提供特定 JSON 文档的意思,所以它把一切都交给我们来判断和决定。
许多解决方案之一可能是定义“司法”和“自然”的变体,并将它们在逻辑上组合成模式,正如您所期望的那样:
要么法律是一个对象(+相关约束)并且自然不是一个对象或者法律不是一个对象并且自然是一个对象
(+相关约束)。
替代架构(请注意包含一些测试 JSON 文档的“示例”部分):
{
"$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
更新
如 cmets 中所述,您需要了解错误详细信息。鉴于所选工具对更复杂模式的 if-then-else 错误详细信息的限制,您是否尝试使用不同的关键字重塑模式以触发尽可能少的开销消息?
替代架构 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
}
]
},
]
}