Json 数组或对象的模式验证
Json Schema Validation of Array or Objects
我有一组验证选项:
{
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "parsePosition Validator",
"type": "object",
"properties": {
"option1": {
"type": "array",
"items": {
"tpye": "number"
},
"minItems": 3
},
"option2": {
"type": "array",
"items": {
"tpye": "number"
},
"minItems": 3
},
"option3": {
"oneOf": [
{
"type": "array",
"items": {
"tpye": "number"
},
"minItems": 3
},
{
"x": { "type":"number" },
"y": { "type":"number" },
"z": { "type":"number" },
"required": ["x","y","z"]
}
}
},
"oneOf": [
{ "required":["option1"] },
{ "required":["option2"] },
{ "required":["option3"] }
]
}
本质上json的输入可以是三个选项中的任何一个。尝试验证选项 3 时出现我的问题。可能的输入可能是:
{
"option3": [1,2,3]
}
或
{
"option3": {
"x": 1,
"y": 2,
"z": 3
}
}
但是,当我使用数组而不是对象时,验证器不会抛出空数组或数组大小不正确的问题。
我是不是漏掉了什么?
你错过了一些非常简单的东西......
您还没有将 type: object
放入 oneOf
的第二个子架构中。对于选项 3.
你需要这个的原因是 JSON 模式是一种基于约束的语言。
指定properties
,并不意味着实例位置必须是一个对象;你还需要说“这个位置应该是一个对象”。
任何大小的数组在您的子模式下都是有效的 oneOf[1]
,因为 properties
和 `required 仅适用于对象。
我有一组验证选项:
{
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "parsePosition Validator",
"type": "object",
"properties": {
"option1": {
"type": "array",
"items": {
"tpye": "number"
},
"minItems": 3
},
"option2": {
"type": "array",
"items": {
"tpye": "number"
},
"minItems": 3
},
"option3": {
"oneOf": [
{
"type": "array",
"items": {
"tpye": "number"
},
"minItems": 3
},
{
"x": { "type":"number" },
"y": { "type":"number" },
"z": { "type":"number" },
"required": ["x","y","z"]
}
}
},
"oneOf": [
{ "required":["option1"] },
{ "required":["option2"] },
{ "required":["option3"] }
]
}
本质上json的输入可以是三个选项中的任何一个。尝试验证选项 3 时出现我的问题。可能的输入可能是:
{
"option3": [1,2,3]
}
或
{
"option3": {
"x": 1,
"y": 2,
"z": 3
}
}
但是,当我使用数组而不是对象时,验证器不会抛出空数组或数组大小不正确的问题。
我是不是漏掉了什么?
你错过了一些非常简单的东西......
您还没有将 type: object
放入 oneOf
的第二个子架构中。对于选项 3.
你需要这个的原因是 JSON 模式是一种基于约束的语言。
指定properties
,并不意味着实例位置必须是一个对象;你还需要说“这个位置应该是一个对象”。
任何大小的数组在您的子模式下都是有效的 oneOf[1]
,因为 properties
和 `required 仅适用于对象。