当前位置:   article > 正文

json schema validate_jsonschema validate

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

schema
  1. {
  2. "$schema": "http://json-schema.org/draft-03/schema#",
  3. "title": "App",
  4. "description": "APP LIST",
  5. "type": "array",
  6. "properties":{
  7. "appID": {
  8. "type": "string",
  9. "required":true
  10. },
  11. "appName":{
  12. "type": "string",
  13. "required":true
  14. },
  15. "appCaption":{
  16. "type": "string",
  17. "required":true
  18. },
  19. "icon":{
  20. "type":"string",
  21. "required":true
  22. }
  23. }
  24. }


json:
  1. [{"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/

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta http-equiv="X-UA-Compatible" content="chrome=1" />
  6. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  7. <title>dojox.json.schema example</title>
  8. <script src="http://ajax.googleapis.com/ajax/libs/dojo/1.7.0/dojo/dojo.js"
  9. type="text/javascript"></script>
  10. <script type="text/javascript">
  11. require(["dojox/json/schema"], function() {
  12. // Object to validate
  13. var successObj = {
  14. "foo" : "bar"
  15. };
  16. var failureObj = {
  17. "foo" : 1234
  18. };
  19. // Schema
  20. var schema = {
  21. "type": "object",
  22. "properties" : {
  23. "foo" : {
  24. "type" : "string"
  25. },
  26. "icon":{
  27. }
  28. }
  29. };
  30. // Run validation, which should succeed
  31. // Change this line to use failureObj to see the failure case
  32. var result = dojox.json.schema.validate(successObj, schema);
  33. // Check results
  34. if (result.valid) {
  35. // Success, do something
  36. alert("Object is valid");
  37. } else {
  38. // Failure - extract the errors array
  39. var errorArr = result.errors;
  40. alert("property : " + errorArr[0].property + "\nmessage : "
  41. + errorArr[0].message);
  42. }
  43. });
  44. </script>
  45. </head>
  46. <body>
  47. <h1>Example use of dojox.json.schema</h1>
  48. <p>
  49. 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.'
  50. </p>
  51. </body>
  52. </html>


在postman中的json validate
  1. tests["Status code is 200"] = responseCode.code === 200;
  2. var data = JSON.parse(responseBody);
  3. tests["status"] = data.status === true;
  4. tests["info"] = data.info === "success";
  5. var appData = data.data;
  6. tests['appCount'] = appData.length === 3;
  7. var schema = {
  8. "$schema": "http://json-schema.org/draft-04/schema#",
  9. "title": "App",
  10. "description": "APP LIST",
  11. "type": "object",
  12. "properties":{
  13. "appID": {
  14. "type": "string"
  15. },
  16. "appName":{
  17. "type": "string"
  18. },
  19. "appCaption":{
  20. "type": "string"
  21. },
  22. "icon":{
  23. "type":"string",
  24. "required":true,
  25. "pattern":"^http://"
  26. }
  27. }
  28. };
  29. $.each(appData, function (i) {
  30. var app = this;
  31. tests['appName'+i] = tv4.validate(app, schema);
  32. })


参考资料:
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/
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/盐析白兔/article/detail/286818
推荐阅读
相关标签
  

闽ICP备14008679号