Schema validation ignoring array sub-fields type

Hi,

I have the following JSON response:

{
    "access": [
        {
            "id": 1,
            "client": "127.0.0.1",
            "apikey": "apikey1",
            "description": "Local call"
        },
        {
            "id": 2,
            "client": "::1",
            "apikey": "apikey2",
            "description": "Local call"
        },
        {
            "id": 3,
            "client": "192.168.0.100",
            "apikey": "apikey3",
            "description": "Remote call"
        }
    ]
}

And this JSON Schema:

var accessSchema = {
  "items": {
    "type": "object",
    "properties": {
      "access: { 
          "type": "array",
          "items": {
              "properties": {
                  "id": { "type": "string" },
                  "client": { "type": "string" },
                  "apikey": { "type": "string" },
                  "description": { "type": "**integer**" }
              }
          }
      }
    }
  }
}

As you can see, I’ve declared description (a string property) as an INTEGER just to get an error. But for my surprise, the following test is returning always TRUE:

    pm.test('Valid JSON schema', function() {
        pm.expect(tv4.validate(JSON.parse(responseBody), accessSchema)).to.be.true;
    });

What is wrong with my code?

Hey @fermani

Welcome to the community :wave:

Not sure what was happening for you locally but the JSON Schema you posted wasn’t valid JSON - It was missing a closing " at the end of the access key.

I’ve used a different Schema validation module that’s built-in to Postman, to help with this question.

It’s very similar to the tv4 module but is actively maintained.

I mocked out the response body from the question and this seems to be working (failing the test due to the wrong data type).

var Ajv = require('ajv'),
    ajv = new Ajv({logger: console}),
    accessSchema = {
        "properties": {
            "access": { 
                "type": "array",
                "items": {
                    "properties": {
                        "id": { "type": "string" },
                        "client": { "type": "string" },
                        "apikey": { "type": "string" },
                        "description": { "type": "integer" }
                      }
                  }
              }
        }
    }

pm.test('Schema is valid', function() {
    pm.expect(ajv.validate(accessSchema, pm.response.json())).to.be.true;
});  

When the schema data types are correct, the test is passing:

It worked, thanks.

I think the problem was my schema, I shouldn’t use the “items” property first. Just removed (after seeing your example) and it worked.

1 Like