JSON 架构条件验证配置 - 不支持的关键字:[ "const" ]]



我想在我的模式中设置条件验证。我在SO上看到了一个例子。我有一个类似的设置,我想验证字段public是否设置为字符串";公开";。如果它被设置为"0";"公共";那么我想使字段描述附件URL标签成为必需。如果字段没有设置为"0";"公共";则不需要这些字段。

{
"$schema": "http://json-schema.org/draft-04/schema#",
"title": "Update todo",
"type": "object",
"properties": {
"public": {
"type": "string"
},
"description": {
"type": "string",
"minLength": 3
},
"tags": {
"type": "array",
"items": {
"type": "string"
},
"uniqueItems": true,
"minItems": 1
},
"attachmentUrl": {
"type": "string"
}
},
"anyOf": [
{
"not": {
"properties": {
"public": { "const": "public" }
},
"required": ["public"]
}
},
{ "required": ["description", "tags", "attachmentUrl"] }
],
"additionalProperties": false
}

但是,当我尝试这样部署它时,我会得到以下错误:

指定的模型无效:验证结果:警告:[],错误:[指定的模型架构无效。不支持的关键字:[quot;const"]]

;const";关键字直到06年草案才被添加。您应该升级到至少支持该版本的实现。

https://json-schema.org/draft-06/json-schema-release-notes.html#additions-和向后兼容的更改

否则,您可以使用";enum";具有单个值:"enum": ["public"]

最新更新