我已经编写了一个模式来验证响应主体。并将所有项目设置为"必需"。但当主体返回空数组时,它会一直到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"
}