【问题标题】:Make Structure JSON Schema制作结构 JSON 模式
【发布时间】:2019-03-23 10:58:38
【问题描述】:

如何将以下对象转换为有效的 JSON 架构?我在实现使用rest-assured 的工具时需要它。

{
  "page": 2,
  "per_page": 3,
  "total": 12,
  "total_pages": 4,
  "data": [{
      "id": 4,
      "first_name": "Eve",
      "last_name": "Holt",
      "avatar": "https://s3.amazonaws.com/uifaces/faces/twitter/marcoramires/128.jpg"
    },
    {
      "id": 5,
      "first_name": "Charles",
      "last_name": "Morris",
      "avatar": "https://s3.amazonaws.com/uifaces/faces/twitter/stephenmoon/128.jpg"
    },
    {
      "id": 6,
      "first_name": "Tracey",
      "last_name": "Ramos",
      "avatar": "https://s3.amazonaws.com/uifaces/faces/twitter/bigmancho/128.jpg"
    }
  ]
}

【问题讨论】:

    标签: arrays json rest-assured json-schema-validator


    【解决方案1】:

    这里是您可以在线生成 JSON Schema Draft V4 Schemas 的链接

    JSON Schema Generator Online

    这是生成的 JSON 模式。您可以根据您的要求更改所需的块

    {
      "$schema": "http://json-schema.org/draft-04/schema#",
      "type": "object",
      "properties": {
        "page": {
          "type": "integer"
        },
        "per_page": {
          "type": "integer"
        },
        "total": {
          "type": "integer"
        },
        "total_pages": {
          "type": "integer"
        },
        "data": {
          "type": "array",
          "items": [
            {
              "type": "object",
              "properties": {
                "id": {
                  "type": "integer"
                },
                "first_name": {
                  "type": "string"
                },
                "last_name": {
                  "type": "string"
                },
                "avatar": {
                  "type": "string"
                }
              },
              "required": [
                "id",
                "first_name",
                "last_name",
                "avatar"
              ]
            }
          ]
        }
      },
      "required": [
        "page",
        "per_page",
        "total",
        "total_pages",
        "data"
      ]
    }
    

    一旦你有了 JSON 和 Schema,你就可以放心地测试了,如下所示

    @Test(enabled=true)
        public void schemaValidation() {
    
                RestAssured.given().get("http://localhost:8080/endpoint")
                        .then().assertThat().body(matchesJsonSchemaInClasspath(System.getProperty("user.dir")+File.separator+"resources"+File.separator+"JsonSchemas"+File.separator+"SampleSchema.json"));
    
        }
    

    或者您有如下自定义验证

    @Test(enabled=true)
        public void schemaValidation2() throws IOException, ProcessingException {
    
               Response response =  RestAssured.given().get("http://localhost:8080/endpoint")
                        .andReturn();
    
               com.github.fge.jsonschema.main.JsonSchema schemaFileJson = ValidationUtils.getSchemaNode(System.getProperty("user.dir")+File.separator+"resources"+File.separator+"JsonSchemas"+File.separator+"SampleSchema.json");
                ObjectMapper mapper = new ObjectMapper();
                com.fasterxml.jackson.databind.JsonNode responseBodyJsonObject = mapper.readTree(response.getBody().asString());
    
    
                if(schemaFileJson.validate(responseBodyJsonObject).isSuccess()) {
                    System.out.println("Schema is valid");
                }else {
                    System.out.println("Schema is not valid");
                }
    
        }
    

    Maven 依赖项

    <dependency>
                <groupId>org.json</groupId>
                <artifactId>json</artifactId>
                <version>20180130</version>
            </dependency>
            <dependency>
                <groupId>com.github.fge</groupId>
                <artifactId>json-schema-validator</artifactId>
                <version>2.2.6</version>
            </dependency>
            <dependency>
        <groupId>io.rest-assured</groupId>
        <artifactId>json-schema-validator</artifactId>
        <version>3.0.0</version>
    </dependency>
            <!-- https://mvnrepository.com/artifact/com.github.fge/jackson-coreutils -->
            <dependency>
                <groupId>com.github.fge</groupId>
                <artifactId>jackson-coreutils</artifactId>
                <version>1.8</version>
            </dependency>
            <!-- https://mvnrepository.com/artifact/com.github.fge/json-schema-core -->
            <dependency>
                <groupId>com.github.fge</groupId>
                <artifactId>json-schema-core</artifactId>
                <version>1.2.5</version>
            </dependency>
            <dependency>
                <groupId>org.hamcrest</groupId>
                <artifactId>hamcrest-core</artifactId>
                <version>1.3</version>
            </dependency>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-05
      • 2014-08-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多