背景:嗨!我一直在尝试开发一种模式,该模式验证对象中的数组是否仅包含在我的definitions
块中定义的一些对象。
例如,我有以下JSON数据需要验证:
{
"component_type":"section",
"component_properties":{
"section_name":"first_section",
"fields":[
{
"component_type":"spacer",
"component_properties":{
"width":6
}
},
{
"component_type":"textbox",
"component_properties":{
"display_text":"hello!",
"text_color":"black"
}
},
{
"component_type":"spacer",
"component_properties":{
"width":3
}
}
]
}
}
该数据背后的思想是,在最高级别定义了一个section
"组件",其中填充了定义为也要验证的子组件(spacer
、textbox
(。
我的主要问题是:我不太清楚如何验证只由几个定义的对象组成的数组。我当前的模式如下:
{
"$schema": "http://json-schema.org/draft-07/schema",
"type": "object",
"properties": {
"component_type": {
"type": "string",
"const": "section"
},
"component_properties": {
"type": "object",
"properties": {
"section_name": { "type": "string"},
"sections": {
"type": "array",
"oneOf": [
{ "$ref": "#/definitions/section_field" },
{
"type": "array",
"items": {
"$ref": "#/definitions/section_field"
}
}
]
}
}
}
},
"definitions": {
"spacer": {
"type": "object",
"properties": {
"component_type": {
"type": "string",
"const": "spacer"
},
"component_properties": {
"width": "number"
}
}
},
"textbox": {
"type": "object",
"properties": {
"component_type": {
"type": "string",
"const": "textbox"
},
"component_properties": {
"display_text": "hello!",
"text_color": "purple"
}
}
},
"section_field": {
"oneOf": [
{
"$ref": "#/definitions/spacer"
},
{
"$ref": "#/definitions/textbox"
}
]
}
}
}
此模式不能确保节级别component_properties的"字段"数组中的所有项都具有字段"component_type": "spacer"
或"component_type": "textbox"
。例如,"component_type": "another_one"
应该无法通过验证。
如果有任何其他信息会有帮助,请随时告诉我!如果能在这里得到一些指导,我将不胜感激。
除了以下细节之外,您的模式几乎很好:
- 应删除
sections
属性中的”type”: “array”
。应该只保留”oneOf”
。否则,第一个"oneOf"
部分(单个元素(将永远无效 "component_properties"
应该是"type": "object"
本身,然后在其"properties"
中列出width
/display_text
/text_color
。它看起来就像你刚刚复制了一个示例内容
但是,您的模式接受您的示例是因为一个拼写错误:
- 在您的示例中,您有一个
fields
属性。但在你的模式中,它被称为sections
。我认为它在两者中都应该被称为fields
由于没有任何属性被标记为required
,因此给定的示例验证为fine。fields
属性没有约束。如果你把sections
标记为required
,它会抱怨的。
总而言之,您的模式的以下版本应该可以执行您想要的操作:
{
"$schema": "http://json-schema.org/draft-07/schema",
"type": "object",
"required": ["component_type", "component_properties"],
"properties": {
"component_type": { "const": "section" },
"component_properties": {
"type": "object",
"required": ["section_name", "fields"],
"properties": {
"section_name": { "type": "string" },
"fields": {
"oneOf": [
{ "$ref": "#/definitions/section_field" },
{
"type": "array",
"items": { "$ref": "#/definitions/section_field" }
}
]
}
}
}
},
"definitions": {
"spacer": {
"type": "object",
"required": ["component_type", "component_properties"],
"properties": {
"component_type": { "const": "spacer" },
"component_properties": {
"properties": {
"width": { "type": "number" }
}
}
}
},
"textbox": {
"type": "object",
"required": ["component_type", "component_properties"],
"properties": {
"component_type": { "const": "textbox" },
"component_properties": {
"type": "object",
"properties": {
"display_text": { "type": "string" },
"text_color": { "type": "string" }
}
}
}
},
"section_field": {
"oneOf": [
{ "$ref": "#/definitions/spacer" },
{ "$ref": "#/definitions/textbox" }
]
}
}
}