制作结构JSON模式

时间:2019-03-23 10:58:39

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

如何将以下对象转换为有效的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"
    }
  ]
}

1 个答案:

答案 0 :(得分:0)

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

JSON Schema Generator Online

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

@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"));

    }

一旦您拥有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");
            }

    }

或者您具有如下的自定义验证

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

Maven依赖项

{{1}}