Below is my JSON schema
{
"$schema": "http://json-schema.org/draft-04/schema#",
"type": "array",
"items": [
{
"type": "object",
"properties": {
"id": {
"type": "integer"
},
"name": {
"type": "string"
},
"stations": {
"type": "array",
"items": [
{
"type": "object",
"properties": {
"id": {
"type": "integer"
},
"serial_number": {
"type": "string"
},
"name": {
"type": "string"
}
},
"required": [
"id",
"serial_number",
"name"
]
}
]
}
},
"required": [
"id",
"name",
"stations"
]
}
]
}
Below is the json to validate
[
{
"id": 1,
"name": "Test location",
"stations": [
{
"id": 1,
"serial_number": "TEST001",
"name": "TEST-STN!"
}
]
},
{
"id": 2,
"name": "Test location2"
}
]
Here the element "stations" is marked as required in schema, but it is missing in the second Item of json. still tv4 validation is passed.
What we really required is, it should fail the validation because the station element is missing in the second Item
The observation is IF station element is NOT present in any of the JSON Item, then validation is failing. But if the station element is present in one of the item then validation is passed
pm.test("Login Validation", function() { pm.expect(tv4.validate(pm.response.json(), pm.environment.get('schema.json'), true, true), tv4.error).to.be.true;});
I tried tv4 option "checkRecursive" with value both true and false...Still it passing the validation
Any help is appreciated
I think something like this would work for you and show the issue:
let schema = {
"type": "array",
"items": {
"type": "object",
"required": [
"id",
"name",
"stations"
],
"properties": {
"id": {
"type": "integer"
},
"name": {
"type": "string"
},
"stations": {
"type": "array",
"items": {
"type": "object",
"required": [
"id",
"serial_number",
"name"
],
"properties": {
"id": {
"type": "integer"
},
"serial_number": {
"type": "string"
},
"name": {
"type": "string"
}
}
}
}
}
}
}
pm.test("Check Schemat", () => {
pm.response.to.have.jsonSchema(schema)
})
I've included the jsonSchema()
Postman function as this uses AJV rather than the older and not currently maintained tv4 module.