允许引用架构中具有其他属性,但不允许其他属性

时间:2020-09-30 18:07:41

标签: jsonschema

假设我有两个用于验证json文件的架构。

testSchema.json

{
    "$schema": "http://json-schema.org/draft-07/schema",
    "type": "object",
    "additionalProperties": false,
    "properties": {
        "$schema": { "type": "string" },
        "sample": { "type": "number" }
    },
    "anyOf": [
        { "$ref": "./testSchema2.json" },
        {}
    ]
}

testSchema2.json

{
    "$schema": "http://json-schema.org/draft-04/schema",
    "type": "object",
    "properties": {
        "test": { "type": "string" },
        "test2": { "type": "number" }
    }
}

test.json

{
    "$schema": "../testSchema.json",
    "sample": 0,
    "test": "some text" //this line throws error "Property is not allowed"
}

我希望针对包含的架构的属性和所引用的任何架构的属性对文件进行验证。我想念什么吗?

编辑:我想排除任何未在我包含/引用的架构中明确定义的对象。

1 个答案:

答案 0 :(得分:1)

从JSON模式草稿2019-09(在draft-07之后),可以使用unevaluatedProperties关键字。

additionalProperties不能“透视”应用程序关键字,例如“ anyOf”和“ $ ref”,并且只能基于同一架构对象中的属性来工作。

draft-07或更高版本无法实现。