目录
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的测试吗?的更多相关文章

  1. rest-assured之Schema validation(包括JSON Schema validation及Xml Schema validation)

    rest-assured从2.1.0版本开始支持  Schema 验证,包括JSON Schema validation及Xml Schema validation.我们之前断言响应体都是一个一个字段 ...

  2. .net 项目使用 JSON Schema

    .net 项目使用 JSON Schema 最近公司要做配置项的改造,要把appsettings.json的内容放到数据库,经过分析还是用json的方式存储最为方便,项目改动性最小,这就牵扯到一个问题 ...

  3. Json Schema的使用

    直接上案例: 在Web Api通讯中,客户端发送json数据,服务端反序列化json(json与某个类形成对应关系),在某些情况下,需要校验其上传的json是否合法. 服务端是使用Json.net(n ...

  4. Json Schema简介

    1. 引言 什么是Json Schema? 以一个例子来说明 假设有一个web api,接受一个json请求,返回某个用户在某个城市关系最近的若干个好友.一个请求的例子如下: { "city ...

  5. 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 ...

  6. Understanding JSON Schema

    json schema 在线校验器 译自:Understanding JSON Schema { "type": "object", "propert ...

  7. Map传参优雅检验,试试json schema validator

    背景 笔者目前所在团队的代码年代已久,早年规范缺失导致现在维护成本激增,举一个深恶痛疾的例子就是方法参数使用Map"一撸到底",说多了都是泪,我常常在团队内自嘲"咱硬是把 ...

  8. 使用JSON Schema来验证接口数据

    最近在做一些关于JSON Schema的基建,JSON Schema可以描述一个JSON结构,那么反过来他也可以来验证一个JSON是否符合期望的格式. 如果之前看我写的<使用joi来验证数据模型 ...

  9. Json.Net使用JSON Schema验证JSON格式【实例】

    给出一个Json,验证其格式是否符合规则. { "coord": { //对象 "lon": 145.77, "lat": -16.92 } ...

随机推荐

  1. 【jQuery】serializeArray()与serialize()的区别

    serialize()序列化表单元素为字符串,用于 Ajax 请求. serializeArray()序列化表单元素为JSON数据. 具体实例如下: 1 <!DOCTYPE html PUBLI ...

  2. 拓扑编号 vijos1790

    题意就是拓扑排序,要求1的序号尽可能小,然后2的序号尽可能小,3,4... 一开始很容易想到直接贪心,每次选一个入度为0的点,如果有多个,就选编号最小的那个,但是很容易找到反例. 看了下题解,应该是反 ...

  3. HDU1004 BALLO0N

    Let the Balloon Rise Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others ...

  4. Python学习路程day13

    JavaScript JavaScript是一门编程语言,浏览器内置了JavaScript语言的解释器,所以在浏览器上按照JavaScript语言的规则编写相应代码之,浏览器可以解释并做出相应的处理. ...

  5. input , textarea 边框问题

    一.去掉边框: 看看基本的HTML: 复制代码 代码如下: <div class="wrap"> <input type="text" cla ...

  6. 更新包地址安装新版node.js

    # apt-get update # apt-get install -y python-software-properties software-properties-common # add-ap ...

  7. C++学习笔记24:makefile文件

    makefile make命令:负责c/c++程序编译与链接 make根据指定命令进行建构 建构规则文件:GNUmakefile , makefile,Makefile makefile 文件格式 m ...

  8. cd hit使用

    ~~和唐老师一个实验室的人开发的~~ CD-HIT is a very widely used program for clustering and comparing protein or nucl ...

  9. kallisto:Near-optimal RNA-Seq quantification

    Near-optimal RNA-Seq quantification https://pachterlab.github.io/kallisto 文章标题:   Pseudoalignment fo ...

  10. codeforces195a

    link:http://codeforces.com/problemset/problem/336/A 很简单的一道题目,当初有个单词不认识,isosceles原来意思是等腰的o(╯□╰)o #inc ...