zoukankan      html  css  js  c++  java
  • 技术那么多,你想看看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>
    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     }

     ##转载注明出处:http://www.cnblogs.com/wade-xu/p/4662127.html 

    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

      

    感谢阅读,如果您觉得本文的内容对您的学习有所帮助,您可以点击右下方的推荐按钮,您的鼓励是我创作的动力。

    ##转载注明出处:http://www.cnblogs.com/wade-xu/p/4662127.html 

  • 相关阅读:
    Xcode删除断点的方法
    UIImageView上面不能加载button
    TTThumbsViewController 由 4张 改为 3张的方式
    TTThumbsViewController 由 4张 改为 3张的方式 增补
    TTButton 的正确使用的方法
    使用缓存到本地的图片对initWithUrl的进行初始化
    extThree20XML extThree20JSON 引入到工程中的方式
    TTButton的使用小结
    千万别study English,应学会learn English——英语学习方法强烈推荐
    转脱壳方法
  • 原文地址:https://www.cnblogs.com/wade-xu/p/4662127.html
Copyright © 2011-2022 走看看