我使用ajv在寫入數據庫之前驗證一些JSON數據。我的請求數據基本上看起來像這樣(作爲一個例子):ajv validate failed with correct schema「data should not have additional properties」
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"
}
]
}
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,那麼驗證就會通過。