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

无法校验递归JSON Schema:文件类型校验结果不符合预期求助

JSON Schema 问题排查与修复

问题根因

  • 字符串类型file校验失败:原Schema中directory的patternProperties递归引用了根Schema#,根Schema强制要求类型为object,字符串类型的file无法满足该规则,导致误判失败。
  • 同时存在$data与$stringData未校验失败:原file对象的oneOf互斥逻辑编写错误,且未声明允许携带$meta属性,导致规则匹配失效。

修复后Schema

{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://example.com/dir.schema.json",
  "type": "object",
  "patternProperties": {
    "^[^\\/?%*:|\"<>]+$": {
      "$ref": "#/$defs/directory"
    }
  },
  "additionalProperties": false,
  "$defs": {
    "file": {
      "oneOf": [
        {
          "type": "string"
        },
        {
          "type": "object",
          "properties": {
            "$meta": {
              "type": "object",
              "additionalProperties": true,
              "properties": {
                "createdAt": {
                  "type": "string"
                },
                "size": {
                  "type": "integer"
                }
              }
            },
            "$data": {
              "type": "string",
              "description": "base64 encoded data"
            },
            "$stringData": {
              "type": "string"
            }
          },
          "exclusiveRequired": ["$data", "$stringData"],
          "additionalProperties": false
        }
      ]
    },
    "directory": {
      "anyOf": [
        {
          "$ref": "#/$defs/file"
        },
        {
          "type": "object",
          "properties": {
            "$meta": {
              "type": "object",
              "additionalProperties": true,
              "properties": {
                "createdAt": {
                  "type": "string"
                },
                "size": {
                  "type": "integer"
                }
              }
            }
          },
          "patternProperties": {
            "^[^\\/?%*:|\"<>]+$": {
              "$ref": "#/$defs/directory"
            }
          },
          "additionalProperties": false
        }
      ]
    }
  }
}

验证效果

  • 测试数据中file1B.txt同时携带$data与$stringData,会正常触发校验失败
  • 测试数据中字符串类型的file2.txt、file1.txt都会正常校验通过

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.04 07:57:00