Testing Properties and Arrays correctly in GET

I managed to get something to work, after banging my head against a wall for hours.
I do agree this schema validation stuff may be a lot more than I can handle at the moment, so I may go back to what I had originally.

It’s been a learning experience though!

If you’re interested by the way, this is what I found would actually test Passes & fails correctly:

const jsonData = pm.response.json();

pm.test('Schema is valid', function() {
    var Ajv = require('ajv');
    ajv = new Ajv(),
    schema = { //Schema should contain an Array
        "type": "array",
        "items": {//In this Array there should be a Properties Object
                    "type": "object", //These are the properties within this object. This will test TYPE of Property.
                    "properties": {
                        "gameId": { "type": "integer" },
                        "name": { "type": "string" },
                        "description": { "type": "string" },
                        "minPlayers": { "type": "integer", "maximum": 1},
                        "maxPlayers": { "type": "integer", "maximum": 1},
                        "rating": {"type": "string" },
                        "online": {"type": "boolean" }
                        
            },"required" : ["gameId", "name", "description", "minPlayers", "maxPlayers", "rating", "online"]//Required will make sure that the Properties listed are present
        }
    };
    
    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;
  
});

Thank you

1 Like