JSON 模式递归似乎没有正确验证
JSON schema recursion doesnt seem to properly validate
我正在浏览文档以尝试弄清楚循环是如何工作的,这样我就可以验证 object 数组中的每个 object 都与模式匹配。
递归似乎是我想要的,但给出的示例不起作用:https://json-schema.org/understanding-json-schema/structuring.html
我正在尝试验证该示例,但它始终“有效”。我尝试更改 JSON 中的所有字段名称,但没关系:
不确定发生了什么。对于这个例子,我将如何验证每个 child 匹配的人模式(而不是在模式中静态地写出每个)。
例如,我想验证这个JSON。 toplevel
下可以有任意数量的 object,“objectsList” 下可以有任意数量的 object。我想确保“objectsList”下的每个 object 都具有正确的字段名称和类型(同样无需在架构中对整个内容进行硬编码):
{
"toplevel": {
"objectOne": {
"objectsList": [
{
"field1": 1231,
"field2": "sekfjlskjflsdf",
"field3": ["ssss","eeee"],
},
{
"field1": 11,
"field2": "sef",
"field3": ["eeee","qqqq"],
},
{
"field1": 1231,
"field2": "wwwww",
"field3": ["sisjflkssss","esdfsdeee"],
},
]
},
"objectTwo": {
"objectsList": [
{
"field1": 99999,
"field2": "yuyuyuyuyu",
"field3": ["ssssuuu","eeeeeee"],
},
{
"field1": 221,
"field2": "vesdlkfjssef",
"field3": ["ewerweeee","ddddq"],
},
]
},
}
}
怎么了?
这里的问题不是递归——你的模式看起来不错。
根本问题与此处相同:
JSON Schema is designed for extensibility. That means it allows any kind of additional properties to be added as long as they are not conflicting with the known/expected keywords.
解决方案
这里的解决方案是在您的“人”(来自屏幕截图)和顶级架构中添加 "additionalProperties": false
以防止接受那些不正确的对象。第二个示例也是如此:在 "type": "object"
的任何定义中,如果您不想定义这些无关的属性,则必须添加 "additionalProperties": false
。
或者,您可以将预期属性声明为 required
以确保至少存在这些属性。
为什么?
根据json-schema.org/understanding-json-schema(强调我的):
The additionalProperties
keyword is used to control the handling of extra stuff, that is, properties
whose names are not listed in the properties keyword. By default any additional properties are allowed.
The additionalProperties
keyword may be either a boolean or an object. If additionalProperties
is a boolean and set to false, no additional properties will be allowed.
解决您发布的屏幕截图以及实例通过的原因:
- 架构正在查找
person
属性,但 属性 不存在。
- 架构未声明 [=10=] 是必需的。
- 该架构未声明对未定义属性的要求,因此它将始终接受
personsdfsd
属性 中的任何值,而无需进一步检查。
简而言之,您的 JSON 数据很糟糕,您的模式没有任何保护措施。
除此之外,您的架构看起来不错。它应该验证 children
属性 中的项目是否匹配 person
定义的子模式。
我正在浏览文档以尝试弄清楚循环是如何工作的,这样我就可以验证 object 数组中的每个 object 都与模式匹配。
递归似乎是我想要的,但给出的示例不起作用:https://json-schema.org/understanding-json-schema/structuring.html
我正在尝试验证该示例,但它始终“有效”。我尝试更改 JSON 中的所有字段名称,但没关系:
不确定发生了什么。对于这个例子,我将如何验证每个 child 匹配的人模式(而不是在模式中静态地写出每个)。
例如,我想验证这个JSON。 toplevel
下可以有任意数量的 object,“objectsList” 下可以有任意数量的 object。我想确保“objectsList”下的每个 object 都具有正确的字段名称和类型(同样无需在架构中对整个内容进行硬编码):
{
"toplevel": {
"objectOne": {
"objectsList": [
{
"field1": 1231,
"field2": "sekfjlskjflsdf",
"field3": ["ssss","eeee"],
},
{
"field1": 11,
"field2": "sef",
"field3": ["eeee","qqqq"],
},
{
"field1": 1231,
"field2": "wwwww",
"field3": ["sisjflkssss","esdfsdeee"],
},
]
},
"objectTwo": {
"objectsList": [
{
"field1": 99999,
"field2": "yuyuyuyuyu",
"field3": ["ssssuuu","eeeeeee"],
},
{
"field1": 221,
"field2": "vesdlkfjssef",
"field3": ["ewerweeee","ddddq"],
},
]
},
}
}
怎么了?
这里的问题不是递归——你的模式看起来不错。
根本问题与此处相同:
JSON Schema is designed for extensibility. That means it allows any kind of additional properties to be added as long as they are not conflicting with the known/expected keywords.
解决方案
这里的解决方案是在您的“人”(来自屏幕截图)和顶级架构中添加 "additionalProperties": false
以防止接受那些不正确的对象。第二个示例也是如此:在 "type": "object"
的任何定义中,如果您不想定义这些无关的属性,则必须添加 "additionalProperties": false
。
或者,您可以将预期属性声明为 required
以确保至少存在这些属性。
为什么?
根据json-schema.org/understanding-json-schema(强调我的):
The
additionalProperties
keyword is used to control the handling of extra stuff, that is,properties
whose names are not listed in the properties keyword. By default any additional properties are allowed.The
additionalProperties
keyword may be either a boolean or an object. IfadditionalProperties
is a boolean and set to false, no additional properties will be allowed.
解决您发布的屏幕截图以及实例通过的原因:
- 架构正在查找
person
属性,但 属性 不存在。 - 架构未声明 [=10=] 是必需的。
- 该架构未声明对未定义属性的要求,因此它将始终接受
personsdfsd
属性 中的任何值,而无需进一步检查。
简而言之,您的 JSON 数据很糟糕,您的模式没有任何保护措施。
除此之外,您的架构看起来不错。它应该验证 children
属性 中的项目是否匹配 person
定义的子模式。