Pas*_*ito 6 jsonschema linked-data json-schema-validator json-schema-defaults
按照这些示例,我可以引用另一个 JSON 模式中声明的特定属性,并且一切都按预期进行,但我还没有找到一种方法,可以使用另一个基本模式的定义来扩展基本 JSON 模式,而不必显式引用每一个财产。
看起来这会很有用,但我还没有发现有迹象表明它可能或不可能。
想象一下基本模式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"]
}
Run Code Online (Sandbox Code Playgroud)
如果我想要一个更具体的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"]
}
Run Code Online (Sandbox Code Playgroud)
然而,我发现这种方法有两个问题:
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"]
}
Run Code Online (Sandbox Code Playgroud)
我尝试将其包含$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"]
}
Run Code Online (Sandbox Code Playgroud)
thing这具有继承属性但忽略所有附加属性的效果:
gender: Additional property gender is not allowed
nationality: Additional property nationality is not allowed
birthDate: Additional property birthDate is not allowed
Run Code Online (Sandbox Code Playgroud)
您正在寻找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"]
}
],
}
Run Code Online (Sandbox Code Playgroud)
或者,如我所愿,更简洁地写为
{
"$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"]
}
Run Code Online (Sandbox Code Playgroud)
请注意,使用此方法时,您不能使用"additionalProperties": false. 正是出于这个原因,我总是建议人们最好的做法是忽略其他属性,而不是明确禁止它们。