json - 需要 JSON Schema 条件,具体取决于值
问题描述
我的 JSON 验证有问题。我的页面中有 3 个链接的选择框,我需要验证以反映 UI 显示的内容。3 个选择是: - 范围:可以是ScopeNational
、ScopeRegional
或ScopeInternational
- 国家:国家列表 - 地区:地区列表(“欧洲”、“亚洲”等)
在模式中,选择是具有两个属性的对象:“key”和“text”,两者都是字符串。
如果范围是“ScopeNational”,则需要“Country”和“Region”。如果范围是“ScopeRegional”,则只需要“Region”。最后,如果范围是“ScopeInternational”,则不需要“Country”或“Region”。
我尝试了很多配置anyOf
,但我无法实现这是我尝试的最后一个模式,但没有成功oneOf
:if-then-else
{
"$schema": "http://json-schema.org/draft-07/schema#",
"$id": "http://example.com/root.json",
"type": "object",
"title": "Linked scope",
"default": null,
"properties": {
"scope": {
"$id": "#/properties/scope",
"title": "Project scope",
"$ref": "#/definitions/Scope"
},
"country": {
"$id": "#/properties/country",
"title": "Country",
"$ref": "#/definitions/Choice"
},
"region": {
"$id": "#/properties/region",
"title": "Region",
"$ref": "#/definitions/Choice"
}
},
"oneOf": [
{
"properties": {
"scope": {
"properties": {
"key": {
"const": "ScopeNational"
}
}
},
"country": {
"required": [
"key",
"text"
]
},
"region": {
"required": [
"key",
"text"
]
}
}
},
{
"properties": {
"scope": {
"properties": {
"key": {
"const": "ScopeRegional"
}
}
},
"region": {
"required": [
"key",
"text"
]
}
}
},
{
"properties": {
"scope": {
"properties": {
"key": {
"const": "ScopeInternational"
}
}
}
}
}
],
"required": [
"scope"
],
"definitions": {
"Choice": {
"type": "object",
"properties": {
"key": {
"type": "string"
},
"text": {
"type": "string"
}
},
"required": [
"key",
"text"
]
},
"Scope": {
"type": "object",
"properties": {
"key": {
"type": "string",
"enum": [
"ScopeNational",
"ScopeRegional",
"ScopeInternational"
]
},
"text": {
"type": "string"
}
},
"required": [
"key",
"text"
]
}
}
}
谢谢 !
解决方案
我稍微修改了您的架构,如下所示。
{
"$schema": "http://json-schema.org/draft-07/schema#",
"$id": "http://example.com/root.json",
"type": "object",
"title": "Linked scope",
"properties": {
"scope": {
"$id": "#/properties/scope",
"title": "Project scope",
"$ref": "#/definitions/Scope"
}
},
"oneOf": [
{
"properties": {
"scope": {
"properties": {
"key": {
"const": "ScopeNational"
}
}
},
"region": {
"$ref": "#/definitions/Choice"
},
"country": {
"$ref": "#/definitions/Choice"
}
}
},
{
"properties": {
"scope": {
"properties": {
"key": {
"const": "ScopeRegional"
}
}
},
"region": {
"$ref": "#/definitions/Choice"
},
"country": {
"$ref": "#/definitions/NullableChoice"
}
}
},
{
"properties": {
"scope": {
"properties": {
"key": {
"const": "ScopeInternational"
}
}
},
"region": {
"$ref": "#/definitions/NullableChoice"
},
"country": {
"$ref": "#/definitions/NullableChoice"
}
}
}
],
"required": [
"scope",
"country",
"region"
],
"definitions": {
"Choice": {
"type": "object",
"properties": {
"key": {
"type": "string"
},
"text": {
"type": "string"
}
},
"required": [
"key",
"text"
]
},
"NullableChoice": {
"type": "object",
"properties": {
"key": {
"type": ["string", "null"]
},
"text": {
"type": ["string", "null"]
}
},
"required": [
"key",
"text"
]
},
"Scope": {
"type": "object",
"properties": {
"key": {
"type": "string",
"enum": [
"ScopeNational",
"ScopeRegional",
"ScopeInternational"
]
},
"text": {
"type": "string"
}
},
"required": [
"key",
"text"
]
}
}
}
现在所有属性都是必需的,并且新定义NullableChoice
已添加到模式中。请注意,这是null
一种类似于JSON Schema 的类型。string
number
推荐阅读
- digital-ocean - 重定向错误的 URL/路径 DigitalOcean Spaces
- git - 带有 GitFlow 分支的 Azure DevOps 构建和发布管道
- java - metaData.getPrimaryKeys() 在 key 为单个时返回四个相同的记录
- angular - 在 URL 中使用变量时出现 404 错误:模型 [App\Models\Seeker] 没有查询结果
- dll - 从命令行运行 DLL 文件以弹出消息框
- sql - 如何在访问文件中查找丢失的数字
- python - 获取索引值的有效方法
- c# - 读取 wvsx 文件的字符编码问题
- angular - 在文件顶部导入角度模块和在函数参数中导入它有什么区别
- xml - 如何使用不同的模板进行 InDesign XML 导入