AJV 验证失败,架构"data should NOT have additional properties"正确



我正在使用ajv在将某些JSON数据写入数据库之前对其进行验证。我的请求数据基本上如下所示(例如(:

.doc:

"name": "John",
"id": "123-456-789"

这将传递给 ajv 验证器:

const validator: ajv.Ajv = this.getValidator();
validator.validate("Testschema.out", doc)

这就是Testschema.out的样子

{
"id": "Testschema.out",
"type": "object",
"allOf": [{
"$ref": "anotherId#/definitions/someDefinition"
},
{
"$ref": "Testschema"
}
]
}

测试模式保存:

{
"id": "Testschema",
"type": "object",
"properties": {
"name": {
"type": "string"
}
},
"additionalProperties": false,
"required": [
"name"
]
}

虽然一些定义认为:

{
"id": "anotherId",
"type": "object",
"definitions": {
"someDefinition": {
"type": "object",
"properties": {
"id": {
"type": "string"
}
}
}
}
}

但是,验证失败,我得到的错误是"data should NOT have additional properties"具体来说,由于某种原因,"otherId"架构中的任何内容都无法通过验证。如果我在哪里将"id"属性添加到测试架构,那么验证就会通过。

事实证明,问题出在allOf关键字和"Testschema"上的"additionalProperties": false上。请参阅:https://spacetelescope.github.io/understanding-json-schema/reference/combining.html#allof

似乎新的$merge关键字就是答案:https://github.com/epoberezkin/ajv-merge-patch

相关内容

最新更新