为什么 json 架构不验证必需属性中定义的定义
why doesn't json schema validate definitions defined in required attribute
我正在尝试创建一个 json 模式来根据对象的类型验证对象。它会选择正确的定义,但是不会验证所选定义中的必需属性。这是我正在尝试的 json 模式:
{
"$schema": "http://json-schema.org/draft-07/schema#",
"definitions": {
"literal": {
"type": "object",
"properties": {
"raw": { "type": "string" }
},
"required": ["raw"],
"additionalProperties": false
},
"identifier": {
"type": "object",
"properties": {
"name": { "type": "string" }
},
"required": ["name"],
"additionalProperties": false
}
},
"type": "object",
"oneOf": [
{
"type": "object",
"properties": {
"type": {
"enum": ["Literal"]
},
"content": { "$ref": "#/definitions/literal" }
}
},
{
"type": "object",
"properties": {
"type": {
"enum": ["Identifier"]
},
"content": { "$ref": "#/definitions/identifier" }
}
}
],
"required": ["type"]
};
以下架构有效,即使缺少 "raw" 属性:
{ "type" : "Literal" }
谢谢
JSON 架构规范中没有关键字 content
。
一旦您在根架构中断言 "type":"object"
,就无需在子架构中再次执行此操作。
为了将对象 type
枚举值与关联的扩展定义相结合,您需要 allOf
关键字。
同样在定义中,如果您使用 "additionalProperties": false
,您必须列出对象的所有属性(参见 "type": {}
)。对于以前的 defined/validated 属性,您可以只使用宽松模式:{}
或 true
.
{
"$schema": "http://json-schema.org/draft-07/schema#",
"definitions": {
"literal": {
"properties": {
"type": {},
"raw": { "type": "string" }
},
"required": ["raw"],
"additionalProperties": false
},
"identifier": {
"properties": {
"type": {},
"name": { "type": "string" }
},
"required": ["name"],
"additionalProperties": false
}
},
"type": "object",
"oneOf": [
{
"allOf": [
{
"properties": {
"type": {
"enum": ["Literal"]
}
}
},
{"$ref": "#/definitions/literal"}
]
},
{
"allOf": [
{
"properties": {
"type": {
"enum": ["Identifier"]
}
}
},
{"$ref": "#/definitions/identifier" }
]
}
],
"required": ["type"]
}
我正在尝试创建一个 json 模式来根据对象的类型验证对象。它会选择正确的定义,但是不会验证所选定义中的必需属性。这是我正在尝试的 json 模式:
{
"$schema": "http://json-schema.org/draft-07/schema#",
"definitions": {
"literal": {
"type": "object",
"properties": {
"raw": { "type": "string" }
},
"required": ["raw"],
"additionalProperties": false
},
"identifier": {
"type": "object",
"properties": {
"name": { "type": "string" }
},
"required": ["name"],
"additionalProperties": false
}
},
"type": "object",
"oneOf": [
{
"type": "object",
"properties": {
"type": {
"enum": ["Literal"]
},
"content": { "$ref": "#/definitions/literal" }
}
},
{
"type": "object",
"properties": {
"type": {
"enum": ["Identifier"]
},
"content": { "$ref": "#/definitions/identifier" }
}
}
],
"required": ["type"]
};
以下架构有效,即使缺少 "raw" 属性:
{ "type" : "Literal" }
谢谢
JSON 架构规范中没有关键字 content
。
一旦您在根架构中断言 "type":"object"
,就无需在子架构中再次执行此操作。
为了将对象 type
枚举值与关联的扩展定义相结合,您需要 allOf
关键字。
同样在定义中,如果您使用 "additionalProperties": false
,您必须列出对象的所有属性(参见 "type": {}
)。对于以前的 defined/validated 属性,您可以只使用宽松模式:{}
或 true
.
{
"$schema": "http://json-schema.org/draft-07/schema#",
"definitions": {
"literal": {
"properties": {
"type": {},
"raw": { "type": "string" }
},
"required": ["raw"],
"additionalProperties": false
},
"identifier": {
"properties": {
"type": {},
"name": { "type": "string" }
},
"required": ["name"],
"additionalProperties": false
}
},
"type": "object",
"oneOf": [
{
"allOf": [
{
"properties": {
"type": {
"enum": ["Literal"]
}
}
},
{"$ref": "#/definitions/literal"}
]
},
{
"allOf": [
{
"properties": {
"type": {
"enum": ["Identifier"]
}
}
},
{"$ref": "#/definitions/identifier" }
]
}
],
"required": ["type"]
}