jsonschemapostmantv4

Fail to validate schema and correctly use additionalProperties


I'm trying to validate my JSON schema and use the additionalProperties: false to confirm there is not other properties. My responseBody look like this:

[
  {
    "id": 1234567890987654,
    "email": "eemail@domain.com",
    "civility": 0,
    "firstname": "john",
    "lastname": "do",
    "function": null,
    "phone": null,
    "cellphone": null,
    "role": 1,
    "passwordws": "jdnfjnshn55fff5g8"
  },
  {
...}
]

In the postman tests, I add this

var schema = {
    "type": "array",
    "properties": {
        "id": {"type":"number"},
        "email": {"type":"string"},
        "civility": {"type":"number"},
        "firstname": {"type":"string"},
        "lastname": {"type":"string"},
        "function": {"type":"string"},
        "cellphone": {"type":"string"},
        "role": {"type":"number"},
        "passwordws": {"type":"string"},
    },
    "additionalProperties": false,
    "required": ["id", "email", "civility", "role", "passwordws"]
};

var data = JSON.parse(responseBody);
var result = tv4.validateResult(data, schema);
tests["Valid schema"] = result.valid;

the test should return FAIL because I deleted the "phone" properties from schema, but test still run valid... I tried to change the schema to {type:array, properties: {type: object, properties {list of properties}additionalProperties: false}} but test still return PASS instead of FAIL... any idea ?


Solution

  • After some test, and log the result, the error was due to the null value I received sometimes in objects. I changed the schema you sent me

    {
        "type": "array",
        "items": {
            "$ref": "#/definitions/MyObject"
        },
    
        "definitions": {
            "MyObject": {
                "type": "object",
                "required": ["id", "email", "civility", "role", "passwordws"],
                "properties": {
                    "id": {
                        "type": "integer"
                    },
                    "email": {
                        "type": "string"
                    },
                    "civility": {
                        "type": "integer"
                    },
                    "firstname": {
                        "type": ["string", "null"]
                    },
                    "lastname": {
                        "type": ["string", "null"]
                    },
                    "function": {
                        "type": ["string", "null"]
                    },
                    "phone": {
                        "type": ["string", "null"]
                    },
                    "cellphone": {
                        "type": ["string", "null"]
                    },
                    "role": {
                        "type": "integer"
                    },
                    "passwordws": {
                        "type": "string"
                    }
                },
                "additionalProperties": false
            }
        }
    };
    

    and I'm now able to validate correctly the schema.

    Many thanks