ajv validate使用正确的模式失败"数据不应该有其他属性"

时间:2017-08-11 19:03:05

标签: node.js validation jsonschema ajv

我使用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"
        }
    ]
}

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的属性,然后验证将通过。

1 个答案:

答案 0 :(得分:0)

原来问题在于/foo#{pat}bar/ 关键字和pat = "[a-z]+" gsub("foo" pat "bar", rep, target) on" Testschema。"请参阅:https://spacetelescope.github.io/understanding-json-schema/reference/combining.html#allof

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