技术那么多,你想看看JSON Schema的测试吗?
目录
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/#/
##转载注明出处:http://www.cnblogs.com/wade-xu/p/4662127.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
##转载注明出处: http://www.cnblogs.com/wade-xu/p/4662127.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的信息非常详细。
##转载注明出处:http://www.cnblogs.com/wade-xu/p/4662127.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());
}
##转载注明出处:http://www.cnblogs.com/wade-xu/p/4662127.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
感谢阅读,如果您觉得本文的内容对您的学习有所帮助,您可以点击右下方的推荐按钮,您的鼓励是我创作的动力。
##转载注明出处:http://www.cnblogs.com/wade-xu/p/4662127.html
技术那么多,你想看看JSON Schema的测试吗?的更多相关文章
- rest-assured之Schema validation(包括JSON Schema validation及Xml Schema validation)
rest-assured从2.1.0版本开始支持 Schema 验证,包括JSON Schema validation及Xml Schema validation.我们之前断言响应体都是一个一个字段 ...
- .net 项目使用 JSON Schema
.net 项目使用 JSON Schema 最近公司要做配置项的改造,要把appsettings.json的内容放到数据库,经过分析还是用json的方式存储最为方便,项目改动性最小,这就牵扯到一个问题 ...
- Json Schema的使用
直接上案例: 在Web Api通讯中,客户端发送json数据,服务端反序列化json(json与某个类形成对应关系),在某些情况下,需要校验其上传的json是否合法. 服务端是使用Json.net(n ...
- Json Schema简介
1. 引言 什么是Json Schema? 以一个例子来说明 假设有一个web api,接受一个json请求,返回某个用户在某个城市关系最近的若干个好友.一个请求的例子如下: { "city ...
- Json schema 以及在python中的jsonschema
目录 1. JSON Schema简介 2. JSON Schema关键字详解 2.1 $schema 2.2 title和description 2.3 type 3 type常见取值 3.1 当t ...
- Understanding JSON Schema
json schema 在线校验器 译自:Understanding JSON Schema { "type": "object", "propert ...
- Map传参优雅检验,试试json schema validator
背景 笔者目前所在团队的代码年代已久,早年规范缺失导致现在维护成本激增,举一个深恶痛疾的例子就是方法参数使用Map"一撸到底",说多了都是泪,我常常在团队内自嘲"咱硬是把 ...
- 使用JSON Schema来验证接口数据
最近在做一些关于JSON Schema的基建,JSON Schema可以描述一个JSON结构,那么反过来他也可以来验证一个JSON是否符合期望的格式. 如果之前看我写的<使用joi来验证数据模型 ...
- Json.Net使用JSON Schema验证JSON格式【实例】
给出一个Json,验证其格式是否符合规则. { "coord": { //对象 "lon": 145.77, "lat": -16.92 } ...
随机推荐
- 记录一次fat32格式U盘不识别问题
升级了4.1.15内核发现U盘不识别了,考虑到内核编译前的配置是通过localmodconfig完成的,所以大略是缺模块导致. 于是开始查配置,USB控制器,EHCI,mass storeage de ...
- ETL工具与脚本实现之间的对比
scripts, custom code and individual vs. team development doesn’t scale And: ‣Lack of coding standard ...
- 链式编程中的next()和end()
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...
- HBase with MapReduce (MultiTable Read)
hbase当中没有两表联查的操作,要实现两表联查或者在查询一个表的同时也需要访问另外一张表的时候,可以通过mapreduce的方式来实现,实现方式如下:由于查询是map过程,因此这个过程不需要设计re ...
- js(function(){alert(‘’‘)})
function(){alert('sss')}是个匿名函数.没有名字.所以没有办法调用.在外面加个括号,就变成了一个值,值的内容是函数的引用.例如var a = (function(){" ...
- linux信号处理时机
信号号称所谓软中断,事实上,还是没有真正的硬件中断那样能随时改变cpu的执行流 硬件中断之所以能一发生就得到处理是因为处理器在每个指令周期的结尾都会去检查中断,这种粒度是很细的 但是信号的实现只是在进 ...
- django允许跨域备忘笔记
详细信息请拜读网址:https://github.com/ottoyiu/django-cors-headers/ 安装: 在virtaulenv环境中执行 pip install django-co ...
- 搜狗输入法弹出搜狐新闻的解决办法(sohunews.exe)
狗输入法弹出搜狐新闻的解决办法(sohunews.exe) 1.找到搜狗输入法的安装目录(一般是C:\program files\sougou input\版本号\)2.右键点击sohunews.ex ...
- 标准库中的-stack
#include <sequence_concepts.h> __STL_BEGIN_NAMESPACE // Forward declarations of operators == a ...
- Java-->Gson解析相较于Json
--> Gson解析jar包: 链接:http://pan.baidu.com/s/1slCeq77 密码:f9ig --> 官方Json解析工具类: package com.drago ...