我从 http://json-schema.org/examples.html 得到了以下架构,我想知道所需的关键字是否只能出现在顶层。或者如果有对象类型的属性,它也可以包含在属性中。我在规范中找不到与此相关的任何内容 https://datatracker.ietf.org/doc/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"
]
}
最佳答案
4.4 Keywords with the possibility to validate container instances (arrays or objects) only validate the instances themselves and not their children (array items or object properties).
所以我看到是的,你可以在任何级别上进行验证,但验证应该只在需要的同一级别上考虑
关于json - "required"JSON 模式中的关键字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29389583/