0

我一直在尝试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"] 
} 

但是这里有两个问题,我看到的这种方法:

  1. 一次超defin银行足球比赛被更新,依赖模式也必须进行更新
  2. 变得繁琐/冗长的手工维护所有这些引用
  3. 的规则(比如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 

回答

2

您正在寻找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"] 
     } 
    ], 
} 

或者,就像我喜欢,写了consisely作为

{ 
    "$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。正因为如此,我总是建议人们最好的做法是忽略其他属性,而不是明确禁止它们。