简体   繁体   English

验证 json 架构的嵌套内容

[英]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:通过场景:

  1. A data entry 'human' will be accepted, since it is one of the key:value pair (human: Homo sapiens) of category 'primates'.将接受数据条目“人类”,因为它是“灵长类动物”类别的键:值对(人类:智人)之一。
  1. A data entry 'chimpanzee' will also be accepted.数据输入“黑猩猩”也将被接受。 It is also one of the key:value pair (chimpanzee: pan troglodytes) of category 'primates'.它也是“灵长类动物”类别的键:值对(黑猩猩:pan troglodytes)之一。
  2. A data entry 'salmon' will be accepted.将接受数据输入“鲑鱼”。 Its key:value pair (Salmon: Salmo salar Linnaeus) is located in another category 'fish'.它的键:值对(Salmon: Salmo salar Linnaeus)位于另一个类别“鱼”中。

Fail scenarios:失败场景:

  1. Any data entry that are not listed in the animal kingdom dictionary will not be accepted eg Pear, Oranges, Table, Chairs…任何未在动物王国词典中列出的数据条目将不被接受,例如梨、橙子、桌子、椅子……

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM