JSON 架构不适用于空响应正文



我已经编写了一个模式来验证响应主体。并将所有项目设置为"必需"。但当主体返回空数组时,它会一直到PASS,应该是FAIL。架构如下:

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": []
}

您应该使用以下命令排除空数组:

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

相关内容

  • 没有找到相关文章

最新更新