JSON 架构验证失败 - 错误不是“对象”类型

问题描述 投票:0回答:3

我正在尝试使用 jsonschema 和 python_jsonschema_objects 库从架构文件创建 python 对象,填充该对象中的一些数据,然后根据原始架构验证它。不知怎的,我觉得我做错了什么,但不确定到底是什么。

我尝试了几种不同的模式和数据值,以及使用平面/单个对象删除数组。验证仍然失败。

from jsonschema import validate
import python_jsonschema_objects as pjs
import jsonschema
import json
import os

with open('geocoordinate/geocoordinatearray3.schema.json') as opfile:
   schema = json.load(opfile)

builder = pjs.ObjectBuilder(schema)
ns = builder.build_classes()

Coordinate = ns.Rootschema
ca = Coordinate(latitude=22.22,longitude=33.33)
print(ca.serialize())

try:
    print("about to validate first example")
    validate(instance=ca, schema=schema)
except jsonschema.exceptions. ValidationError as e:
    print("this is validation error:", e)
except json.decorder.JSONDecodeError as e:
    print("not JSON", e)  

这是架构文件:

    {
      "definitions": {},
      "$schema": "http://json-schema.org/draft-07/schema#",
      "$id": "http://example.com/root.json",
      "type": "object",
      "title": "rootSchema",
      "required": [
        "latitude",
        "longitude"
      ],
      "properties": {
        "location": {
          "$id": "#/properties/location",
          "type": "string",
          "title": "The Location Schema",
          "default": "",
          "examples": [
            "Denver, CO"
          ],
          "pattern": "^(.*)$"
        },
        "latitude": {
          "$id": "#/properties/latitude",
          "type": "number",
          "title": "The Latitude Schema",
          "default": 0.0,
          "examples": [
            39.7392
          ]
        },
        "longitude": {
          "$id": "#/properties/longitude",
          "type": "number",
          "title": "The Longitude Schema",
          "default": 0.0,
          "examples": [
            -104.9903
          ]
        },
        "alt": {
          "$id": "#/properties/alt",
          "type": "integer",
          "title": "The Alt Schema",
          "default": 0,
          "examples": [
            5280
          ]
        }
      }
    }

我希望这一点能够得到验证,我正在尝试做的事情非常简单。出现此错误:

即将验证第一个示例 这是验证错误:0> latitude= 22.22> location=> longitude= 33.33>> 不是“object”类型

验证模式中的“类型”失败:

架构

实例:

<rootschema alt=<Literal<int> 0> latitude=<Literal<float> 22.22> 
location=<Literal<str> > longitude=<Literal<float> 33.33>>
python json jsonschema
3个回答
2
投票

我成功了,问题是打字。我必须获取 python 实例并序列化()它。我认为它默认是字符串类型。谢谢大家的帮助!


2
投票

我遇到了同样的问题,并在验证之前在 json 字符串上使用了 json.loads :

import json

json.loads('{"property1":"value1"}')


0
投票

我在 json 输出上遇到以下对象类型错误:

jsonschema.exceptions.ValidationError: [{'州': 'AL', '计划': '全面', '覆盖范围': 'D&O', '公司类型': 'NFP', '字段名称': '关联医院/医学院(仅限教育机构)', 'modifiers': [{'Exposure': 'Not Applicable'}, {'Range Min': '1.0'}, {'Range Max': '1.0'}, {'Technical': '1.0'}]}] 不是 'object' 类型

这是架构: { "$schema": "http://json-schema.org/draft-04/schema#", “类型”:“数组”, “项目”: [ { “类型”:“对象”, “特性”: { “状态”: { “类型”:“字符串” }, “计划”: { “类型”:“字符串” }, “覆盖范围”:{ “类型”:“字符串” }, “公司类型”: { “类型”:“字符串” }, “字段名称”:{ “类型”:“字符串” }, “修饰符”:{ “类型”:“数组”, “项目”: [ { “类型”:“对象”, “特性”: { “接触”: { “类型”:“字符串” } }, “必需的”: [ “接触” ] }, { “类型”:“对象”, “特性”: { “最小范围”:{ “类型”:“字符串” } }, “必需的”: [ “最小范围” ] }, { “类型”:“对象”, “特性”: { “最大范围”:{ “类型”:“字符串” } }, “必需的”: [ “最大范围” ] }, { “类型”:“对象”, “特性”: { “技术的”: { “类型”:“字符串” } }, “必需的”: [ “技术的” ] } ] } } } ] }

© www.soinside.com 2019 - 2024. All rights reserved.