我一直在尝试JSON Pointers引用和重用JSON schemas导入所有定义。从外部JSON模式
以下的例子中,我能够引用其他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"]
}
如果我想更具体的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"]
}
但是这里有两个问题,我看到的这种方法:
- 一次超defin银行足球比赛被更新,依赖模式也必须进行更新
- 变得繁琐/冗长的手工维护所有这些引用
- 的规则(比如
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