jsonSchema
功能
1、属性校验
2、属性类型校验
3、属性值校验
from jsonschema import validate, draft7_format_checker
from jsonschema.exceptions import SchemaError, ValidationError
my_schema = {
"$schema": "http://json-schema.org/draft-04/schema#",
"title": "TestInfo",
"description": "some information about test",
"type": "object",
"properties": {
"name": {
"description": "Name of the test"
,"type": "object"
,"properties": {
"str":{
"description": "Name of the test",
"type": "string"
}
}
,"required":['str']
},
"age": {
"description": "age of test",
"type": "integer"
}
},
"required": [
"name", "age"
]
}
# json数据:
json_data = {
"name": {"str11":'bbbbb'},
"age": 25
}
# 验证:
def json_check(json_data):
try:
validate(instance=json_data, schema=my_schema, format_checker=draft7_format_checker)
except SchemaError as e:
return 1,"验证模式schema出错,出错位置:{},提示信息:{}".format(" --> ".join([i for i in e.path if i]), e.message)
except ValidationError as e:
return 1,"不符合schema规定,出错字段:{},提示信息:{}".format(" --> ".join([i for i in e.path if i]), e.message)
else:
return 0,'success'
print(json_check(json_data))
标签:description,format,校验,json,jsonSchema,日志,type,schema
From: https://www.cnblogs.com/dch-21/p/16956039.html