Ajv JSON数据格式校验 使用
1、官网
2、示例代码
<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
<title>
ajv数据格式校验
</title>
</head>
<body>
<!--<script src="https://cdn.bootcss.com/lodash.js/4.17.10/lodash.min.js"></script>-->
<script src="https://cdn.bootcss.com/ajv/6.5.0/ajv.min.js"></script>
<script type="text/javascript">
var ajv = new Ajv();
var schema = {
"properties": {
"data": {
"type": "object",
"required": ["code", "status", "message", "data", "token"],
"properties": {
"code": {
"type": "number"
},
"status": {
"type": "number",
"enum": [0, 1]
},
"message": {
"type": "string"
},
"data": {
"type": "array"
},
"token": {
"type": "string"
}
}
}
}
};
var validData = {
data: {
code: 303,
data: [1, 2],
message: "请重新登录",
status: 0,
token: "beef5b2efa7f958014a21cad0ca68dc5"
}
};
var valid = ajv.validate(schema, validData);
if(!valid) console.log(ajv.errors);
</script>
</body>
</html>
作者:孟繁贵 Email:meng010387@126.com 期待共同进步!