您的位置:首页 > Web前端 > JavaScript

技术那么多,你想看看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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: