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

Python中JSON Schema能否实现嵌套引用?

JSON Schema嵌套引用实现方案

可以通过JSON Schema的$ref关键字实现嵌套引用,只要遵循规范调整schema结构并修正原代码中的语法错误,就能保证验证功能正常运行。

先修正原代码中的核心错误

原schema的类型定义不符合JSON Schema规范,比如'Text': 'string'这种写法是错误的,必须写成'Text': {'type': 'string'};同时对象类型的字段需要明确声明'type': 'object',否则验证器会判定schema无效。

方案一:直接引用schema_two中的Params片段

调整schema结构后,使用JSON Pointer指向schema_two内的Params定义,配合RefResolver让验证器识别引用关系:

from jsonschema import validate, RefResolver

class Schemas:
    # 修正后的schema_two
    schema_two = {
        '$schema': 'https://json-schema.org/draft/2020-12/schema',
        'title': 'schema_two',
        'type': 'object',
        'properties': {
            'Params': {
                'type': 'object',
                'properties': {
                    'Text': {'type': 'string'},
                    'Count': {'type': 'integer'}
                },
                'additionalProperties': False,
                'minProperties': 2
            }
        }
    }

    # 替换Params为对schema_two的引用
    schema_first = {
        '$schema': 'https://json-schema.org/draft/2020-12/schema',
        'title': 'schema_one',
        'type': 'object',
        'properties': {
            # 通过JSON Pointer引用schema_two的Params
            'Params': {'$ref': '#/properties/Params'},
            'User': {
                'type': 'object',
                'properties': {
                    'Name': {'type': 'string'},
                    'Id': {'type': 'integer'}
                },
                'additionalProperties': False,
                'minProperties': 2
            }
        },
        'additionalProperties': False,
        'minProperties': 1
    }

# 验证示例
valid_data = {
    'Params': {'Text': 'test', 'Count': 5},
    'User': {'Name': 'Alice', 'Id': 123}
}

# 关联两个schema并执行验证
resolver = RefResolver.from_schema(Schemas.schema_two)
validate(instance=valid_data, schema=Schemas.schema_first, resolver=resolver)

方案二:用$defs定义可复用片段(推荐)

将schema_two改为专门存放可复用结构的模块,使用draft 2020-12规范的$defs字段存储Params的schema,引用更清晰且符合模块化设计:

from jsonschema import validate, RefResolver

class Schemas:
    # 存放可复用的schema片段
    schema_two = {
        '$schema': 'https://json-schema.org/draft/2020-12/schema',
        'title': 'schema_two',
        '$defs': {
            'ParamsSchema': {
                'type': 'object',
                'properties': {
                    'Text': {'type': 'string'},
                    'Count': {'type': 'integer'}
                },
                'additionalProperties': False,
                'minProperties': 2
            }
        }
    }

    # 引用schema_two中定义的ParamsSchema
    schema_first = {
        '$schema': 'https://json-schema.org/draft/2020-12/schema',
        'title': 'schema_one',
        'type': 'object',
        'properties': {
            'Params': {'$ref': '#/$defs/ParamsSchema'},
            'User': {
                'type': 'object',
                'properties': {
                    'Name': {'type': 'string'},
                    'Id': {'type': 'integer'}
                },
                'additionalProperties': False,
                'minProperties': 2
            }
        },
        'additionalProperties': False,
        'minProperties': 1
    }

# 验证示例
valid_data = {
    'Params': {'Text': 'test', 'Count': 5},
    'User': {'Name': 'Alice', 'Id': 123}
}

resolver = RefResolver.from_schema(Schemas.schema_two)
validate(instance=valid_data, schema=Schemas.schema_first, resolver=resolver)

关键注意事项

  • 所有类型定义必须符合JSON Schema规范,对象类型需明确声明'type': 'object',基础类型(字符串、整数等)需用{'type': 'xxx'}的格式。
  • 使用$ref时,需要通过RefResolver关联被引用的schema,确保验证器能解析引用路径。
  • 如果是跨文件引用,只需调整$ref的路径为文件的相对或绝对路径即可,本地类内的schema用JSON Pointer引用即可。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.12 15:41:13