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

如何定义验证JSON Schema属性的JSON Schema?编译失败求助

如何定义JSON Schema验证包含Schema属性的输入?

我正尝试定义一个JSON Schema,用于验证包含JSON Schema属性的输入。

现有Schema与测试文件

schema.json

{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "http://example.com/schema-validation",
  "type": "object",
  "properties": {
    "someValue": {
      "type": "boolean"
    },
    "metaSchemaProperties": {
      "$ref": "https://json-schema.org/draft/2020-12/schema#/properties"
    }
  }
}

file.json

{
  "someValue": true,
  "metaSchemaProperties": {
    "portRange": {
      "type": "string"
    }
  }
}

我还尝试使用https://json-schema.org/draft/2020-12/meta/core#/properties作为$ref值,但均未成功。

编译错误信息

使用jv CLI编译时失败,报错如下:

$ jv schema.json task.json

jsonschema file:///tmp/schema.json compilation failed
[I#/properties] [S#] doesn't validate with https://json-schema.org/draft/2020-12/schema#
  [I#/properties/$recursiveRef] [S#/properties/$recursiveRef/$ref] doesn't validate with 'https://json-schema.org/draft/2020-12/meta/core#/$defs/uriReferenceString'
    [I#/properties/$recursiveRef] [S#/$defs/uriReferenceString/type] expected string, but got object
  [I#/properties/definitions] [S#/properties/definitions]
    [I#/properties/definitions/$comment] [S#/properties/definitions/additionalProperties/$dynamicRef] doesn't validate with ''
      [I#/properties/definitions/$comment] [S#/type] expected object or boolean, but got string
    [I#/properties/definitions/type] [S#/properties/definitions/additionalProperties/$dynamicRef] doesn't validate with ''
      [I#/properties/definitions/type] [S#/type] expected object or boolean, but got string
  [I#/properties/dependencies] [S#/properties/dependencies]
    [I#/properties/dependencies/$comment] [S#/properties/dependencies/additionalProperties/anyOf] anyOf failed
      [I#/properties/dependencies/$comment] [S#/properties/dependencies/additionalProperties/anyOf/0/$dynamicRef] doesn't validate with ''
        [I#/properties/dependencies/$comment] [S#/type] expected object or boolean, but got string
      [I#/properties/dependencies/$comment] [S#/properties/dependencies/additionalProperties/anyOf/1/$ref] doesn't validate with 'https://json-schema.org/draft/2020-12/meta/validation#/$defs/stringArray'
        [I#/properties/dependencies/$comment] [S#/$defs/stringArray/type] expected array, but got string
    [I#/properties/dependencies/type] [S#/properties/dependencies/additionalProperties/anyOf] anyOf failed
      [I#/properties/dependencies/type] [S#/properties/dependencies/additionalProperties/anyOf/0/$dynamicRef] doesn't validate with ''
        [I#/properties/dependencies/type] [S#/type] expected object or boolean, but got string
      [I#/properties/dependencies/type] [S#/properties/dependencies/additionalProperties/anyOf/1/$ref] doesn't validate with 'https://json-schema.org/draft/2020-12/meta/validation#/$defs/stringArray'
        [I#/properties/dependencies/type] [S#/$defs/stringArray/type] expected array, but got string
  [I#/properties/$recursiveAnchor] [S#/properties/$recursiveAnchor/$ref] doesn't validate with 'https://json-schema.org/draft/2020-12/meta/core#/$defs/anchorString'
    [I#/properties/$recursiveAnchor] [S#/$defs/anchorString/type] expected string, but got object

解决方案

问题出在你直接引用了JSON Schema自身的#/properties节点——这个节点描述的是JSON Schema文档中properties字段的结构规则,而非用来验证某个对象的属性值是合法的JSON Schema。

要实现验证metaSchemaProperties中的每个属性都是合法的JSON Schema,你需要定义metaSchemaProperties为一个对象,并指定其所有额外属性都必须符合JSON Schema规范:

{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "http://example.com/schema-validation",
  "type": "object",
  "properties": {
    "someValue": {
      "type": "boolean"
    },
    "metaSchemaProperties": {
      "type": "object",
      "additionalProperties": {
        "$ref": "https://json-schema.org/draft/2020-12/schema"
      }
    }
  }
}

这个修改后的Schema会确保:

  • metaSchemaProperties是一个对象
  • 该对象下的任意属性(比如portRange)的值都必须是合法的JSON Schema

这样你的file.json就能通过验证,同时也符合你想要的“仅验证properties”的需求。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.29 09:44:57