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

JSON Schema Draft-04多条件校验问题求助

正确的JSON Schema Draft-04 实现指定校验规则

校验规则明确

先再次梳理需实现的校验逻辑,避免歧义:

  • assignments对象中,chapters为可选字段
  • type是必填字段,仅允许multi/notes两个枚举值
  • 当type为multi时:
    • 若父级assignments对象无pointer字段,那么chapters数组的每个元素必须包含pointer字段
    • 若父级assignments对象有pointer字段,chapters元素对pointer无强制要求
  • 当type为notes时:
    • pointer为必填字段,除非completed字段存在且值为true
    • completed为可选字段,仅在设为true时可省略pointer

常见错误原因

你之前的Schema问题集中在两点:

  1. notes类型逻辑颠倒:错误地要求completed: false时才校验pointer,正确逻辑应为「只要completed不是true(包括不存在completed),就必须有pointer」
  2. multi类型逻辑缺失:未正确判断父级pointer的存在性,进而无法强制chapters元素的pointer校验;同时因oneOf/anyOf逻辑重叠,导致多schema匹配报错

最终JSON Schema Draft-04

{
  "type": "object",
  "properties": {
    "assignments": {
      "type": "array",
      "items": {
        "type": "object",
        "required": ["type"],
        "properties": {
          "type": {
            "enum": ["multi", "notes"]
          },
          "chapters": {
            "type": "array",
            "items": {
              "type": "object"
            }
          },
          "pointer": {
            "type": "string" // 可根据实际数据类型调整,比如number
          },
          "completed": {
            "type": "boolean"
          }
        },
        "oneOf": [
          // 处理type=notes的场景
          {
            "properties": {
              "type": { "const": "notes" }
            },
            "anyOf": [
              // 场景1:completed为true,无需pointer
              {
                "properties": {
                  "completed": { "const": true }
                }
              },
              // 场景2:completed不存在或不为true,必须有pointer
              {
                "not": {
                  "properties": {
                    "completed": { "const": true }
                  },
                  "required": ["completed"]
                },
                "required": ["pointer"]
              }
            ]
          },
          // 处理type=multi的场景
          {
            "properties": {
              "type": { "const": "multi" }
            },
            "anyOf": [
              // 场景1:父级有pointer,chapters无强制要求
              { "required": ["pointer"] },
              // 场景2:父级无pointer,若存在chapters则每个元素必须有pointer
              {
                "not": { "required": ["pointer"] },
                "properties": {
                  "chapters": {
                    "type": "array",
                    "items": {
                      "type": "object",
                      "required": ["pointer"]
                    }
                  }
                },
                "dependencies": {
                  "chapters": ["chapters"]
                }
              }
            ]
          }
        ]
      }
    }
  }
}

关键逻辑说明

  1. oneOf分类型隔离:确保每个assignment仅匹配notes或multi的规则,彻底解决「多schema匹配」的报错(对应示例2、5的问题)
  2. notes类型双分支校验:
    • 明确区分「completed=true」和「其他情况」,覆盖completed不存在的场景,强制要求pointer(解决示例3的漏判问题)
  3. multi类型双分支校验:
    • 根据父级是否存在pointer,决定对chapters元素的pointer校验规则,覆盖「父级无pointer且chapters缺pointer」的场景(解决示例4、7的漏判问题)

测试用例匹配结果

对应你的7个测试示例,该Schema会得到预期结果:

  1. 示例1:预期通过 → 正常通过
  2. 示例2:预期通过 → 无枚举错误和多schema匹配错误,正常通过
  3. 示例3:预期因缺少pointer失败 → 校验失败
  4. 示例4:预期因父级无pointer且chapters缺pointer失败 → 校验失败
  5. 示例5:预期通过 → 无枚举错误和pointer缺失错误,正常通过
  6. 示例6:预期通过 → 正常通过
  7. 示例7:预期因部分chapters缺pointer且父级无pointer失败 → 校验失败

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.15 20:13:15