java - 验证响应中特定 JSON 对象的 json 模式

标签 java json jsonschema rest-assured rest-assured-jsonpath

我有一个像这样的 json 响应(响应采用 com.jayway.restassured.response.Response 格式)。

[{
        gameIdGlobal: 1947634,
        season: 2017,
        views: [{
                name: "Recap",
                displayOrder: 1,
                groups: [{
                        type: "static",
                        displayOrder: 1
                    }
                ],
                localizedName: {
                    ENG: "Recap",
                    ESP: "Resumen"
                }
            }
        ]
    }
]

由此,我只需要验证 View 对象的 json 模式。不需要验证整个 json 。为了那个原因 我仅为 View 对象 schema1 创建了一个 json 架构。

schema1.json

{
    "type": "array",
    "items": {
        "id": "view.json",
        "type": "object",
        "properties": {
            "name": {
                "type": "string"
            },
            "displayOrder": {
                "type": "integer",
                "minimum": 1
            },
            "groups": {
                "type": "array"             
            },
            "localizedName": {
                "type": "object",
                "properties": {
                    "ENG": {
                        "type": "string",
                        "description": "the name of the view in english"
                    },
                    "ESP": {
                        "type": "string",
                        "description": "the name of the view in spanish"
                    }
                }
            }
        }
    }
}

我如何执行特定 json 对象的架构验证(来自 josn 响应的 View 对象)

代码

Response response = RestAssured.given().when().get(getURL);
 ValidatableResponse valResponse = response.then().contentType(ContentType.JSON);
  valResponse.body(schema1.json("schema1.json")).assertThat();

最佳答案

您可以指定在将数组作为其属性的对象上允许使用附加属性。以下是整个响应 json 对象的架构:

{
    "$schema": "http://json-schema.org/draft-06/schema#",
    "type": "array",
    "items": {
        "type": "object",
        "required": ["views"],
        "additionalProperties": true,
        "properties": {
            "views": {
                "type": "array",
                "items": {
                    "id": "view.json",
                  ...
        }
    }
}

关于java - 验证响应中特定 JSON 对象的 json 模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49622348/

相关文章:

javascript - “存储” Mongoose 模式/模型(不是文档)以供将来使用

json - 当对象名称中包含DOT时如何从json对象中检索字段值

mysql - Json 模式字段顺序

java - 使用 Kryo 序列化任意 Java 对象(获取 IllegalAccessError)

JavaMail API 和 Tomcat 7 实现

python - 对 Zoho Creator 返回的数据使用 json.loads() - extra_data() 错误

java - 从 stripe webhook 事件中检索 stripe 数据

json - React JSON 架构依赖关系

java - 这个同步块(synchronized block)内发生了什么

java - 通过流式传输将分段文件上传到 Amazon S3 时内存使用率过高?