json sechma 数据格式校验

json sechma 格式比对

sechma 使用介绍:
http://xaber.co/2015/10/20/JSON-schema-%E4%BD%BF%E7%94%A8%E6%95%99%E7%A8%8B%E6%96%87%E6%A1%A3/

https://json-schema.org/understanding-json-schema/index.html
sechma 格式在线比较:http://json-schema-validator.herokuapp.com/index.jsp
sechma 格式在线生成:同上

对于进本使用和含义,可以参考上面的链接进行学习
下面是本人在工作中遇到的问题积累

1、sechma 中格式为 null 的时候,数据中 为 null 才匹配,其他均为不匹配, 对于返回结果可能为 null 字段,采用多个模式

正常校验
不为 null
修改后

2、sechma 中未定义的字段, 数据多出的字段默认校验通过,additionalProperties 指定为 false 则会校验失败


在这里插入图片描述

sechma 格式

{
    
    
  "$schema": "http://json-schema.org/draft-04/schema#",
  "type": "object",
  "properties": {
    
    
    "Array": {
    
    
      "type": "array",
      "items": {
    
    
        "type": "number"
      }
    },
    "Null": {
    
    
      "type": "null"
    },
    "Object": {
    
    
      "type": "object",
      "properties": {
    
    
        "12345": {
    
    
          "type": "string"
        },
        "Number": {
    
    
          "type": "number"
        },
        "String": {
    
    
          "type": "string"
        }
      }
    }
  },
  "additionalProperties": false
}

数据

{
    
    
  "Array": [
    1,
    2,
    3
  ],
  "Null": null,
  "Object": {
    
    
   "Number": 123,
   "12345": "object",
   "String": "Hello World"
  },
  "add":"nothing"
}

3、sechma 中定义了的字段,数据缺失字段默认校验通过,required 指定了哪些属性是必须的,则校验失败

在这里插入图片描述
在这里插入图片描述

sechma 格式

{
    
    
  "$schema": "http://json-schema.org/draft-04/schema#",
  "type": "object",
  "properties": {
    
    
    "Array": {
    
    
      "type": "array",
      "items": {
    
    
        "type": "number"
      }
    },
    "Null": {
    
    
      "type": "null"
    },
    "Object": {
    
    
      "type": "object",
      "properties": {
    
    
        "12345": {
    
    
          "type": "string"
        },
        "Number": {
    
    
          "type": "number"
        },
        "String": {
    
    
          "type": "string"
        }
      },
      "required": [
        "String"
      ]
    }
  }
}

数据

{
    
    
  "Array": [
    1,
    2,
    3
  ],
  "Null": null,
 
  "Object": {
    
    
   "Number": 123,
   "12345": "object"
   }
}

4、sechma 中 object 的含义包含 和 map 两种含义,map 中的 key 是动态变化的,例如项目中常常以业务 id 作为 map 的 key

这个时候我们需要注意两点:

  1. 对于map 的元素我们只需要对比一组即可
  2. 对于动态变化的 key 我门可以采用 patternProperties 方式,进行 key 的匹配

例如,用户信息包含, userId,name,age,其中 key 为userId

{
    
    
  "666": {
    
    
    "name": "易烊千玺",
    "age": 22
  },
  "777": {
    
    
   "name": "彭于晏",
    "age": 37
  }
}

生成的 sechma 格式

{
    
    
  "$schema": "http://json-schema.org/draft-04/schema#",
  "type": "object",
  "properties": {
    
    
    "666": {
    
    
      "type": "object",
      "properties": {
    
    
        "name": {
    
    
          "type": "string"
        },
        "age": {
    
    
          "type": "number"
        }
      }
    },
    "777": {
    
    
      "type": "object",
      "properties": {
    
    
        "name": {
    
    
          "type": "string"
        },
        "age": {
    
    
          "type": "number"
        }
      }
    }
  }
}

例如我们下一次校验的用户吴彦祖的信息为

{
    
    
  "888": {
    
    
    "name": "吴彦祖",
    "age": 55
  }
}

直接按照上面的 sechma 进行校验无效的。没有匹配到 “888” 属性,同上 2,默认通过,如下图所示

在这里插入图片描述

修改, sechma 属性修改为匹配模式patternProperties,然后对属性进行正则匹配即可

{
    
    
  "$schema": "http://json-schema.org/draft-04/schema#",
  "type": "object",
  "patternProperties": {
    
    
    "^[0-9]+$": {
    
    
      "type": "object",
      "properties": {
    
    
        "name": {
    
    
          "type": "string"
        },
        "age": {
    
    
          "type": "number"
        }
      }
    }
  }
}

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/Misszhoudandan/article/details/113729534