技术那么多,你想看看JSON Schema的测试吗?
2015-07-21 11:35
537 查看
目录 1. 什么是JSON Schema? 2. 如何定义一个JSON Schema 3. 如何测试JSON Schema a) 使用JSON Schema validator GUI b) 在Java code里使用JSON Schema validator 4.参考文档
什么是JSON Schema?
JSON模式是基于JSON格式定义JSON数据结构的规范。描述现有的数据格式
干净的人类和机器可读的文档
完成结构验证, 用户
自动化测试
验证客户端提交的数据
如何定义一个JSON Schema
一个简单的例子
JSON Data 如下"Hello, World"
JSON Schema 定义成
{ "type": "string" }
用这个Schema 我们就可以来验证JSON数据
根据Data来生成JSON Schema 有现成的工具可以用http://jsonschema.net/#/
##转载注明出处:/article/6992240.html
接下来看一个基本的JSON Schema
{
"$schema": "http://json-schema.org/draft-04/schema#",
"title": "User",
"description": "demo schema",
"type": "object",
"properties": {
"firstName": { "type": "string" },
"LastName": { "type": "string" },
"age": {
"description": "Age in years",
"type": "integer",
"minimum": 0
}
},
"required": [
"firstName",
"LastName"
]
}
让我们来看看在这个模式中可以使用的各种重要的关键词:
更多的关键字可以参考http://json-schema.org/latest/json-schema-validation.html
or http://tools.ietf.org/html/draft-zyp-json-schema-03#page-11
##转载注明出处: /article/6992240.html
如何测试JSON Schema
当我们编写了一个JSON Schema 用于对客户端提交的数据进行验证之前,我们得确保我们编写的JSON Schema是正确的,我们当然就可以构造一些数据反向验证我们的JSON Schema的正确性与否。网上有三十多个各种语言实现的JSON Schema validator, 我们用的是Java 里非常流行的,GitHub地址在这里。
使用JSON Schema validator GUI
地址 http://json-schema-validator.herokuapp.com/Validation results: success
Validation results: failure
Error Message的信息非常详细。
##转载注明出处:/article/6992240.html
在Java code里使用JSON Schema validator
Maven pom.xml 配置<dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-core</artifactId> <version>2.3.0</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.3.0</version> </dependency> <dependency> <groupId>com.github.fge</groupId> <artifactId>json-schema-validator</artifactId> <version>2.2.6</version> </dependency>
import com.fasterxml.jackson.databind.JsonNode; import com.github.fge.jackson.JsonNodeReader; import com.github.fge.jsonschema.core.report.ProcessingMessage; import com.github.fge.jsonschema.core.report.ProcessingReport; import com.github.fge.jsonschema.main.JsonSchemaFactory;
Validation success
@Test public void validate_driverSet_schema() { //some code to create Json schema, which we want to use data to validate JsonNode schema = readJSONfile("src/test/resources/json/Schema.json"); JsonNode data = readJSONfile("src/test/resources/json/DataGoodExample.json"); ProcessingReport report = JsonSchemaFactory.byDefault().getValidator().validateUnchecked(schema, data); Assert.assertTrue(report.isSuccess()); }
##转载注明出处:/article/6992240.html
Validation failure
@Test // wrong data public void validate_driverSet_schema2() { //some code to create Json schema, which we want to use data to validate JsonNode data = readJSONfile("src/test/resources/json/DataBadExample.json"); JsonNode schema = readJSONfile("src/test/resources/json/Schema.json"); ProcessingReport report = JsonSchemaFactory.byDefault().getValidator().validateUnchecked(schema, data); Assert.assertFalse(report.isSuccess()); // assert error message Iterator<ProcessingMessage> it = report.iterator(); Assert.assertEquals( "instance type (string) does not match any allowed primitive type (allowed: [\"integer\"])", it.next().getMessage()); } private JsonNode readJSONfile(String filePath) { JsonNode instance = null; try { instance = new JsonNodeReader().fromReader(new FileReader(filePath)); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return instance; }
参考文档
官方文档: http://json-schema.org/GitHub: json-schema-validator
感谢阅读,如果您觉得本文的内容对您的学习有所帮助,您可以点击右下方的推荐按钮,您的鼓励是我创作的动力。
##转载注明出处:/article/6992240.html
相关文章推荐
- JSP的九大内置对象和四个作用域
- JavaScript 表单处理
- 接口返回json的取值记录总结
- JSP中用js控制添加行,删除行
- js中几种实用的跨域方法原理详解
- web api写api接口时默认返回的是把你的对象序列化后以XML形式返回,那么怎样才能让其返回为json呢
- js控制select选定项问题
- js 字符串转换成数字的三种方法
- 11. JavaScript 对象
- json 数据 添加 删除 排序
- Jsp页面嵌入外部页面的方法
- JSP向后台传参数的四种方式
- 在JSP表单提交中怎样在Servlet中处理文件上传并且获取JSP页面传过来的一些参数
- 10. Javascript DOM节点
- 【JavaScript 14—学习总结】:从小事做起
- 【JavaScript 14—学习总结】:从小事做起
- js 封装的函数 总结
- 第三章 JS基本概念
- 《JavaScript高级程序设计(第三版)》读书笔记
- js编码url