【发布时间】:2020-06-14 22:16:26
【问题描述】:
我正在尝试定义一个 json 模式,该模式具有多个对象的通用枚举类型,然后根据选择的枚举,定义枚举的可能组合以及所需的其他元素。 该示例具有 {IDNumber, Color, Furniture Type} 的家具数据,然后根据从枚举列表中选择的类型,获取分配有不同枚举的函数。我还添加了一个“分配的人员”示例作为额外元素。
我认为我使用 anyof 和 const 正确地做到了这一点。但是,当我使用 XMLSpy Pro 2020 进行测试时,它会生成无效的 json 示例,并且当我尝试验证无效示例时,它也会通过....所以,1) 我表达得很好吗? 2)我做错了什么? 3)有更好的方法吗? 4) 是工具还是 json 模式? 请帮忙。
JSON Schema :
{
"$schema": "http://json-schema.org/draft-07/schema#",
"description": "Comment describing your JSON Schema",
"type": "object",
"properties": {
"FurnitureData": {
"type": "array",
"items": {
"type": "object",
"properties": {
"IDNumber": {
"type": "object",
"description": "Unique identifier",
"required": [
"value"
],
"properties": {
"value": {
"type": "string",
"maxLength": 16
},
"readOnly": {
"type": "boolean",
"default": true
}
}
},
"Color": {
"type": "object",
"description": "Preferred color",
"required": [
"value"
],
"properties": {
"value": {
"type": "string",
"default": "WOOD",
"enum": [
"BLUE",
"WOOD",
"GREEN"
]
},
"custom value": {
"type": "string",
"maxLength": 32
}
}
},
"Furniture Type": {
"type": "object",
"description": "Kind of Furniture",
"required": [
"value"
],
"properties": {
"value": {
"type": "string",
"default": "CHAIR",
"enum": [
"LAMP",
"TABLE",
"CHAIR"
]
},
"custom value": {
"type": "string",
"maxLength": 32
}
}
}
},
"required": [
"IDNumber",
"Color",
"Furniture Type"
],
"anyOf": [
{
"properties": {
"Furniture Type": {
"value": {
"const": "LAMP"
}
},
"Function": {
"type": "object",
"description": "Lighting arrangement",
"required": [
"value"
],
"properties": {
"value": {
"type": "string",
"enum": [
"LIGHT ON",
"LIGHT OFF"
]
}
}
}
},
"required": [
"Furniture Type",
"Function"
]
},
{
"properties": {
"Furniture Type": {
"value": {
"const": "TABLE"
}
},
"Function": {
"type": "object",
"description": "Size of table",
"required": [
"value"
],
"properties": {
"value": {
"type": "string",
"enum": [
"3' x 4'",
"6' x 4'",
"12' x 4'",
"10' round"
]
}
}
}
},
"required": [
"Furniture Type",
"Function"
]
},
{
"properties": {
"Furniture Type": {
"value": {
"const": "CHAIR"
}
},
"Function": {
"type": "object",
"description": "Planned use",
"required": [
"value"
],
"properties": {
"value": {
"type": "string",
"enum": [
"WORKSPACE SEAT",
"SPARE SEAT"
]
}
}
},
"Person assigned": {
"type": "object",
"description": "Seating assignment",
"required": [
"value"
],
"properties": {
"value": {
"type": "string"
}
}
}
},
"required": [
"Furniture Type",
"Function",
"Person assigned"
]
}
]
}
}
},
"required": [
"FurnitureData"
]
}
XMLSpy 验证为正常的无效 JSON 示例:(它链接到我在信息页面中的架构) 灯不应该允许 6x4 作为函数......
{
"FurnitureData": [
{
"Color": {
"value": "WOOD",
"readOnly": "a",
"custom value": "a"
},
"IDNumber": {
"value": "a",
"readOnly": true
},
"Furniture Type": {
"value": "LAMP",
"custom value": "a"
},
"Function": {
"value": "6' x 4'",
"custom value": "a"
}
}
]
}
另一个无效示例...椅子具有“分配的人员”并且显示了错误的类型值,但这也可以验证...
{
"FurnitureData": [
{
"Color": {
"value": "WOOD",
"readOnly": "a",
"custom value": "a"
},
"IDNumber": {
"value": "a",
"readOnly": true
},
"Furniture Type": {
"value": "CHAIR",
"custom value": "a"
},
"Function": {
"value": "6' x 4'",
"custom value": "a"
}
}
]
}
这是遵循中的建议 See enum section using anyof
也许我必须使用 if-then 构造?在这里,我在 any-of 中尝试了 if-then,但我也得到了 json 的验证,它允许来自其他家具类型的枚举......
{
"$schema": "http://json-schema.org/draft-07/schema#",
"description": "Comment describing your JSON Schema",
"type": "object",
"properties": {
"FurnitureData": {
"type": "array",
"items": {
"type": "object",
"properties": {
"IDNumber": {
"type": "object",
"description": "Unique identifier",
"required": [
"value"
],
"properties": {
"value": {
"type": "string",
"maxLength": 16
},
"readOnly": {
"type": "boolean",
"default": true
}
}
},
"Color": {
"type": "object",
"description": "Preferred color",
"required": [
"value",
"readOnly"
],
"properties": {
"value": {
"type": "string",
"default": "WOOD",
"enum": [
"BLUE",
"WOOD",
"GREEN"
]
},
"custom value": {
"type": "string",
"maxLength": 32
}
}
},
"Furniture Type": {
"type": "object",
"description": "Kind of Furniture",
"required": [
"value"
],
"properties": {
"value": {
"type": "string",
"default": "CHAIR",
"enum": [
"LAMP",
"TABLE",
"CHAIR"
]
},
"custom value": {
"type": "string",
"maxLength": 32
}
}
}
},
"required": [
"IDNumber",
"Color",
"Furniture Type"
],
"anyOf": [
{
"if": {
"properties": {
"Furniture Type": {
"value": {
"const": "LAMP"
}
}
}
},
"then": {
"properties": {
"Function": {
"type": "object",
"description": "Lighting arrangement",
"required": [
"value"
],
"properties": {
"value": {
"type": "string",
"enum": [
"LIGHT ON",
"LIGHT OFF"
]
}
}
}
},
"required": [
"Function"
]
}
},
{
"if": {
"properties": {
"Furniture Type": {
"value": {
"const": "TABLE"
}
}
}
},
"then": {
"properties": {
"Function": {
"type": "object",
"description": "Size of table",
"required": [
"value"
],
"properties": {
"value": {
"type": "string",
"enum": [
"3' x 4'",
"6' x 4'",
"12' x 4'",
"10' round"
]
}
}
}
},
"required": [
"Function"
]
}
},
{
"if": {
"properties": {
"Furniture Type": {
"value": {
"const": "CHAIR"
}
}
}
},
"then": {
"properties": {
"Function": {
"type": "object",
"description": "Planned use",
"required": [
"value"
],
"properties": {
"value": {
"type": "string",
"enum": [
"WORKSPACE SEAT",
"SPARE SEAT"
]
}
}
},
"Person assigned": {
"type": "object",
"description": "Seating assignment",
"required": [
"value"
],
"properties": {
"value": {
"type": "string"
}
}
}
},
"required": [
"Function",
"Person assigned"
]
}
}
]
}
}
},
"required": [
"FurnitureData"
]
}
【问题讨论】:
标签: json jsonschema xmlspy