参考:https://blog.csdn.net/swinfans/article/details/89231682
https://www.jianshu.com/p/1711f2f24dcf?utm_campaign=hugo
import jsonschema
schema = {
"type": "object",
"properties": {
"version_no": {"type": "string"},
"versions": {
"type": "array",
"minItems": 1,
"items": {
"type": "object",
"required": ["name", "type"],
"properties": {
"name": {
"type": "string",
"minLength": 1
},
"type": {
"type": "string",
"enum": ["tecs", "os"]
},
"size": {
"type": "number",
},
}
}
},
},
"required": ["version_no", "versions"]
}
data = {
"version_no": "x123",
"versions": [
{
"name": "123.bin",
"type": "tecs",
"size": 123
},
{
"name": "123.bin",
"type": "o1s",
}
]
}
try:
jsonschema.validate(data, schema)
except jsonschema.exceptions.ValidationError as e:
print(e.message) # 'o1s' is not one of ['tecs', 'os']