我生成了以下JSON:
{
"someString" : "example",
"obj1" : {
"opt1" : 1,
"opt2" : 1,
"opt3" : "aaa"
},
"obj2" : {
"opt1" : 55,
"opt2" : 55,
"opt3" : "bbb"
}
}
并且会有更多具有相同数据类型的对象(obj1,obj2,obj3,obj4,...)(opt1,opt2,opt3)
现在我想为此创建架构,但我不知道如何在架构中组合所有这些对象。
编辑:
我创建了架构:
root: {
"type" : "object",
"oneOf" : [
{
"properties" : {
"someString" : { "type" : "string" }
},
"patternProperties" : { "^.*$" : { "$ref" : "./schemas/myPatternProperties.json#" } },
"additionalProperties" : false }
}
]
}
和myPatternProperties.json看起来:
{
"type" : "object",
"properties" : {
"opt1" : { "type" : "number" },
"opt2" : { "type" : "number" },
"opt3" : { "type" : "string" },
}
"required" : [ "opt1", "opt2", "opt3" ]
}
是否有任何错误,因为我生成的JSON仍未被识别为此架构类型。
答案 0 :(得分:2)
据我所知,你的问题是describe object with a lot of properties with the same type and some naming rules
。要解决此问题,您必须指定patternProperties
部分
{
"patternProperties": {
"^(/[^/]+)+$": { "$ref": "http://some.site.somewhere/entry-schema#" }
}
该构造为属性指定pattern to match
。示例how to use patternProperties详情请见specification
更新
实际上,完整的方案必须是那样的
{
"$schema": "http://json-schema.org/draft-06/schema#",
"type": "object",
"properties": {
"someString": {
"type": "string"
}
},
"patternProperties": {
"^obj([0-9]+)$": {
"$ref": "#/definitions/objEntity"
}
},
"additionalProperties": false,
"required": [ "someString" ],
"definitions": {
"objEntity": {
"type": "object",
"properties": {
"opt1": { "type": "number" },
"opt2": { "type": "number" },
"opt3": { "type": "string" }
},
"required": ["opt1", "opt2", "opt3"]
}
}
}
当然,您可以将该方案拆分为多个文件,并将链接更改为类型定义。