Validate schema with array that is sometimes empty

I am trying to validate a JSON schema and I have hit a bit of a wall!

I started with RV4 and then inspired by Testing Properties and Arrays correctly in GET I moved to AJV.

My problem is that I want to validate an array that is sometimes legitimately empty. I can get the test passing, but when I change the type of one of the object keys in the array, the test still passes.

I have the following test:

const jsonData = pm.response.json();

pm.test('Schema is valid', function(){
var Ajv = require('ajv');
ajv = newAjv(),

schema = {
	"type": "object",
	"properties": {
		"products": {
			"type": "array",
			"items": [{
				"type": "object",
				"properties": {
					"price": {
						"type": "integer"
					},
					"stats": {
						"type": "array",
						"items": [{
							"type": "object",
							"properties": {
								"value": {
									"type": "integer"
								},
								"type": {
									"type": "integer"
								}
							},
							"required": ["value",
							"type"]
						}]
					},
					"images": {
						"type": "array",
						"items": [{
							"type": "object",
							"properties": {
								"file": {
									"type": "string"
								},
								"title": {
									"type": "string"
								},
								
							},
							"required": ["file",
							"title"]
						}]
					}
				},
				"required": ["price",
				"stats",
				"images"]
			}]
		}
	},
	"required": ["products"]
};

var validate = ajv.compile(schema);
var valid = validate(jsonData)
if(valid === false){
  console.log('Invalid: '+ ajv.errorsText(validate.errors));
}	
pm.expect(valid).to.be.true;
});

The array “stats” always has at least one object and I can change the key type e.g. from integer to string, and the test fails - great.

The array “images” does NOT always contain an item - an empty array here is expected. What I find is even when I have an object returned in the response that has objects in the array, changing the type of the key to trigger a failure still causes the test to pass:

  • Empty array = pass
  • Array with items and correct key types tested - pass
  • Array with items and incorrect key types tested - pass

What I really want to do is accept that an empty array is valid, but if an object is returned, validate that the specified schema is still correct.

Am I missing something here? I am at a complete loss!

I’m fairly new to the topic of JSON schema validation and do not know why your tests always pass, but I think I spot another issue in your schema. Please check how you define your images and stats in the items detail as you might need to remove the surrounding square brackets.

From:

"images": {
    "type": "array",
    "items": [{
        "type": "object",
        "properties": {
            "file": {
                "type": "string"
            },
            "title": {
                "type": "string"
            }
        },
        "required": ["file", "title"]
    }]
}

To:

"images": {
    "type": "array",
    "items": {
        "type": "object",
        "properties": {
            "file": {
                "type": "string"
            },
            "title": {
                "type": "string"
            }
        },
        "required": ["file", "title"]
    }
}

The square brackets are used for validating tuples according to the description in https://json-schema.org/understanding-json-schema/reference/array.html#tuple-validation . I understood that if you define the items as array, then must the element at the respective position match it. Any following item will be unvalidated. If you define items directly as an object, then must all items validate against this one definition.

Regarding your initial problem, it would be helpful to get an example of the JSON data you used.

Let me know if that was somehow helpful.