我从http://json-schema.org/examples.html获得了以下架构,我想知道所需关键字是否只能位于顶层。或者,如果存在object类型的属性,它也可以在属性中。我在规范http://tools.ietf.org/html/draft-fge-json-schema-validation-00#section-5.4.3中找不到与此相关的任何内容。
{
"title": "Example Schema",
"type": "object",
"properties": {
"firstName": {
"type": "string"
},
"lastName": {
"type": "string"
},
"age": {
"description": "Age in years",
"type": "integer",
"minimum": 0
}
},
"required": ["firstName", "lastName"]
}
所以下面的例子是一个有效的架构
{
"title":"Example Schema",
"type":"object",
"properties":{
"firstName":{
"type":"string"
},
"lastName":{
"type":"string"
},
"age":{
"type":"object",
"properties":{
"minAge":{
"type":"number"
},
"maxAge":{
"type":"number"
},
"required":[
"minAge",
"maxAge"
]
}
}
},
"required":[
"firstName",
"lastName"
]
}
答案 0 :(得分:1)
4.4可以验证容器实例的关键字(数组 或对象)仅验证实例本身而不是它们的实例 children(数组项或对象属性)。
所以我看到是的,你可以在任何级别上使用那些但是验证应该只考虑与所需的相同级别
答案 1 :(得分:0)
required
关键字可以出现在任何架构中。所有架构关键字都是如此。
(元关键字$schema
有一个特例,建议只有顶级关键字)
答案 2 :(得分:0)
是的,required
是任何架构中的有效关键字。嵌套模式没有限制。
要使用您的示例,以下是有效的架构,并将验证您希望的方式。
{
"title": "Example Schema",
"type": "object",
"properties": {
"firstName": {
"type": "string"
},
"lastName": {
"type": "string"
},
"age": {
"type": "object",
"properties": {
"minAge": {
"type": "number"
},
"maxAge": {
"type": "number"
}
},
"required": [
"minAge",
"maxAge"
]
}
},
"required": [
"firstName",
"lastName"
]
}