【发布时间】:2018-05-25 05:15:08
【问题描述】:
我正在尝试创建一个根据值具有略微不同结构的架构,并决定使用草案 07 和 ajv 来验证它。 我正在尝试创建的结构如下 -
"url":{
"some-random-string":{
"pattern":"somevalue",
"handler":"one-of-a-few-allowed-values",
"kwargs":{ "conditional object" with further constraints}
}
}
其中,模式是必需的,某些kwargs 对象将具有其他必需的键。我尝试使用一系列 if..then 语句并结合如下参考:
"url": {
"type": "object",
"patternProperties": {
"^.*$": {
"properties": {
"pattern": {
"type": "string"
},
"handler": {
"type": "string",
"enum": ["val1","val2"...
]
}
},
"required": ["pattern"],
"if": {
"properties": {
"handler": {
"enum": ["val1"]
}
}
},
"then": {
"properties": {
"kwargs": {
"$ref": "#/definitions/val1"
}
}
},
"if": {
"properties": {
"handler": {
"enum": ["val2"]
}
}
},
"then": {
"properties": {
"kwargs": {
"$ref": "#/definitions/val2"
},"required":["function"]
}
},
所需的模式约束有效,所需的功能约束无效。
我什至尝试将所有 if-then 语句包装到一个 allOf 数组中,每组 if-then 都在一个对象中,但它似乎不起作用。
参考目前看起来像这样
"val2": {
"type": ["object", "boolean"],
"properties": {
"kwargs": {
"type": "object",
"properties": {
"function": {
"type": "string"
},
"methods": {
"type": "array",
"items": {
"enum": ["GET", "PUT", "POST", "DELETE", "OPTIONS"]
}
}
}
}
}
}
【问题讨论】:
标签: jsonschema ajv