Schema Draft v7 if statement is not informing error in detail
我正在尝试使用JsonSchema中的if语句来验证必需的属性,但是它没有详细通知我该属性的错误。
验证已正确完成,但错误未指定属性以及验证失败。
它适用于对象根级别的必需属性,但是当我在对象内部指定必需属性时,它只是警告json不匹配并指定
模式示例:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 | { "$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示例:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | { "name":"Joao", "partners": [ { "juridical": null, "natural": { } }, { "juridical": { "tradeName":"" }, "natural": null } ] } |
它应该警告第一位搭档必须输入"姓名"(
使用这样的简单模式,可以按预期工作:
模式示例:
1 2 3 4 5 | { "if": {"properties": {"power": {"minimum": 9000 } } }, "then": {"required": ["disbelief" ] }, "else": {"required": ["confidence" ] } } |
Json示例:
1 | {"power": 10000 } |
我正在使用JsonSchemaValidator.net来验证结果。
基本上,JSON文档是否根据JSON模式进行验证。此逻辑贯穿所有子模式和条件。
错误消息的内容取决于JSON Schema验证程序的特定实现。您使用的是来自特定提供商的。正如Relequestal指出的那样,除非提供程序文档描述了这种情况,否则您不能指望特定实现中的特定类型的错误消息处理。
如何向验证者的作者提出建议,以扩展if-then-else案件的消息,并为您的案件提供帮助?
替代方法:据我了解,您的目标是使用此特定的验证器获得尽可能多的特定错误信息。就是这样,因此替代方案可能适合目标。由于JSON Schema本身是JSON文档,因此您可以考虑以下解决方法:以某种一致的方式在Schema中命名节点,并使用逻辑运算符(" anyOf "(逻辑OR)," allOf "(逻辑AND)之一," oneOf "(逻辑XOR)),而不是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文档的含义,因此,所有这些都由我们来判断和决定。
许多解决方案之一可能是定义" juridical "和" natural "的变体,并按照您的期望将它们逻辑地组合到架构中,如您所愿:
either juridical is an object (+relevant constraints) AND natural is not an object or juridical is not an object and natural is an object
(+relevant constraints).
替代模式(请注意" examples "部分包含一些测试JSON文档):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 | { "$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
更新
如注释中所述,您正在查看错误详细信息。给定所选工具在更复杂模式的if-then-else错误详细信息方面的约束,您是否尝试使用其他关键字重塑模式以触发尽可能少的开销消息?
替代方案2
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 | { "$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 } ] }, ] } |