1

我正在尝试定义一个 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"
            }
        }
    ]
}

这是遵循 使用 anyof 查看枚举部分中的建议

也许我必须使用 if-then 构造?在这里,我尝试了 if-then 在 any-of 但我也得到了 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"
    ]
}
4

1 回答 1

1

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

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

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

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

而它需要...

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

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

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

这样做,您会发现假设是错误的,并且allOf[1]是有效的。当然,您希望const被拾取,因此这个子模式会失败,但它没有被拾起,因为您丢失properties并且value不是有效的 JSON 模式关键字。

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

于 2020-03-02T16:57:31.563 回答