-
Notifications
You must be signed in to change notification settings - Fork 22
/
validate.js
62 lines (51 loc) · 1.56 KB
/
validate.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
const { HttpRequest, ExpectedHttpRequest } = require('./model/http-request');
const { HttpResponse, ExpectedHttpResponse } = require('./model/http-response');
function proxy(validatableObject, method, cb) {
let result;
try {
result = validatableObject[method]();
} catch (error) {
return cb(error, null);
}
return cb(null, result);
}
/**
* @param {string} real
* @param {string} expected
* @param {'request'|'response'} type
*/
function getValidatableObject(real, expected, type) {
let request;
let response;
switch (type) {
case 'request':
request = new HttpRequest(real);
request.expected = new ExpectedHttpRequest(expected);
return request;
case 'response':
response = new HttpResponse(real);
response.expected = new ExpectedHttpResponse(expected);
return response;
default:
throw new Error(
`Can't validate: expected HTTP message type to be "request" or "response", but got: ${type}.`
);
}
}
function isValid(real, expected, type, cb) {
console.warn(`\
Usage of "isValid" is deprecated and will be removed in the future releases of Gavel.
Use "isValid" property of the validation result instead:
const { validate } = require('gavel');
const result = validate(real, expected, 'request');
const { isValid } = result;\
`);
return proxy(getValidatableObject(real, expected, type), 'isValid', cb);
}
function isValidatable(real, expected, type, cb) {
return proxy(getValidatableObject(real, expected, type), 'isValidatable', cb);
}
module.exports = {
isValid,
isValidatable
};