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!