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

JSON Schema Draft-4条件必填字段配置异常求助

JSON Schema Draft-4 条件必填字段配置问题

需求规则:

  • 无论env字段取何值,label1、label2、label3均为必填
  • 当env取值为sit或uat时,额外要求date字段必填

第一版配置

"date": {
      "type": [
        "string"
      ]
    }
  },
  "anyOf": [
    {
      "not": {
        "properties": { "env": { "enum": ["sit", "uat"] } },
        "required": ["label1","label2","label3"]
        }
    },
    { "required": ["date"] }
  ],

第二版配置

"labels": {
  "additionalProperties": {
    "type": [
      "string",
      "null"
    ]
  },
  "properties": {
    "label1": {
      "type": [
        "string",
        "number"
      ]
    },
    "label2": {
      "type": [
        "string"
      ]
    },
    "label3": {
      "type": "string"
    },
    "date": {
      "type": "string"
    }
  },
  "required": [
    "label1",
    "label2",
    "label3"
  ],
  "oneOf": [
    {
      "required": [
        "date"
      ],
      "type": "object",
      "properties": {
        "env": {
          "type": "string",
          "enum": [
            "sit",
            "uat"
          ]
        }
      }
    },
    {
      "not": {
        "required": [
          "date"
        ]
      },
      "type": "object",
      "properties": {
        "env": {
          "type": "string",
          "enum": [
            "dev",
            "prod"
          ]
        }
      }
    }
  ],

测试结果与预期不符

  • 测试1:env为sit/uat且传入date,实际与预期均通过
  • 测试2:env为sit/uat但未传入date,实际通过,预期失败
  • 测试3:env为dev/prod且未传入date,实际与预期均通过

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.05 04:48:29