【发布时间】:2019-08-31 00:57:40
【问题描述】:
我正在尝试创建一个复杂的模式,该模式将检查属性的值,然后根据同一属性的值进行验证。我想知道是否可以在同一架构中使用 $ref 和 allOf ,如果可以,如何?我在让它工作时遇到了一些麻烦。值得注意的是,我正在使用 AJV。请在下面查看我的代码
{
"$ref": "#/definitions/Welcome",
"definitions": {
"Welcome": {
"properties": {
"auth": {
"type": "string",
"enum": ["oauth1","oauth2"]
},
"environment": {
"$ref": "#/definitions/Environment"
}
}
},
"Environment": {
"properties": {
"dev": {
"type": "object"
}
}
},
"Oauth1": {
"type": "object",
"properties": {
"temporary_credentials": {
"type": "string"
}
}
},
"Oauth2": {
"type": "object",
"properties": {
"auth_url": {
"type": "string"
}
}
}
},
"allOf": [
{
"if": {
"auth": {
"const": "oauth1"
}
},
"then": {
"environment": {
"dev": {
"$ref": "#/definitions/Oauth1
}
}
}
},
{
"if": {
"auth": {
"const": "oauth2"
}
},
"then": {
"environment": {
"dev": {
"$ref": "#/definitions/Oauth2
}
}
}
}
]
}
要针对 this 架构进行验证的示例 json 输入将是这样的
{
"auth": "oauth1",
"environment": {
"dev": {
"temporary_credentials": "xyzzy"
}
}
}
我觉得我的“then”语句或 allOf 的放置可能有错误。我会得到的错误类似于“$ref:路径“#”的架构中忽略的关键字。
【问题讨论】:
-
@gregsdennis mine 正在使用带有 "$ref" 的 "if" 和 "then" 感谢您的意见
-
使用
$ref或内联模式都没有关系;逻辑(和解决方案)是相同的。不要同时使用*Of和if/then/else。 -
此外,
then块内的架构需要一些properties标注。 -
@gregsdennis 为什么不能 allOf 和 if/then/else 一起使用?我在我的模式中使用它;当我有一个 if/thens 列表时,我将它包装在一个 allOf 中,并且从来没有遇到过问题。
标签: json schema jsonschema json-schema-validator