AJV Multilevel/Nested JSON 模式验证
AJV Multilevel/Nested JSON schema validation
使用架构
{
"type": "object",
"required": [
"person",
"animal"
],
"person": {
"title": "person",
"type": "object",
"required": [
"name"
],
"properties": {
"name": {
"type": "string"
}
}
},
"animal": {
"title": "animal",
"type": "object",
"properties": {
"name": {
"type": "string"
}
}
}
}
此架构在与此对象进行比较时有效
{
"person": 0,
"animal": "dog"
}
我只希望它验证 person 对象中的属性(因为它也有必需的属性)。例如,只有以下内容有效:
{
"person": {
"name": "myName"
},
"animal": "dog"
}
如何确保使用 AJV 在我的模式中验证嵌套对象?
在您的架构中,您需要将 animal
和 person
放入 properties
对象中。
目前,由于那些 属性 键不在 properties
对象中,它们被归类为未知关键字并被忽略。
否则,是的,你猜对了。
使用架构
{
"type": "object",
"required": [
"person",
"animal"
],
"person": {
"title": "person",
"type": "object",
"required": [
"name"
],
"properties": {
"name": {
"type": "string"
}
}
},
"animal": {
"title": "animal",
"type": "object",
"properties": {
"name": {
"type": "string"
}
}
}
}
此架构在与此对象进行比较时有效
{
"person": 0,
"animal": "dog"
}
我只希望它验证 person 对象中的属性(因为它也有必需的属性)。例如,只有以下内容有效:
{
"person": {
"name": "myName"
},
"animal": "dog"
}
如何确保使用 AJV 在我的模式中验证嵌套对象?
在您的架构中,您需要将 animal
和 person
放入 properties
对象中。
目前,由于那些 属性 键不在 properties
对象中,它们被归类为未知关键字并被忽略。
否则,是的,你猜对了。