json模式不适用于空响应主体

时间:2016-03-25 03:49:01

标签: javascript json schema postman tv4

我已经编写了一个模式来验证响应主体。并将所有项目设置为“必需”。但是当身体返回空阵列时,它直到PASS,这应该是失败的。这样的架构:

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);

响应如下:

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

1 个答案:

答案 0 :(得分:0)

您应该使用以下内容排除空数组:

"type": "array",
"minItems": 1
"items": {
  "$ref": "#/definitions/MyObject"
}