`
flex_莫冲
  • 浏览: 1074916 次
  • 性别: Icon_minigender_1
  • 来自: 广州
社区版块
存档分类
最新评论

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
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); 
})




参考资料:
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/
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics