我正在使用ajv在将某些 JSON 数据写入数据库之前对其进行验证。我的请求数据基本上看起来像这样(例如):
文档:
"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"
}
]
}
Testschema 认为:
{
"id": "Testschema",
"type": "object",
"properties": {
"name": {
"type": "string"
}
},
"additionalProperties": false,
"required": [
"name"
]
}
虽然 someDefinition 持有这一点:
{
"id": "anotherId",
"type": "object",
"definitions": {
"someDefinition": {
"type": "object",
"properties": {
"id": {
"type": "string"
}
}
}
}
}
但是,验证失败,并且我得到的错误是"data should NOT have additional properties"
明确地,“anotherId”模式中的任何内容由于某种原因验证失败。如果我在哪里将“id”属性添加到 Testschema,那么验证就会通过。