【问题标题】:How to use additionalProperties with allOf in JSON schema?如何在 JSON 模式中将附加属性与 allOf 一起使用?
【发布时间】:2016-12-08 20:04:54
【问题描述】:

考虑这个例子:

"allOf": [
    {"$ref": "test-address-prefix-types-base.json#"},
    {
        "properties": {}, "additionalProperties" : false
    }
]}

当我使用 Java 模式验证器进行验证时,我收到错误消息:

"keyword":"additionalProperties","message":"object instance has properties which are not allowed by the schema: [\"attributes\",\"type\"]"}]

但针对基本架构 (test-address-prefix-types-base) 验证的相同 JSON 对象通过且没有错误。

引用的架构(基础架构)没有设置附加属性。

这是我正在使用的 json 消息:

        String message = "{\"data\":{\"attributes\":{" +
            "\"notation\": \"A\"," +
            "\"prefixType\": \"A\"}" +
            ",\"type\":\"test-address-prefix-types\"}}";

我错过了架构中的任何内容吗? 谢谢

【问题讨论】:

    标签: json jsonschema json-schema-validator


    【解决方案1】:

    您的架构可以这样扩展:

    allof:它必须针对两个模式验证 independently

    • 第一个具有通过ref 链接的任意属性。
    • 第二个不允许任何属性"additionalProperties" : false,除了那些在空集"properties" : {} 中定义的属性。换句话说,它不能有任何属性。

    这个问题可能会在标准草案 5 中得到解决。更多信息请访问the following SO question

    【讨论】:

    • 所以问题是,这个“合并”只是一个提案,还没有发布,对吧?我已经检查了json-schema.org 中的 json 模式规范,但没有提及它。我们正在使用 AJV 进行验证,目前还不支持合并。
    • 是的,您还不能安全地应用它。如果可能,您可以将 "additionalProperties" : false 添加到引用的架构中。
    • 你知道 Json Schema Validator (github.com/daveclayton/json-schema-validator) 是否支持 $merge 吗?我刚刚意识到 AJV 添加了对 $merge 的支持,但我们正在使用适用于 Java 的 Json 模式验证器,从我在这里看到的github.com/daveclayton/json-schema-validator/wiki/v5:-merge 它支持 $merge 但我无法让它工作?它返回错误,因为 $merge 未被识别
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-06-11
    • 1970-01-01
    • 1970-01-01
    • 2014-05-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多