Shailesh Waghole
Shailesh Waghole

Reputation: 11

Getting Error " Data Should Be Object" while validating json schema

I have generated json schema for json schema tool. I have added that schema in variable. But during validation I am getting error that "Validate json Schema | AssertionError: expected data to satisfy 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

Upvotes: 1

Views: 4560

Answers (1)

lucas-nguyen-17
lucas-nguyen-17

Reputation: 5917

Problem is your response is json array, not json object, so you need to change schema a little bit.

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

Upvotes: 3

Related Questions