允许参考架构中的其他属性,但 none 其他

Allow additional properties in reference schemas, but none else

假设我有两个模式用于验证 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"
}

我希望根据包含的模式的属性和引用的任何模式的属性验证文件。我错过了什么吗?

编辑:我想排除任何未在我的 included/referenced 架构中明确定义的对象。

来自 JSON 架构草案 2019-09(在 draft-07 之后),这可以通过使用 unevaluatedProperties 关键字来实现。

additionalProperties 无法“看穿”诸如“anyOf”和“$ref”之类的应用程序关键字,只能基于同一模式对象中的属性起作用。

这对于 draft-07 或以前的版本是不可能的。