一,JSONSchema的概念
JSONSchema是一种用来描述JSON数据的一种JSON数据结构。JSON Schema也有版本,目前的版本有 Draft 7,Draft 6,Draft 4 和Draft 3。使用之前我们先进行cmd安装:pip install jsonschema
二,JSONSchema解析
2.1,type节点
string
number(int/float)
object(dict)
array(list)
boolean
null
{
"type": "string" }
2.2,properties
json以key–value的形式存在,实际就是描述存在的key值。
"properties": {
"name": {
"type": "string"}
2.3,propertyNames
实际是用来描述properties中key的信息,可用正则匹配进行限制描述。
"properties": {
"name_123": {
"type": "string","propertyNames":"^[A-Za-z_][A-Za-z0-9_]*$"}
以上采用了key的名称进行限制,必须以字母开头,且以"_"链接字母或字符的形式,否则将不符合规范。
2.4,required
required是用来描述json中必须存在哪些key。
data= {
"type": "object",
"properties": {
"name": {
"type": "string"},
"age": {
"type": "number"},
"gender": {
"type": "string"}
},
"required": ["name"] # 要求检测d的json数据必须包含name这个key
}
2.5,enum
enum枚举值限制,意思是json数据某个key的value值,只能是以下这几个枚举里的值。
"name": {
"type": "string","enum"