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

Python jsonschema库验证含definitions的JSON Schema时出现PointerToNowhere错误的求助

Python jsonschema库验证含definitions的JSON Schema时出现PointerToNowhere错误的求助

我在验证一个包含definitions的JSON Schema时遇到了问题,在线的Schema验证工具都能正常通过,但用Python的jsonschema库验证时却抛出了这个错误:

PointerToNowhere: '/definitions/ipv4_pattern' does not exist inside {}

难道jsonschema库不支持definitions吗?以下是我的代码:

import jsonschema

def main():
    str_json_yaml= {
                      "version": 1,
                      "group": "infrastructure",
                      "service": "monitoring",
                      "tech": "172.21.101.155"}

    str_json_schema = {
                      "$schema": "http://json-schema.org/draft-04/schema#",
                      "type": "object",
                      "properties": {
                        "version": {
                          "type": "integer"
                        },
                        "group": {
                          "type": "string"
                        },
                        "service": {
                          "type": "string"
                        },
                        "tech": { "$ref": "#/definitions/ipv4_pattern" }
                      },
                      "definitions": {
                            "ipv4_pattern": {"type": "string",
                                             "pattern": "^(?=\\d+\\.\\d+\\.\\d+\\.\\d+$)(?:(?:25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9][0-9]|[0-9])\\.?){4}$"
                                            }
                      }
                    }

    validator = jsonschema.Draft202012Validator({})
    obj_return = validator.evolve(schema=str_json_schema)

    lst_errors = []
    for error in obj_return.iter_errors(str_json_yaml):
        lst_errors.append('Item: ' + error.json_path + ' / Error: ' + error.message)
    if len(lst_errors) > 0:
        print(*lst_errors, sep='\n')
        return False
    else:
        return True

if __name__ == "__main__":
    print(main())

别担心,jsonschema库是完全支持definitions的,问题出在你的Validator初始化方式上!

你看代码里这两行:

validator = jsonschema.Draft202012Validator({})
obj_return = validator.evolve(schema=str_json_schema)

你先创建了一个基于空Schema的Draft202012Validator实例,再通过evolve方法替换Schema。这种方式不仅多余,还触发了两个关键问题:

  1. 版本不匹配:你的Schema声明的是Draft-04版本,但你用了最新的Draft202012Validator来初始化,不同Schema版本的引用解析逻辑存在兼容性差异
  2. 上下文未正确更新:先基于空Schema构建Validator的内部引用上下文,后续替换Schema后,这个上下文没有被正确重建,导致库找不到/definitions/ipv4_pattern

修正后的代码

直接用对应版本的Validator类加载你的Schema即可,不需要绕evolve的弯路:

import jsonschema

def main():
    str_json_yaml= {
                      "version": 1,
                      "group": "infrastructure",
                      "service": "monitoring",
                      "tech": "172.21.101.155"}

    str_json_schema = {
                      "$schema": "http://json-schema.org/draft-04/schema#",
                      "type": "object",
                      "properties": {
                        "version": {
                          "type": "integer"
                        },
                        "group": {
                          "type": "string"
                        },
                        "service": {
                          "type": "string"
                        },
                        "tech": { "$ref": "#/definitions/ipv4_pattern" }
                      },
                      "definitions": {
                            "ipv4_pattern": {"type": "string",
                                             "pattern": "^(?=\\d+\\.\\d+\\.\\d+\\.\\d+$)(?:(?:25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9][0-9]|[0-9])\\.?){4}$"
                                            }
                      }
                    }

    # 直接用Draft4Validator加载对应版本的Schema
    validator = jsonschema.Draft4Validator(str_json_schema)

    lst_errors = []
    for error in validator.iter_errors(str_json_yaml):
        lst_errors.append('Item: ' + error.json_path + ' / Error: ' + error.message)
    if len(lst_errors) > 0:
        print(*lst_errors, sep='\n')
        return False
    else:
        return True

if __name__ == "__main__":
    print(main())

如果你的需求是使用Draft202012版本的Schema规范,只需要把Schema里的$schema字段改成"https://json-schema.org/draft/2020-12/schema",然后直接用Draft202012Validator(str_json_schema)初始化即可,同样不需要evolve。

备注:内容来源于stack exchange,提问作者devops-db

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.14 11:25:27