【问题标题】:JSON Schema draft v7 - definition of additional enum properties based on common enum type selection - testing in XMLSpy Pro 2020JSON Schema Draft v7 - 基于通用枚举类型选择的附加枚举属性定义 - 在 XMLSpy Pro 2020 中进行测试
【发布时间】: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


    【解决方案1】:

    JSON Schema properties 对象的值是子模式(JSON 模式本身)。

    知道,如果您在 properties.FurnitureData.anyOf[1].properties['Furniture Type'] 获取子模式,并将其作为模式......它实际上并没有表达任何约束。

    您的架构中该位置的子架构是...

    {
      "value": {
        "const": "TABLE"
      }
    }
    

    而它需要......

    {
      "properties":{
        "value": {
          "const": "TABLE"
        }
      }
    }
    

    调试此类问题的最简单方法是测试您的假设。

    您假设 allOf[1]allOf[2] 应该失败,因此将这些子模式替换为 false(布尔值是有效的模式)。

    这样做,你会发现假设是错误的,allOf[1] 是有效的。当然,您希望 const 被选中,因此这个子模式会失败,但它没有被选中,因为您缺少 propertiesvalue 不是有效的 JSON 模式关键字。

    您可以使用https://jsonschema.dev/s/Xt1Yi 运行这些快速测试

    【讨论】:

    • 这是一个子模式.. 非常感谢。这样的日子已经过去了。还有一个问题,这是最好的方法吗?有没有更短的方法?我有 20 个字段的大约 80 个“任意”变体需要调整。另外,还在做一些测试……只是需要一点时间来确认今天晚些时候一切正常。非常感谢!
    • 很抱歉您花了这么长时间调试这个问题!随意加入 JSON Schema slack。几乎每天都有专家在那里工作=]
    • 如果您愿意,请随时通过链接网站给我买杯咖啡
    • 一杯当之无愧的咖啡...如果您有任何想法,如果这是获得任何变化的长列表的最佳方式,请告诉我
    • 不客气!这种结构和方法有效,但我个人更喜欢使用if/then/else 关键字来显示更清晰的意图并打破验证问题。如果您想加入我们的 slack,很高兴与您一起解决这个问题 =]
    猜你喜欢
    • 2018-04-18
    • 2022-12-17
    • 2022-01-23
    • 2020-10-07
    • 2020-07-19
    • 1970-01-01
    • 1970-01-01
    • 2019-03-23
    • 1970-01-01
    相关资源
    最近更新 更多