json schema validate

为了检测API返回的数据结构是否正确,刚好chrome的插件postman可以做这件事。其实它用的也是tv4的库来验证。又找了一些开源的验证工具。如下
在线验证工具:
只支持http://json-schema.org/draft-03/schema#
https://json-schema-validator.herokuapp.com/

schema

{
"$schema": "http://json-schema.org/draft-03/schema#",
"title": "App",
"description": "APP LIST",
"type": "array",
"properties":{
"appID": {
"type": "string",
"required":true
},
"appName":{
"type": "string",
"required":true
},
"appCaption":{
"type": "string",
"required":true
},
"icon":{
"type":"string",
"required":true
}
}
}


json:

[{"appID":"1","appName":"push","appCaption":"push ","icon":""},{"appID":"52","appName":"Sample(\u4e13\u7528)","appCaption":"Sample(\u4e13\u7528)","icon":""},{"appID":"64","appName":"\u65b0\u73af\u5883\u6d4b\u8bd5dddd","appCaption":"\u65b0\u73af\u5883\u6d4b\u8bd5","icon":""}]


dojo json schema:
http://pro.jsonlint.com/



<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="chrome=1" />
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>dojox.json.schema example</title>

<script src="http://ajax.googleapis.com/ajax/libs/dojo/1.7.0/dojo/dojo.js"
type="text/javascript"></script>

<script type="text/javascript">

require(["dojox/json/schema"], function() {
// Object to validate
var successObj = {
"foo" : "bar"
};
var failureObj = {
"foo" : 1234
};

// Schema
var schema = {
"type": "object",
"properties" : {
"foo" : {
"type" : "string"
},
"icon":{

}
}
};

// Run validation, which should succeed
// Change this line to use failureObj to see the failure case
var result = dojox.json.schema.validate(successObj, schema);

// Check results
if (result.valid) {
// Success, do something
alert("Object is valid");
} else {
// Failure - extract the errors array
var errorArr = result.errors;
alert("property : " + errorArr[0].property + "\nmessage : "
+ errorArr[0].message);
}
});

</script>


</head>

<body>
<h1>Example use of dojox.json.schema</h1>
<p>
Due to its use of the Dojo CDN distribution on google.com, this HTML file MUST be accessed through an HTTP server such as Apache. file:/// URIs won't work.'
</p>
</body>

</html>


在postman中的json validate
[code]
tests["Status code is 200"] = responseCode.code === 200;

var data = JSON.parse(responseBody);
tests["status"] = data.status === true;
tests["info"] = data.info === "success";
var appData = data.data;
tests['appCount'] = appData.length === 3;
var schema = {
"$schema": "http://json-schema.org/draft-04/schema#",
"title": "App",
"description": "APP LIST",
"type": "object",
"properties":{
"appID": {
"type": "string"
},
"appName":{
"type": "string"
},
"appCaption":{
"type": "string"
},
"icon":{
"type":"string",
"required":true,
"pattern":"^http://"
}
}
};

$.each(appData, function (i) {
var app = this;
tests['appName'+i] = tv4.validate(app, schema);
})


[/code]

参考资料:
http://www.asbjornenge.com/wwc/json_schema.html
http://json-schema.org/documentation.html
postman 用的验证工具
https://github.com/geraintluff/tv4
http://www.getpostman.com/docs/
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值