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

JSON Schema条件必填:未传vehicleType仍提示topSpeed必填排查

问题根因

你当前的if条件存在逻辑漏洞:JSON Schema 校验properties下的const规则时,默认不会要求被校验的字段必须存在。当请求中未传入vehicleType字段时,不存在与const: "Car"冲突的取值,校验器会判定if条件成立,直接触发then块中topSpeed必填的规则,最终抛出不符合预期的校验错误。

修复方法

在if条件块中显式增加vehicleType字段的必填要求,确保仅当vehicleType字段存在、且取值严格为Car时,才触发topSpeed的必填校验。
修复后的完整Schema片段如下:

"vehicle": {
    "type": "object",
    "properties": {
      "valuation": {
        "type": "number"
      },
      "vehicleType": {
        "type": "string",
        "enum": ["Car", "Airplane", "Boat", "Motorcycle", "Truck"]
      },
      "topSpeed": {
        "type": "number"
      }
    },
    "if": {
      "properties": {
        "vehicleType": {
          "const": "Car"
        }
      },
      "required": ["vehicleType"]
    },
    "then": {
      "required": ["topSpeed"]
    }
  }
规则匹配验证

修复后的逻辑完全覆盖需求场景:

  • 未传入vehicleType字段:if条件不成立,topSpeed非必填
  • vehicleType取值为非Car的合法枚举值:if条件不成立,topSpeed非必填
  • vehicleType存在且取值为Car:if条件成立,强制要求topSpeed字段传入

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.01 04:55:19