You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

新手求助:如何为JSON Schema中的granted与denied数组配置枚举约束

How to Add Enum Constraints to JSON Schema Arrays for Your Complex JSON Structure

Hey there! Manual JSON Schema tweaks can feel a bit tricky, especially when you need to lock down array values to specific options. Let's walk through creating a schema for your JSON that includes the enum constraints you need for the granted and denied arrays.

Here's the complete, adjusted JSON Schema:

{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "type": "object",
  "properties": {
    "iD": {
      "type": "integer"
    },
    "balance": {
      "type": "object",
      "properties": {
        "currency": {
          "type": "string"
        },
        "balance": {
          "type": "number"
        },
        "date": {
          "type": "string",
          "format": "date"
        }
      },
      "required": ["currency", "balance", "date"]
    },
    "Permissions": {
      "type": "object",
      "properties": {
        "granted": {
          "type": "array",
          "items": {
            "type": "string",
            "enum": ["canLoginWithPassword", "canGame", "canWithdraw", "canDeposit"]
          }
        },
        "failedConditions": {
          "type": "array",
          "items": {
            "type": "object",
            "properties": {
              "name": {
                "type": "string"
              },
              "description": {
                "type": "string"
              },
              "details": {
                "type": "object",
                "properties": {
                  "reason": {
                    "type": "string"
                  }
                },
                "required": ["reason"]
              },
              "denied": {
                "type": "array",
                "items": {
                  "type": "string",
                  "enum": ["canLoginWithPassword", "canGame", "canWithdraw", "canDeposit"]
                }
              }
            },
            "required": ["name", "description", "details", "denied"]
          }
        }
      },
      "required": ["granted", "failedConditions"]
    }
  },
  "required": ["iD", "balance", "Permissions"]
}

Key parts to note for your enum constraints:

  • For the granted array:
    We define it as an array with "type": "array", then use the items keyword to specify that every element in the array must be a string matching one of your allowed permissions. The enum list includes all four possible values you mentioned, so any element in granted has to be one of these (you can have one, multiple, or even an empty array by default).

  • For the denied array inside failedConditions:
    The exact same logic applies here. We nest the enum constraint under the items property of the denied array, ensuring only your specified permissions are allowed here too.

Extra touches I added:

  • Marked required fields with the required keyword (you can adjust this if some fields are optional)
  • Added type constraints for all fields (like iD as integer, date as ISO date string) to make the schema more robust
  • Used draft-07 (a widely supported version of JSON Schema)

If you need to enforce a minimum number of items in the arrays (e.g., at least one permission), you can add "minItems": 1 to the array's schema. For example:

"granted": {
  "type": "array",
  "minItems": 1,
  "items": {
    "type": "string",
    "enum": ["canLoginWithPassword", "canGame", "canWithdraw", "canDeposit"]
  }
}

内容的提问来源于stack exchange,提问作者Abiodun Adeoye

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.28 17:47:27