什麼是JSON Schema?
JSON模式是基於JSON格式定義JSON數據結構的規範。
- 描述現有的數據格式
- 乾淨的人類和機器可讀的文檔
- 完成結構驗證, 用戶
- 自動化測試
- 驗證客戶端提交的數據
如何定義一個JSON Schema
一個簡單的例子
JSON Data 如下
"Hello, World"
JSON Schema 定義成
{
"type": "string"
}
用這個Schema 我們就可以來驗證JSON數據
根據Data來生成JSON Schema 有現成的工具可以用http://jsonschema.net/#/
接下來看一個基本的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
如何測試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的信息非常詳細。
在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>
1 import com.fasterxml.jackson.databind.JsonNode; 2 import com.github.fge.jackson.JsonNodeReader; 3 import com.github.fge.jsonschema.core.report.ProcessingMessage; 4 import com.github.fge.jsonschema.core.report.ProcessingReport; 5 import com.github.fge.jsonschema.main.JsonSchemaFactory;
Validation success
1 @Test 2 public void validate_driverSet_schema() { 3 4 //some code to create Json schema, which we want to use data to validate 5 6 JsonNode schema = readJSONfile("src/test/resources/json/Schema.json"); 7 8 JsonNode data = readJSONfile("src/test/resources/json/DataGoodExample.json"); 9 10 ProcessingReport report = 11 JsonSchemaFactory.byDefault().getValidator().validateUnchecked(schema, data); 12 Assert.assertTrue(report.isSuccess()); 13 }
Validation failure
1 @Test 2 // wrong data 3 public void validate_driverSet_schema2() { 4 5 //some code to create Json schema, which we want to use data to validate 6 7 JsonNode data = readJSONfile("src/test/resources/json/DataBadExample.json"); 8 JsonNode schema = readJSONfile("src/test/resources/json/Schema.json"); 9 10 ProcessingReport report = 11 JsonSchemaFactory.byDefault().getValidator().validateUnchecked(schema, data); 12 Assert.assertFalse(report.isSuccess()); 13 14 // assert error message 15 Iterator<ProcessingMessage> it = report.iterator(); 16 Assert.assertEquals( 17 "instance type (string) does not match any allowed primitive type (allowed: [\"integer\"])", 18 it.next().getMessage()); 19 20 } 21 22 private JsonNode readJSONfile(String filePath) { 23 JsonNode instance = null; 24 try { 25 instance = new JsonNodeReader().fromReader(new FileReader(filePath)); 26 } catch (FileNotFoundException e) { 27 e.printStackTrace(); 28 } catch (IOException e) { 29 e.printStackTrace(); 30 } 31 return instance; 32 }
參考文檔
官方文檔: http://json-schema.org/
GitHub: json-schema-validator