【问题标题】:Getting Error " Data Should Be Object" while validating json schema验证 json 架构时出现错误“数据应该是对象”
【发布时间】:2021-11-12 22:36:19
【问题描述】:

我已经为 json 模式工具生成了 json 模式。我已在变量中添加了该模式。但是在验证过程中,我收到错误消息“Validate json Schema | AssertionError: expected data to meet schema but found following errors: data should be object”。

const schema = {
    "$schema": "http://json-schema.org/draft-07/schema",
    "$id": "http://example.com/example.json",
    "type": "object",
    "title": "The root schema",
    "description": "The root schema comprises the entire JSON document.",
    "default": {},
    "examples": [
        {
            "name": "Learn Appium Automation with Java",
            "isbn": "{{isbn}}",
            "aisle": "227",
            "author": "{{author_name}}"
        }
    ],
    "required": ["name", "isbn", "aisle", "author"],
    "properties": {
        "name": {
            "$id": "#/properties/name",
            "type": "string",
            "title": "The name schema",
            "description": "An explanation about the purpose of this instance.",
            "default": "",
            "examples": ["Learn Appium Automation with Java"]
        },
        "isbn": {
            "$id": "#/properties/isbn",
            "type": "string",
            "title": "The isbn schema",
            "description": "An explanation about the purpose of this instance.",
            "default": "",
            "examples": ["{{isbn}}"]
        },
        "aisle": {
            "$id": "#/properties/aisle",
            "type": "string",
            "title": "The aisle schema",
            "description": "An explanation about the purpose of this instance.",
            "default": "",
            "examples": ["227"]
        },
        "author": {
            "$id": "#/properties/author",
            "type": "string",
            "title": "The author schema",
            "description": "An explanation about the purpose of this instance.",
            "default": "",
            "examples": ["{{author_name}}"]
        }
    },
    "additionalProperties": true
};



pm.test("Validate json Schema",function(){
 
    pm.response.to.have.jsonSchema(schema);
})

enter image description here

【问题讨论】:

    标签: json api postman postman-collection-runner postman-testcase


    【解决方案1】:

    问题是您的响应是 json 数组,而不是 json 对象,因此您需要稍微更改架构。

    const schema = {
        "type": "array",
        "items": {
            "type": "object",
            "properties": {
                "name": {
                    "type": "string"
                },
                "isbn": {
                    "type": "string"
                },
                "aisle": {
                    "type": "string"
                },
                "author": {
                    "type": "string"
                }
            },
            "required": ["name", "isbn", "aisle", "author"],
            "additionalProperties": true
        }
    };
    

    【讨论】:

      猜你喜欢
      • 2021-06-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-05-15
      • 2018-09-07
      • 2011-11-21
      • 1970-01-01
      • 2020-07-11
      相关资源
      最近更新 更多