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

JSON Schema条件属性验证失效问题排查与修复求助

问题分析与修复

你的错误主要来自两个不必要的冗余逻辑,导致约束未被正确触发:

  1. 没必要把if/then逻辑包裹在allOf数组中,这会增加验证器的处理复杂度,甚至导致规则未被正确合并。
  2. then部分重复定义了userPaths的type和items规则,虽然本身不报错,但冗余的写法干扰了验证器对核心约束(minItems:1)的应用。

当你将userPaths设为空数组时,原schema未生效的核心原因是:包裹在allOf中的then规则未被验证器正确合并到主约束逻辑中。

修复后的Schema

{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "type": "object",
  "properties": {
    "notificationType": {
      "type": "string"
    },
    "userPaths": {
      "type": "array",
      "items": {
        "type": "string"
      }
    },
    "id": {
      "type": "string"
    }
  },
  "required": ["notificationType", "userPaths", "id"],
  "if": {
    "properties": {
      "notificationType": {
        "const": "Directed"
      }
    }
  },
  "then": {
    "properties": {
      "userPaths": {
        "minItems": 1
      }
    }
  }
}

关键修改点

  • 移除冗余的allOf包裹,直接在schema顶级使用if/then逻辑,符合draft-07规范,确保验证器能正确识别条件约束。
  • then部分仅保留minItems:1的核心约束,复用主schema中已定义的userPaths类型和元素规则,避免冗余且确保约束生效。

修改后,当notificationType为Directed时,userPaths为空数组会触发验证失败,完全符合你的需求。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.23 07:23:22