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

JSON Schema验证报错求助:请求解释错误原因

JSON Schema验证错误解析及修正后代码说明

嘿,我知道你在验证这段JSON Schema时碰到了看不懂的错误,先给你看已经修正了拼写错误(把原代码里的"mini"改成了正确的"minimum")后的完整Schema:

{
  "$schema": "http://json-schema.org/draft-04/schema#",
  "description": "Modified JSON Schema draft v4 that includes the optional '$ref' and 'format'",
  "definitions": {
    "schemaArray": {
      "type": "array",
      "minItems": 1,
      "items": { "$ref": "#" }
    },
    "positiveInteger": {
      "type": "integer",
      "minimum": 0
    },
    "positiveIntegerDefault0": {
      "allOf": [
        { "$ref": "#/definitions/positiveInteger" },
        { "default": 0 }
      ]
    },
    "simpleTypes": {
      "enum": [ "array", "boolean", "integer", "null", "number", "object", "string" ]
    },
    "stringArray": {
      "type": "array",
      "items": { "type": "string" },
      "minItems": 1,
      "uniqueItems": true
    }
  },
  "type": "object",
  "properties": {
    "id": { "type": "string", "format": "uri" },
    "$schema": { "type": "string", "format": "uri" },
    "$ref": { "type": "string" },
    "format": { "type": "string" },
    "title": { "type": "string" },
    "description": { "type": "string" },
    "default": { },
    "multipleOf": { "type": "number", "minimum": 0, "exclusiveMinimum": true },
    "maximum": { "type": "number" },
    "exclusiveMaximum": { "type": "boolean", "default": false },
    "minimum": { "type": "number" },
    "exclusiveMinimum": { "type": "boolean", "default": false },
    "maxLength": { "$ref": "#/definitions/positiveInteger" },
    "minLength": { "$ref": "#/definitions/positiveIntegerDefault0" },
    "pattern": { "type": "string", "format": "regex" },
    "additionalItems": { "anyOf": [ { "type": "boolean" }, { "$ref": "#" } ], "default": { } },
    "items": { "anyOf": [ { "$ref": "#" }, { "$ref": "#/definitions/schemaArray" } ], "default": { } },
    "maxItems": { "$ref": "#/definitions/positiveInteger" },
    "minItems": { "$ref": "#/definitions/positiveIntegerDefault0" },
    "uniqueItems": { "type": "boolean", "default": false },
    "maxProperties": { "$ref": "#/definitions/positiveInteger" },
    "minProperties": { "$ref": "#/definitions/positiveIntegerDefault0" },
    "required": { "$ref": "#/definitions/stringArray" },
    "additionalProperties": { "anyOf": [ { "type": "boolean" }, { "$ref": "#" } ], "default": { } },
    "definitions": { "type": "object", "additionalProperties": { "$ref": "#" }, "default": { } },
    "properties": { "type": "object", "additionalProperties": { "$ref": "#" }, "default": { } },
    "patternProperties": { "type": "object", "additionalProperties": { "$ref": "#" }, "default": { } },
    "dependencies": { "type": "object", "additionalProperties": { "anyOf": [ { "$ref": "#" }, { "$ref": "#/definitions/stringArray" } ] } },
    "enum": { "type": "array", "minItems": 1, "uniqueItems": true },
    "type": { "anyOf": [ { "$ref": "#/definitions/simpleTypes" }, { "type": "array", "items": { "$ref": "#/definitions/simpleTypes" }, "minItems": 1, "uniqueItems": true } ] },
    "allOf": { "$ref": "#/definitions/schemaArray" },
    "anyOf": { "$ref": "#/definitions/schemaArray" },
    "oneOf": { "$ref": "#/definitions/schemaArray" },
    "not": { "$ref": "#" }
  },
  "dependencies": {
    "exclusiveMaximum": [ "maximum" ],
    "exclusiveMinimum": [ "minimum" ]
  }
}

接下来,我给你梳理这段Schema可能触发的常见错误及其含义:

  • 循环引用警告/错误:这段Schema大量使用$ref: "#"来引用自身(比如items、additionalItems字段),虽然JSON Schema draft-04允许这种递归定义,但部分验证工具对深度递归的支持有限,可能会抛出"检测到循环引用"或"无法解析递归Schema结构"的提示。
  • format关键字兼容性问题:Schema里用到了format: "regex"和format: "uri",但draft-04规范中format是可选的语义验证项,并非所有工具都支持所有format值。如果你的验证工具不支持regex格式,会出现"未知的format类型"或"format验证未实现"的错误。
  • 无类型的default值:多个字段(比如顶级的default、additionalItems的默认值)设置为{},这表示一个空对象,但有些严格的验证工具会要求default值必须明确指定类型,可能会提示"default值未定义类型"。
  • 依赖项未满足错误:Schema定义了dependencies规则,要求exclusiveMaximum必须和maximum同时出现,exclusiveMinimum必须和minimum同时出现。如果待验证的Schema里只写了exclusiveMaximum却没加maximum,就会触发"依赖字段缺失"的错误,提示需要同时提供对应的maximum或minimum。

要是你能给出具体的错误信息,我可以更精准地帮你定位问题。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.27 07:28:19