jsonschema - 从外部 JSON 模式导入所有定义

标签 jsonschema linked-data json-schema-validator json-schema-defaults

我一直在试验 JSON Pointers引用和重用 JSON schemas .

按照示例,我能够引用在另一个 JSON 模式中声明的特定属性,一切都按预期进行,但是我还没有找到一种方法来扩展基本 JSON 模式,而无需显式引用另一个基本模式的定义每一个属性(property)。

似乎这会很有用,但我还没有发现它可能与否的迹象。

想象一下基本架构 things :

{
    "$schema": "http://json-schema.org/draft-04/schema#",
    "id": "http://example.com/thing.json",
    "type": "object",
    "additionalProperties": false,
    "properties": {
        "url": {
            "id": "url",
            "type": "string",
            "format": "uri"
        },
        "name": {
            "id": "name",
            "type": "string"
        }
    },
    "required": ["name"]
}

如果我想要更具体的 person重用 thing 的两个属性的架构我可以:
{
    "$schema": "http://json-schema.org/draft-04/schema#",
    "id": "http://example.com/thing/person.json",
    "type": "object",
    "additionalProperties": false,
    "properties": {
        "url": {
            "$ref": "http://example.com/thing.json#/properties/url",
        },
        "name": {
            "$ref": "http://example.com/thing.json#/properties/name",
        },
        "gender": {
            "id": "gender",
            "type": "string",
            "enum": ["F", "M"]
        },
        "nationality": {
            "id": "nationality",
            "type": "string"
        },
        "birthDate": {
            "id": "birthDate",
            "type": "string",
            "format": "date-time"
        }
    },
    "required": ["gender"]
}

但是,我发现这种方法存在两个问题:
  • 一旦更新了超定义,依赖模式也必须更新
  • 手动维护所有这些引用变得繁琐/冗长
  • 规则(如 required: name )不是引用定义
  • 的一部分

    有没有办法通过使用单个全局引用来获得以下有效的 JSON 模式?
    {
        "$schema": "http://json-schema.org/draft-04/schema#",
        "id": "http://example.com/thing/person.json",
        "type": "object",
        "additionalProperties": false,
        "properties": {
            "url": {
                "id": "url",
                "type": "string",
                "format": "uri"
            },
            "name": {
                "id": "name",
                "type": "string"
            }
            "gender": {
                "id": "gender",
                "type": "string",
                "enum": ["F", "M"]
            },
            "nationality": {
                "id": "nationality",
                "type": "string"
            },
            "birthDate": {
                "id": "birthDate",
                "type": "string",
                "format": "date-time"
            }
        },
        "required": ["name", "gender"]
    }
    

    我试过包括 $ref在模式的根目录中,如下所示:
    {
        "$schema": "http://json-schema.org/draft-04/schema#",
        "id": "http://jsonschema.net/thing/person",
        "type": "object",
        "additionalProperties": false,
        "$ref": "http://example.com/thing.json",
        "properties": {
            "gender": {/* ... */},
            "nationality": {/* ... */},
            "birthDate": {/* ... */}
        },
        "required": ["gender"]
    }
    

    这具有继承thing的作用属性,但忽略所有其他属性:
    gender: Additional property gender is not allowed
    nationality: Additional property nationality is not allowed
    birthDate: Additional property birthDate is not allowed
    

    最佳答案

    您正在寻找 allOf关键词。 JSON Schema 不像我们很多人习惯的那样进行继承。相反,您可以告诉它数据需要对父模式(事物)和子模式(人)都有效。

    {
        "$schema": "http://json-schema.org/draft-04/schema#",
        "id": "http://example.com/thing.json",
        "type": "object",
        "properties": {
            "url": {
                "id": "url",
                "type": "string",
                "format": "uri"
            },
            "name": {
                "id": "name",
                "type": "string"
            }
        },
        "required": ["name"]
    }
    
    {
        "$schema": "http://json-schema.org/draft-04/schema#",
        "id": "http://example.com/thing/person.json",
        "allOf": [
            { "$ref": "http://example.com/thing.json" },
            {
                "type": "object",
                "properties": {
                    "gender": {
                        "id": "gender",
                        "type": "string",
                       "enum": ["F", "M"]
                    },
                    "nationality": {
                        "id": "nationality",
                        "type": "string"
                    },
                    "birthDate": {
                        "id": "birthDate",
                        "type": "string",
                        "format": "date-time"
                    }
                },
                "required": ["gender"]
            }
        ],
    }
    
    或者,如我所愿,更简洁地写成
    {
        "$schema": "http://json-schema.org/draft-04/schema#",
        "id": "http://example.com/thing/person.json",
        "allOf": [{ "$ref": "http://example.com/thing.json" }],
        "properties": {
            "gender": {
                "id": "gender",
                "type": "string",
                "enum": ["F", "M"]
            },
            "nationality": {
                "id": "nationality",
                "type": "string"
            },
            "birthDate": {
                "id": "birthDate",
                "type": "string",
                "format": "date-time"
            }
        },
        "required": ["gender"]
    }
    
    请注意,使用这种方法,您不能使用 "additionalProperties": false .正是出于这个原因,我总是建议人们最好的做法是忽略其他属性,而不是明确禁止它们。

    关于jsonschema - 从外部 JSON 模式导入所有定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34771998/

    相关文章:

    xml - 从 JSON 模式生成 XML 模式 (XSD)

    visual-studio - Visual Studio(使用 Resharper)中负数未通过 JSON 架构验证检查

    node.js - 如何声明嵌套属性的依赖关系?

    rdf - 尝试使用 MIREOT Protégé Plugin 重用外部术语时出现问题

    java - 如何使用 json-schema-validator 根据超模式验证 JSON

    jsonschema - Json 模式动态 key 验证

    java - JSON 架构 : External JSON Schema file is not validating json

    sparql - 如何获取 DBpedia 实体的 Wikidata ID?

    jsonschema - json-schema 描述对象键的值(当键是动态时)

    r - 使用 SPARQL 计算 DBpedia wikilink 和外部链接的数量