Validating schema with an array

Hi everyone. Happy New Year! I need some help on validating a JSON schema with an array.
The array has an object with properties that I would like to validate. JSON example below.
[
{
“id”: “05265cbe-e811-4033-af3a-d927fb87235b”,
“name”: “Bickley Towers”,
“type”: null,
“description”: “Rebuild of Bickley Towers”,
“location”: {
“addressLine1”: “58 Grove street”,
“addressLine2”: null,
“city”: “Bromley”,
“stateOrCounty”: “Greater London”,
“postcodeOrZipcode”: “BR1 7LP”,
“country”: “UK”
},
“creationDate”: “2021-01-03T16:22:16.654Z”,
“contractDate”: “0001-01-01T00:00:00”,
“startDate”: null,
“quoteAmount”: 150000.0,
“currencyCode”: “GBP”,
“status”: “Draft”,
“requestCount”: 0,
“participantCount”: 1
}
]

The test I have running against it is below. How do I check that the properties in the object are correct? I have tried to use “required” but that doesn’t seem to work.

const jsonData = pm.response.json();

const schema = {
“type” : “array”,
“minItems”: 1,
“items” : {
“type” :“object”,
“properties” : {
“id”: {“type”: “string”},
“name”: {“type”: “string”},
“type”: {
“type”: [“string”,“null”]
},
“description”: {“type”: “string”},
“location”: {
“type”: “object”,
“properties”: {
“addressLine1”: {
“type”: [“string”,“null”]
},
“addressLine2”: {
“type”: [“string”,“null”]
},
“city”: {“type”: “string”},
“stateOrCounty”: {“type”: “string”},
“postcodeOrZipcode”: {“type”: “string”},
“country”: {“type”: “string”},
}
},
“creationDate”: {“type”: “string”},
“contractDate”: {“type”: “string”},
“startDate”: {
“type”: [“string”,“null”]
},
“quoteAmount”: {“type”: “number”},
“currencyCode”: {“type”: “string”},
“status”: {“type”: “string”},
“requestCount”: {“type”: “number”}

        }
    }, 
};

pm.test(“Validate Schema”, ()=> {
pm.response.to.have.jsonSchema(schema);
});

could you try this schema :

{
    "$schema": "http://json-schema.org/draft-07/schema",
    "$id": "http://example.com/example.json",
    "type": "array",
    "title": "The root schema",
    "description": "The root schema comprises the entire JSON document.",
    "default": [],
    "examples": [
        [
            {
                "id": "05265cbe-e811-4033-af3a-d927fb87235b",
                "name": "Bickley Towers",
                "type": null,
                "description": "Rebuild of Bickley Towers",
                "location": {
                    "addressLine1": "58 Grove street",
                    "addressLine2": null,
                    "city": "Bromley",
                    "stateOrCounty": "Greater London",
                    "postcodeOrZipcode": "BR1 7LP",
                    "country": "UK"
                },
                "creationDate": "2021-01-03T16:22:16.654Z",
                "contractDate": "0001-01-01T00:00:00",
                "startDate": null,
                "quoteAmount": 150000.0,
                "currencyCode": "GBP",
                "status": "Draft",
                "requestCount": 0,
                "participantCount": 1
            }
        ]
    ],
    "additionalItems": true,
    "items": {
        "$id": "#/items",
        "anyOf": [
            {
                "$id": "#/items/anyOf/0",
                "type": "object",
                "title": "The first anyOf schema",
                "description": "An explanation about the purpose of this instance.",
                "default": {},
                "examples": [
                    {
                        "id": "05265cbe-e811-4033-af3a-d927fb87235b",
                        "name": "Bickley Towers",
                        "type": null,
                        "description": "Rebuild of Bickley Towers",
                        "location": {
                            "addressLine1": "58 Grove street",
                            "addressLine2": null,
                            "city": "Bromley",
                            "stateOrCounty": "Greater London",
                            "postcodeOrZipcode": "BR1 7LP",
                            "country": "UK"
                        },
                        "creationDate": "2021-01-03T16:22:16.654Z",
                        "contractDate": "0001-01-01T00:00:00",
                        "startDate": null,
                        "quoteAmount": 150000.0,
                        "currencyCode": "GBP",
                        "status": "Draft",
                        "requestCount": 0,
                        "participantCount": 1
                    }
                ],
                "required": [
                    "id",
                    "name",
                    "type",
                    "description",
                    "location",
                    "creationDate",
                    "contractDate",
                    "startDate",
                    "quoteAmount",
                    "currencyCode",
                    "status",
                    "requestCount",
                    "participantCount"
                ],
                "properties": {
                    "id": {
                        "$id": "#/items/anyOf/0/properties/id",
                        "type": "string",
                        "title": "The id schema",
                        "description": "An explanation about the purpose of this instance.",
                        "default": "",
                        "examples": [
                            "05265cbe-e811-4033-af3a-d927fb87235b"
                        ]
                    },
                    "name": {
                        "$id": "#/items/anyOf/0/properties/name",
                        "type": "string",
                        "title": "The name schema",
                        "description": "An explanation about the purpose of this instance.",
                        "default": "",
                        "examples": [
                            "Bickley Towers"
                        ]
                    },
                    "type": {
                        "$id": "#/items/anyOf/0/properties/type",
                        "type": "null",
                        "title": "The type schema",
                        "description": "An explanation about the purpose of this instance.",
                        "default": null,
                        "examples": [
                            null
                        ]
                    },
                    "description": {
                        "$id": "#/items/anyOf/0/properties/description",
                        "type": "string",
                        "title": "The description schema",
                        "description": "An explanation about the purpose of this instance.",
                        "default": "",
                        "examples": [
                            "Rebuild of Bickley Towers"
                        ]
                    },
                    "location": {
                        "$id": "#/items/anyOf/0/properties/location",
                        "type": "object",
                        "title": "The location schema",
                        "description": "An explanation about the purpose of this instance.",
                        "default": {},
                        "examples": [
                            {
                                "addressLine1": "58 Grove street",
                                "addressLine2": null,
                                "city": "Bromley",
                                "stateOrCounty": "Greater London",
                                "postcodeOrZipcode": "BR1 7LP",
                                "country": "UK"
                            }
                        ],
                        "required": [
                            "addressLine1",
                            "addressLine2",
                            "city",
                            "stateOrCounty",
                            "postcodeOrZipcode",
                            "country"
                        ],
                        "properties": {
                            "addressLine1": {
                                "$id": "#/items/anyOf/0/properties/location/properties/addressLine1",
                                "type": "string",
                                "title": "The addressLine1 schema",
                                "description": "An explanation about the purpose of this instance.",
                                "default": "",
                                "examples": [
                                    "58 Grove street"
                                ]
                            },
                            "addressLine2": {
                                "$id": "#/items/anyOf/0/properties/location/properties/addressLine2",
                                "type": "null",
                                "title": "The addressLine2 schema",
                                "description": "An explanation about the purpose of this instance.",
                                "default": null,
                                "examples": [
                                    null
                                ]
                            },
                            "city": {
                                "$id": "#/items/anyOf/0/properties/location/properties/city",
                                "type": "string",
                                "title": "The city schema",
                                "description": "An explanation about the purpose of this instance.",
                                "default": "",
                                "examples": [
                                    "Bromley"
                                ]
                            },
                            "stateOrCounty": {
                                "$id": "#/items/anyOf/0/properties/location/properties/stateOrCounty",
                                "type": "string",
                                "title": "The stateOrCounty schema",
                                "description": "An explanation about the purpose of this instance.",
                                "default": "",
                                "examples": [
                                    "Greater London"
                                ]
                            },
                            "postcodeOrZipcode": {
                                "$id": "#/items/anyOf/0/properties/location/properties/postcodeOrZipcode",
                                "type": "string",
                                "title": "The postcodeOrZipcode schema",
                                "description": "An explanation about the purpose of this instance.",
                                "default": "",
                                "examples": [
                                    "BR1 7LP"
                                ]
                            },
                            "country": {
                                "$id": "#/items/anyOf/0/properties/location/properties/country",
                                "type": "string",
                                "title": "The country schema",
                                "description": "An explanation about the purpose of this instance.",
                                "default": "",
                                "examples": [
                                    "UK"
                                ]
                            }
                        },
                        "additionalProperties": true
                    },
                    "creationDate": {
                        "$id": "#/items/anyOf/0/properties/creationDate",
                        "type": "string",
                        "title": "The creationDate schema",
                        "description": "An explanation about the purpose of this instance.",
                        "default": "",
                        "examples": [
                            "2021-01-03T16:22:16.654Z"
                        ]
                    },
                    "contractDate": {
                        "$id": "#/items/anyOf/0/properties/contractDate",
                        "type": "string",
                        "title": "The contractDate schema",
                        "description": "An explanation about the purpose of this instance.",
                        "default": "",
                        "examples": [
                            "0001-01-01T00:00:00"
                        ]
                    },
                    "startDate": {
                        "$id": "#/items/anyOf/0/properties/startDate",
                        "type": "null",
                        "title": "The startDate schema",
                        "description": "An explanation about the purpose of this instance.",
                        "default": null,
                        "examples": [
                            null
                        ]
                    },
                    "quoteAmount": {
                        "$id": "#/items/anyOf/0/properties/quoteAmount",
                        "type": "number",
                        "title": "The quoteAmount schema",
                        "description": "An explanation about the purpose of this instance.",
                        "default": 0.0,
                        "examples": [
                            150000.0
                        ]
                    },
                    "currencyCode": {
                        "$id": "#/items/anyOf/0/properties/currencyCode",
                        "type": "string",
                        "title": "The currencyCode schema",
                        "description": "An explanation about the purpose of this instance.",
                        "default": "",
                        "examples": [
                            "GBP"
                        ]
                    },
                    "status": {
                        "$id": "#/items/anyOf/0/properties/status",
                        "type": "string",
                        "title": "The status schema",
                        "description": "An explanation about the purpose of this instance.",
                        "default": "",
                        "examples": [
                            "Draft"
                        ]
                    },
                    "requestCount": {
                        "$id": "#/items/anyOf/0/properties/requestCount",
                        "type": "integer",
                        "title": "The requestCount schema",
                        "description": "An explanation about the purpose of this instance.",
                        "default": 0,
                        "examples": [
                            0
                        ]
                    },
                    "participantCount": {
                        "$id": "#/items/anyOf/0/properties/participantCount",
                        "type": "integer",
                        "title": "The participantCount schema",
                        "description": "An explanation about the purpose of this instance.",
                        "default": 0,
                        "examples": [
                            1
                        ]
                    }
                },
                "additionalProperties": true
            }
        ]
    }
}

you can use :slight_smile:
JSON Schema Tool to create schema for response .

by default it allows additional properties , test won’t fail if extra fields are there fails only if fields are missing. You can click settings and unselect additional properties.

The above schema will fail for additional proeprties also

1 Like

Thanks @praveendvd
Busy at work this week so haven’t got a chance to try this out. Will let you know when I do!