0

I have write one schema to validate the response body. And set all the items as "required". But when the body return empty array, it till PASS, which supposed should be FAIL. Schema like this:

var schema = {
  "$schema": "http://json-schema.org/draft-04/schema#",
  "type": "array",
  "items": {
    "$ref": "#/definitions/MyObject"
  },
  "definitions": {
    "MyObject": {
      "type": ["object"],
      "properties": {
        "transactionId": "integer",
        "transactionType": "string",
        "bpCode": "string",
        "bpId": "string",
        "postingDate ": "string",
        "dueDate": "string",
        "totalAmount": "number",
        "balanceDue": "number",
        "reconcileAmount": "number",
        "debitCredit": "string",
        "remarks": "string",
      },
      "required": ["transactionId", "transactionType", "bpCode", "bpId", "postingDate", "dueDate", "totalAmount", "balanceDue", "reconcileAmount", "debitCredit", "remarks"],
      "additionalProperties": false
    }
  }
};

tests["Valid respong body schema"] = tv4.validate(data.body, schema);

The response like this:

{
  "errorCode": null,
  "errorMessage": null,
  "body": []
}

1 Answers1

0

You should exclude the empty array with:

"type": "array",
"minItems": 1
"items": {
  "$ref": "#/definitions/MyObject"
}
jens.klose
  • 351
  • 2
  • 8