使用json-editor并查看此answer,我尝试使用json-schema v4执行以下操作:
使用root属性选择两个类别之一['cloting','accessory'],这将确定'material'属性的枚举值。
我试图解决的案例有多个enum-properties,具体取决于'category'-value。
伪代码示例:
{
"type": "object",
"Title": "Products",
"definitions": {
"clothing": {
"materials": ["yak", "merino"]
},
"accessories": {
"materials": ["brass", "silver"]
}
},
"properties": {
"productType": {
"type": "string",
"enum": [
"clothing",
"accessories"
]
},
"materials": {
"type": "array",
"title": "Materials",
"items": {
"type": "string",
"title": "Material",
"enum": [
{"$ref" : "#definitions/{{productType}}/materials"}
]
}
}
}
}
- 关于如何构建这个的任何建议?
答案 0 :(得分:-1)
在这种情况下,使用oneOf子句为第一级定义类型可能更容易,并且仅在嵌套级别使用枚举。类似的东西:
"definitions" : {
"materialType" : {
"oneOf" : [{
"$ref" : "#definitions/clothing"
}, {
"$ref" : "#definitions/accesories"
}
]
},
"clothing" : {
"materials" : {
"enum" : ["yak", "merino"]
}
},
"accessories" : {
"materials" : ["enum" : ["brass", "silver"]]
}
}
然后你使用这样的materialType:
"materials": {"$ref":"#definitions/materialType"}
如果你还要在对象实例中对materialType进行编码,那么你可以像这样添加另一个属性枚举,但我不建议这样做:
"clothing" : {
"materials" : {
"enum" : ["yak", "merino"]
},
"kind": {"enum" : ["clothing"]}
}