我的JSON验证出现问题。我的页面上有3个链接的选择框,我需要进行验证以反映UI所显示的内容。
3个选择是:
-范围:可以是ScopeRegional
,ScopeInternational
或anyOf
-国家:国家列表
-地区:地区列表(“欧洲”,“亚洲”等)
在模式中,选择是具有两个属性的对象:“键”和“文本”,都是字符串。
如果范围是“ ScopeNational”,则需要“国家”和“地区”。如果scope为“ ScopeRegional”,则仅需要“ Region”。最后,如果scope为“ ScopeInternational”,则不需要“ Country”或“ Region”。
我尝试使用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"
]
}
}
}
进行许多配置,但是我无法实现
这是我尝试的最后一个模式,但没有成功:
{{1}}
谢谢!
答案 0 :(得分:0)
我对您的架构进行了如下修改。
{
"$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模式中的string
和number
之类的类型。