JSON 模式:如何检查数组是否至少包含一个具有给定值的 属性 的对象?
JSON Schema: How to check that an array contains at least one object with a property with a given value?
如何检查以下 json 数组 names
中的至少一个元素具有 属性 nickName
的值 Ginny
?
{
"names": [
{
"firstName": "Hermione",
"lastName": "Granger"
}, {
"firstName": "Harry",
"lastName": "Potter"
}, {
"firstName": "Ron",
"lastName": "Weasley"
}, {
"firstName": "Ginevra",
"lastName": "Weasley",
"nickName": "Ginny"
}
]
}
目前我使用的是 draft-06 版本(FAQ here)。
这是我不工作的模式:
{
"$schema": "http://json-schema.org/draft-06/schema#",
"title": "Complex Array",
"description": "Schema to validate the presence and value of an object within an array.",
"type": "object",
"properties": {
"names": {
"type": "array",
"minItems": 1,
"items": {
"type": "object",
"properties": {
"firstName": {
"type": "string"
},
"lastName": {
"type": "string"
},
"nickName": {
"type": "string"
}
},
"anyOf": [
{"required": ["nickName"]}
]
}
}
}
}
我设法用 draft-06
弄明白了。在此版本中添加了一个新关键字 contains
。根据这个草案 specification:
contains
The value of this keyword MUST be a valid JSON Schema.
An array instance is valid against "contains" if at least one of its elements is valid against the given schema.
工作架构:
{
"$schema": "http://json-schema.org/draft-06/schema#",
"title": "Complex Array",
"type": "object",
"properties": {
"names": {
"type": "array",
"minItems": 1,
"contains": {
"type": "object",
"properties": {
"firstName": {
"type": "string"
},
"lastName": {
"type": "string"
},
"nickName": {
"type": "string",
"pattern": "^Ginny$"
}
},
"required": ["nickName"]
},
"items": {
"type": "object",
"properties": {
"firstName": {
"type": "string"
},
"lastName": {
"type": "string"
},
"nickName": {
"type": "string"
}
}
}
}
}
}
如何检查以下 json 数组 names
中的至少一个元素具有 属性 nickName
的值 Ginny
?
{
"names": [
{
"firstName": "Hermione",
"lastName": "Granger"
}, {
"firstName": "Harry",
"lastName": "Potter"
}, {
"firstName": "Ron",
"lastName": "Weasley"
}, {
"firstName": "Ginevra",
"lastName": "Weasley",
"nickName": "Ginny"
}
]
}
目前我使用的是 draft-06 版本(FAQ here)。
这是我不工作的模式:
{
"$schema": "http://json-schema.org/draft-06/schema#",
"title": "Complex Array",
"description": "Schema to validate the presence and value of an object within an array.",
"type": "object",
"properties": {
"names": {
"type": "array",
"minItems": 1,
"items": {
"type": "object",
"properties": {
"firstName": {
"type": "string"
},
"lastName": {
"type": "string"
},
"nickName": {
"type": "string"
}
},
"anyOf": [
{"required": ["nickName"]}
]
}
}
}
}
我设法用 draft-06
弄明白了。在此版本中添加了一个新关键字 contains
。根据这个草案 specification:
contains
The value of this keyword MUST be a valid JSON Schema. An array instance is valid against "contains" if at least one of its elements is valid against the given schema.
工作架构:
{
"$schema": "http://json-schema.org/draft-06/schema#",
"title": "Complex Array",
"type": "object",
"properties": {
"names": {
"type": "array",
"minItems": 1,
"contains": {
"type": "object",
"properties": {
"firstName": {
"type": "string"
},
"lastName": {
"type": "string"
},
"nickName": {
"type": "string",
"pattern": "^Ginny$"
}
},
"required": ["nickName"]
},
"items": {
"type": "object",
"properties": {
"firstName": {
"type": "string"
},
"lastName": {
"type": "string"
},
"nickName": {
"type": "string"
}
}
}
}
}
}