数组类型的 jsonschema 验证不正确的数据,如何解决?
jsonschema for array type validates incorrect data, how to fix?
我有以下 jsonschema:
{
"$schema": "http://json-schema.org/schema#",
"type": "object",
"properties": {
"abc": {
"type": "array",
"item": {
"type": "object",
"minItems": 1,
"properties": {
"a" : {"type": "string"},
"b" : {"type": "string"}
},
"required": [ "a", "b" ]
}
}
},
"required": [ "abc" ]
}
如果我将以下数据传递给验证器:
{
"abc": [
{
},
{
}
]
}
验证器不会输出错误,但这样的数据不正确。
您使用了 item
而不是 items
。
此外,"minItems": 1
需要向上移动到父对象。
{
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "object",
"properties": {
"abc": {
"type": "array",
"minItems": 1,
"items": {
"type": "object",
"properties": {
"a": {
"type": "string"
},
"b": {
"type": "string"
}
},
"required": [
"a",
"b"
]
}
}
},
"required": [
"abc"
]
}
检查和验证
我有以下 jsonschema:
{
"$schema": "http://json-schema.org/schema#",
"type": "object",
"properties": {
"abc": {
"type": "array",
"item": {
"type": "object",
"minItems": 1,
"properties": {
"a" : {"type": "string"},
"b" : {"type": "string"}
},
"required": [ "a", "b" ]
}
}
},
"required": [ "abc" ]
}
如果我将以下数据传递给验证器:
{
"abc": [
{
},
{
}
]
}
验证器不会输出错误,但这样的数据不正确。
您使用了 item
而不是 items
。
此外,"minItems": 1
需要向上移动到父对象。
{
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "object",
"properties": {
"abc": {
"type": "array",
"minItems": 1,
"items": {
"type": "object",
"properties": {
"a": {
"type": "string"
},
"b": {
"type": "string"
}
},
"required": [
"a",
"b"
]
}
}
},
"required": [
"abc"
]
}
检查和验证