[英]validate nested content of json schema
I'm creating a schema consists of multiple categories.我正在创建一个包含多个类别的架构。 In each category there's an array of key:value pairs.在每个类别中都有一组键:值对。 Each key represents the display name of value.每个键代表值的显示名称。 Each value is unique and can only be assigned to a single key and category.每个值都是唯一的,并且只能分配给单个键和类别。
As an example, a category called 'primates' will have 'human' as one of the key/ display name, and the biological name 'Homo sapiens' will be the corresponding value of the key:value pair.例如,名为“灵长类动物”的类别将“人类”作为键/显示名称之一,而生物学名称“智人”将是键:值对的对应值。
I want to validate data entry so that data matches with only one of the key/ display name.我想验证数据输入,以便数据仅与键/显示名称之一匹配。 I put anyOf
for the categories, does it do the same job?我把anyOf
放在类别中,它做同样的工作吗? Is this how you will arrange items in the schema?这是您在架构中排列项目的方式吗?
{
"$schema": "https://example.com/schema/dictionary",
"$id": "https://example.com/schemaoutput/dictionary",
"description": "A schema that validates the minimum requirements for validation output",
"type": "array",
"items": {
"additionalProperties": false,
"properties": {
"subcat1": {
"type": "string",
"title": "category1",
"tag": [
{
"display_labelA": [
"class_A"
],
"display_labelB": [
"class_B"
]
}
]
},
"subcat2": {
"type": "string",
"title": "category2",
"tag": [
{
"display_labelC": [
"class_C"
],
"display_labelD": [
"class_D"
]
}
]
}
},
"anyOf": [
{
"required": [
"subcat1",
"subcat2"
]
}
]
}
}
Edit:编辑:
As requested I updated the post with expected pass and fail scenarios.根据要求,我用预期的通过和失败场景更新了帖子。 For example I want to create a json schema containing different categories of animals in the animal kingdom.例如,我想创建一个 json 模式,其中包含动物王国中不同类别的动物。 Each key:value pair refer to the commonly known animal name and the corresponding scientific name.每个键:值对指的是众所周知的动物名称和相应的学名。 Only data entries of animals' commonly known names will be accepted.仅接受动物众所周知的名称的数据条目。
Pass scenarios:通过场景:
Fail scenarios:失败场景:
Looking through 1 and other posts I think enum
is what I need to map key:value pairs (ie biological name:commonly known name) under each category (primates or fish).浏览1和其他帖子,我认为enum
是每个类别(灵长类动物或鱼类)下的 map 键:值对(即生物学名称:俗名)所需要的。 I'm using anyOf
at the end of the JSON schema to validate data entry that contain any values specified under any category.我在 JSON 架构的末尾使用anyOf
来验证包含在任何类别下指定的任何值的数据条目。
{
"$schema": "https://example.com/schema/dictionary",
"$id": "https://example.com/schemaoutput/dictionary",
"description": "A schema that validates the minimum requirements for validation output",
"type": "array",
"items": {
"additionalProperties": false,
"properties": {
"subcat1": {
"type": "string",
"title": "category1",
"enum": [{
"display_labelA": "class_A",
"display_labelB": "class_B"
}]
},
"subcat2": {
"type": "string",
"title": "category2",
"enum": [{
"display_labelA": "class_A",
"display_labelB": "class_B"
}]
}
},
"anyOf": [{
"required": [
"subcat1",
"subcat2"
]
}]
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.