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

时间:2019-08-13 18:57:54

标签: python json jsonschema

我正在尝试使用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>纬度= 22.22>位置=>经度= 33.33 >>不是'object'类型

无法验证架构中的“类型”:

模式

在实例上:

<rootschema alt=<Literal<int> 0> latitude=<Literal<float> 22.22> 
location=<Literal<str> > longitude=<Literal<float> 33.33>>

1 个答案:

答案 0 :(得分:0)

我可以使用了,问题是打字。我必须带python实例并对其进行serialize()。我认为这是默认情况下的字符串类型。谢谢大家的帮助!

相关问题