我正在使用JSON模式进行数据建模。 我定义了一个基本的文档(Document)
模式,以后我将用它来定义模型模式(例如产品(Product)
,类别(Category)
,用户(User)
等)。
我这样做是因为我希望所有模型都继承特定的结构/规则。 例如,每个模型实例都应具有某些通用属性(例如id
,createdAt
,updatedAt
)。 在面向对象编程(OOP)术语中:Product extends Document
,因此它继承了其实例属性。 在模式术语(我想)中,Document
是用于创建模型模式的元模式。
我已经按以下方式定义了Document模式:
{
"$schema": "http://json-schema.org/draft-07/schema#",
"id": "http://example.com/schemas/document.json#",
"title": "Document",
"type": "object",
"additionalProperties": false,
"required": ["type", "name", "fields"],
"properties": {
"type": {
"constant": "document"
},
"name": {
"type": "string"
},
"title": {
"type": "string"
},
"description": {
"type": "string"
},
"readOnly": {
"type": "boolean"
},
"properties": {
// common properties
// model-specific properties
}
}
}
- 如何指定文档元模式 "extends" 基础 JSON 模式 (draft-07),以便不必定义草案的所有属性 (
$schema
,id
等)? - 如何指定每个模型模式的
properties
包含一些通用属性 (id
,createdAt
, ...),而无需在每个模型模式定义中定义它们?
bar
字段必须同时符合基本架构中的字符串类型和覆盖架构中的整数类型吗?由于这是不可能的,因此任何JSON都无法通过覆盖架构的验证,对吗? - md2perpeallOf
。在早期的 JSON Schema 草案中,如果对象中有$ref
,则它被认为只是一个引用,而不是模式。这意味着该引用内部的任何 JSON Schema 关键字都会被忽略,因为关键字仅在模式内有意义。这实际上意味着$ref
必须总是单独存在,并且我们需要使用allOf
来组合引用和模式。在新版本的$ref
中,该引用是$ref
关键字的字符串值,且$ref
的作用类似于具有一个模式的allOf
。 - Jason DesrosiersadditionalProperties
替换为unevaluatedProperties
,它将按您的期望工作。 - Jason Desrosiers