Tim*_*m B 6 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)
Run Code Online (Sandbox Code Playgroud)
这是架构文件:
{
"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
]
}
}
}
Run Code Online (Sandbox Code Playgroud)
我期待这一点得到验证,我正在尝试做的事情非常简单。出现此错误:
即将验证第一个示例,这是验证错误:0> latitude= 22.22> location= > longitude= 33.33>> is not of type 'object'
验证架构中的“类型”失败:
图式
实例上:
<rootschema alt=<Literal<int> 0> latitude=<Literal<float> 22.22>
location=<Literal<str> > longitude=<Literal<float> 33.33>>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
14849 次 |
| 最近记录: |