jsonschema 扩展且没有附加属性
jsonschema extending and no additional properties
我正在使用 jsonschema 来验证描述条目(给定类型)如何显示的条目类型。这些条目可以有页面并分为方面。
页面和方面都可以设置条件,我想重用一个基本模式,即使方面的条件可以有页面条件没有的 2 个其他属性。
这是一个我经常遇到的普遍问题。我想扩展架构,同时能够在所有情况下将 "additionalProperties" 设置为 false。
我也看不出有可能用 anyOf, allOf 修复它而不重复。
或者我应该放弃 additionalProperties
还是接受重复项?
{
"$comment": "page condition",
"type": "object",
"properties": {
"condition": {
"type": "object",
"properties": {
"aspect": {
"type": "string"
},
"value": {
"type": "string"
},
"compare": {
"type": "string"
}
},
"required": [
"aspect",
"value"
],
"additionalProperties": false
}
}
}
...
{
"$comment": "aspect condition",
"type": "object",
"properties": {
"condition": {
"type": "object",
"properties": {
"aspect": {
"type": "string"
},
"value": {
"type": "string"
},
"compare": {
"type": "string"
},
"disabled_text": {
"type": "string"
},
"default_pass": {
"type": "boolean"
}
},
"required": [
"aspect",
"value"
],
"additionalProperties": false
}
}
}
不幸的是,draft-7 JSON 模式无法解决这个问题。
您需要从您希望引用的任何架构中删除 additionalProperties: false
。
减少重复的一种方法是,在您的引用模式中,重新定义属性,但仅使用 true
的值。这意味着验证部分仍然发生在引用的模式本身中。
我在这张幻灯片的最近演讲中将此作为示例问题进行了修复:
https://stoic-agnesi-d0ac4a.netlify.com/32
结果架构的一部分:
{
"$schema": "http://json-schema.org/draft-07/schema",
"definitions": {
"geneticsPatient": {
"type": [
"object"
]
},
"regularPatient": {
"type": [
"object"
]
}
},
"properties": {
"patient": {
"additionalProperties": false,
"properties": {
"name": true,
"phenotypicFeatures": true,
"genomicFeatures": true
},
"allOf": [
{
"$ref": "#/definitions/regularPatient"
},
{
"$ref": "#/definitions/geneticsPatient"
}
]
}
}
}
在最近发布的 2019-09 草案中,我们添加了一个新关键字,但您仍然需要在引用的架构中不定义 additionalProperties: false
。
您可以从我的其他一些幻灯片中了解更多信息:https://speakerdeck.com/relequestual/json-schema-draft-8-to-vocabularies-and-beyond
我正在使用 jsonschema 来验证描述条目(给定类型)如何显示的条目类型。这些条目可以有页面并分为方面。
页面和方面都可以设置条件,我想重用一个基本模式,即使方面的条件可以有页面条件没有的 2 个其他属性。
这是一个我经常遇到的普遍问题。我想扩展架构,同时能够在所有情况下将 "additionalProperties" 设置为 false。
我也看不出有可能用 anyOf, allOf 修复它而不重复。
或者我应该放弃 additionalProperties
还是接受重复项?
{
"$comment": "page condition",
"type": "object",
"properties": {
"condition": {
"type": "object",
"properties": {
"aspect": {
"type": "string"
},
"value": {
"type": "string"
},
"compare": {
"type": "string"
}
},
"required": [
"aspect",
"value"
],
"additionalProperties": false
}
}
}
...
{
"$comment": "aspect condition",
"type": "object",
"properties": {
"condition": {
"type": "object",
"properties": {
"aspect": {
"type": "string"
},
"value": {
"type": "string"
},
"compare": {
"type": "string"
},
"disabled_text": {
"type": "string"
},
"default_pass": {
"type": "boolean"
}
},
"required": [
"aspect",
"value"
],
"additionalProperties": false
}
}
}
不幸的是,draft-7 JSON 模式无法解决这个问题。
您需要从您希望引用的任何架构中删除 additionalProperties: false
。
减少重复的一种方法是,在您的引用模式中,重新定义属性,但仅使用 true
的值。这意味着验证部分仍然发生在引用的模式本身中。
我在这张幻灯片的最近演讲中将此作为示例问题进行了修复: https://stoic-agnesi-d0ac4a.netlify.com/32
结果架构的一部分:
{
"$schema": "http://json-schema.org/draft-07/schema",
"definitions": {
"geneticsPatient": {
"type": [
"object"
]
},
"regularPatient": {
"type": [
"object"
]
}
},
"properties": {
"patient": {
"additionalProperties": false,
"properties": {
"name": true,
"phenotypicFeatures": true,
"genomicFeatures": true
},
"allOf": [
{
"$ref": "#/definitions/regularPatient"
},
{
"$ref": "#/definitions/geneticsPatient"
}
]
}
}
}
在最近发布的 2019-09 草案中,我们添加了一个新关键字,但您仍然需要在引用的架构中不定义 additionalProperties: false
。
您可以从我的其他一些幻灯片中了解更多信息:https://speakerdeck.com/relequestual/json-schema-draft-8-to-vocabularies-and-beyond