为什么';t json模式验证在required属性中定义的定义
我试图创建一个json模式,根据对象的类型验证对象。它选择正确的定义,但是,它不会验证所选定义中所需的属性。以下是我正在尝试的json模式:为什么';t json模式验证在required属性中定义的定义,json,jsonschema,Json,Jsonschema,我试图创建一个json模式,根据对象的类型验证对象。它选择正确的定义,但是,它不会验证所选定义中所需的属性。以下是我正在尝试的json模式: { "$schema": "http://json-schema.org/draft-07/schema#", "definitions": { "literal": { "type": "object", "properties": { "r
{
"$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模式规范中没有关键字
内容
一旦在根模式中断言了“type”:“object”
,就不需要在子模式中再次断言
为了将对象type
枚举值与关联的扩展定义相结合,您需要allOf
关键字
在定义中,如果使用“additionalProperties”:false,则必须列出对象的所有属性(请参见“type”:{}
)。对于以前定义/验证的属性,您可以只使用许可模式:{}
或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模式规范中没有关键字content
一旦在根模式中断言了“type”:“object”
,就不需要在子模式中再次断言
为了将对象type
枚举值与关联的扩展定义相结合,您需要allOf
关键字
在定义中,如果使用“additionalProperties”:false,则必须列出对象的所有属性(请参见“type”:{}
)。对于以前定义/验证的属性,您可以只使用许可模式:{}
或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"]
}
好奇地想知道“内容”
从哪里来。OP你能解释一下吗?谢谢,这似乎已经解决了:)@Relequestual我可能是从一个无效的示例复制粘贴了“content”属性。我花了几个小时研究这个问题,所以最终结果在这里复制,有多处修改,很难知道“内容”
是从哪里来的。OP你能解释一下吗?谢谢,这似乎已经解决了:)@Relequestual我可能是从一个无效的示例复制粘贴了“content”属性。我花了几个小时研究这个问题,所以最终结果在这里复制,有多处修改