有没有办法在基于相同级别 属性 的 jsonschema 中创建枚举?

Is there a way to create an enum in jsonschema based on a same level property?

描述

我有一个 JSON 对象,它有两个属性(除其他外),即 categorysubcategory,我有一个函数可以获取类别列表,另一个函数可以获取给定类别的子类别。

所以我的架构看起来像这样:

{
  "type": "array",
  "items": {
    "type": "obect",
    "properties": {
      "category": {
        "type": "string",
        "enum": getCategories()
      },
      "subcategory": {
        "type": "string",
        "enum": getSubcategories(category)
      }
  }
}

Note: One subcategory is in only one main category

问题

类别列表很大,我想用 json 架构检查类别和子类别是否有效,所以我想知道是否有办法这样做。

Using npm package for node js

示例:

假设我的类别 A 和 B 具有 [A1,A2][B1,B2] ,如果要验证的 json 具有类别 A 我想检查子类别是否在 [A1,A2] 不在 [A1,A2,B1,B2]

我试过的

尝试了另一个 中的 if-else 语句,但正如我提到的那样,列表很大,看起来根本不是一个好的做法

目前,需要 if/then 构造来指定基于类别的子类别值:

{
  "type": "array",
  "items": {
    "type": "object",
    "properties": {
      "category": {
        "type": "string",
        "enum": [ ... ]
      },
      "subcategory": {
        "type": "string",
      },
    },
    "allOf": [
      {
        "if": {
          "properties": {
            "category": { "const": "option1" },
          }
        },
        "then": {
          "properties": {
            "subcategory": {
              "enum": [ ... ]
            }
          }
        }
      },
      {
        "if": {
          "properties": {
            "category": { "const": "option2" },
          }
        },
        "then": {
          "properties": {
            "subcategory": {
              "enum": [ ... ]
            }
          }
        }
      },
      { ... }
    ] 
  }
}

但是,有人提议为下一个规范草案添加一个新关键字,这将大大缩短语法:https://github.com/json-schema-org/json-schema-spec/issues/1082