Arrays minItems不';在JSON模式中似乎无法正确验证
我正在编写一个简单的JSON模式,并使用Arrays minItems不';在JSON模式中似乎无法正确验证,arrays,json,validation,jsonschema,Arrays,Json,Validation,Jsonschema,我正在编写一个简单的JSON模式,并使用minItems验证给定数组中的项数。我的模式如下: { "title": "My Schema", "type": "object", "properties": { "root": { "type": "array", "properties": { "id": { "type": "string" }, "m
minItems
验证给定数组中的项数。我的模式如下:
{
"title": "My Schema",
"type": "object",
"properties": {
"root": {
"type": "array",
"properties": {
"id": {
"type": "string"
},
"myarray": {
"type": "array",
"items": {
"type": "string"
},
"minItems": 4,
"uniqueItems": true
},
"boolean": {
"type": "boolean"
}
},
"required": ["id","myarray","boolean"]
}
},
"required": [
"root"
],
"additionalProperties": false
}
现在,如果元素myarray
中没有任何内容,我希望下面的JSON验证失败。但使用时,它会通过。我是否做错了什么,或者我使用的模式验证器是否有问题
{
"root":[
{
"id":"1234567890",
"myarray":[],
"boolean":true
}
]
}
我不知道为什么或者它叫什么,但是您的需求的正确模式定义应该如下面所示 根据我对JSON模式定义的理解,您应该在items声明中声明数组的属性。在模式中,您可以在数组项声明之外定义属性 在模式中,有两种不同类型的数组声明:
- 一次只使用一个对象(“myarray”对象的字符串)
- 使用复杂对象(下面代码中的对象名“myComplexType”)时
{
"title": "My Schema",
"type": "object",
"properties": {
"root": {
"type": "array",
"items": { <-- Difference here - "items" instead of "properties"
"type": "object", <-- here - define the array items as a complex object
"title": "myComplexType", <-- here - named for easier referencing
"properties": { <-- and here - now we can define the actual properties of the object
"id": {
"type": "string"
},
"myarray": {
"type": "array",
"items": {
"type": "string"
},
"minItems": 4,
"uniqueItems": true
},
"boolean": {
"type": "boolean"
}
}
},
"required": [
"id",
"myarray",
"boolean"
]
}
},
"required": [
"root"
],
"additionalProperties": false
}
{
“标题”:“我的模式”,
“类型”:“对象”,
“财产”:{
“根”:{
“类型”:“数组”,
“项目”:{您的模式唯一的错误是root
属性的类型应该是object
,而不是array
。因为properties
关键字没有为数组定义,所以它被忽略。因此,您尝试测试的模式部分被完全忽略,即使我t是对的
这是说明书中的相关段落
某些验证关键字仅适用于一个或多个基元类型。如果给定关键字无法验证实例的基元类型,则应成功验证此关键字和实例
谢谢,这工作得很好。至于你最后的评论——可能是验证器只是确保它是有效的JSON,而不是有效的JSON模式,谁知道呢。。。