在线生成jsonschame
https://jsonschema.net/home
pip install jsonschema
from jsonschema import validate
def test_schame():
schame = {
“
s
c
h
e
m
a
"
:
"
h
t
t
p
:
/
/
j
s
o
n
−
s
c
h
e
m
a
.
o
r
g
/
d
r
a
f
t
−
07
/
s
c
h
e
m
a
"
,
"
schema": "http://json-schema.org/draft-07/schema", "
schema":"http://json−schema.org/draft−07/schema","id”: “http://example.com/example.json”,
“type”: “object”,
“required”: [
“productId”,
“productName”,
“price”,
“tags”
],
“properties”: {
“productId”: {
“$id”: “#/properties/productId”,
“type”: “integer”,
“title”: “The productId schema”,
“description”: “An explanation about the purpose of this instance.”,
“default”: 0,
},
"productName": {
"$id": "#/properties/productName",
"type": "string",
"title": "The productName schema",
"description": "An explanation about the purpose of this instance.",
},
"price": {
"$id": "#/properties/price",
"type": "number",
},
"tags": {
"$id": "#/properties/tags",
"type": "array",
"additionalItems": True,
"items": {
"$id": "#/properties/tags/items",
"anyOf": [
{
"$id": "#/properties/tags/items/anyOf/0",
"type": "string",
"title": "The first anyOf schema",
"description": "An explanation about the purpose of this instance.",
"default": "",
"examples": [
"home",
"green"
]
}
]
}
}
},
"additionalProperties": True
}
test_data = {
“productId”: 1,
“productName”: “A green door”,
“price”: 12.50,
“tags”: [“home”, “green”]
}
validate(instance=test_data,schema=schame)
参考:https://blog.csdn.net/zbj18314469395/article/details/93721393