-
Notifications
You must be signed in to change notification settings - Fork 96
/
output-validation-rule.js
135 lines (107 loc) · 3.15 KB
/
output-validation-rule.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
'use strict';
const assert = require('assert');
const Joi = require('joi');
const helpMsg = ' -> see: https://github.com/koajs/joi-router/#validating-output';
module.exports = OutputValidationRule;
function OutputValidationRule(status, spec) {
assert(status, 'OutputValidationRule: missing status param');
assert(spec, 'OutputValidationRule: missing spec param');
this.ranges = status.split(',').map(trim).filter(Boolean).map(rangify);
assert(this.ranges.length > 0, 'invalid status code: ' + status + helpMsg);
this.status = status;
this.spec = spec;
this.validateSpec();
}
/**
* Validates it's input values
*
* @throws Error
*/
OutputValidationRule.prototype.validateSpec = function validateSpec() {
assert(this.spec.body || this.spec.headers, 'output validation key: ' +
this.status + ' must have either a body or headers validator specified');
};
OutputValidationRule.prototype.toString = function toString() {
return this.status;
};
/**
* Determines if this rule has overlapping logic
* with `ruleB`.
*
* @returns Boolean
*/
OutputValidationRule.prototype.overlaps = function overlaps(ruleB) {
return OutputValidationRule.overlaps(this, ruleB);
};
/**
* Checks if this rule should be run against the
* given `ctx` response data.
*
* @returns Boolean
*/
OutputValidationRule.prototype.matches = function matches(ctx) {
for (let i = 0; i < this.ranges.length; ++i) {
const range = this.ranges[i];
if (ctx.status >= range.lower && ctx.status <= range.upper) {
return true;
}
}
return false;
};
/**
* Validates this rule against the given `ctx`.
*/
OutputValidationRule.prototype.validateOutput = function validateOutput(ctx) {
let result;
if (this.spec.headers) {
result = Joi.compile(this.spec.headers).validate(ctx.response.headers);
if (result.error) return result.error;
// use casted values
ctx.set(result.value);
}
if (this.spec.body) {
result = Joi.compile(this.spec.body).validate(ctx.body);
if (result.error) return result.error;
// use casted values
ctx.body = result.value;
}
};
// static
/**
* Determines if ruleA has overlapping logic
* with `ruleB`.
*
* @returns Boolean
*/
OutputValidationRule.overlaps = function overlaps(a, b) {
/* eslint-disable prefer-arrow-callback */
return a.ranges.some(function checkRangeA(rangeA) {
return b.ranges.some(function checkRangeB(rangeB) {
if (rangeA.upper >= rangeB.lower && rangeA.lower <= rangeB.upper) {
return true;
}
return false;
});
});
/* eslint-enable prefer-arrow-callback */
};
// helpers
function trim(s) {
return s.trim();
}
function rangify(rule) {
if (rule === '*') {
return { lower: 0, upper: Infinity };
}
const parts = rule.split('-');
assert(parts.length && parts.length < 3, 'invalid status code: ' + rule + helpMsg);
const lower = parts[0];
const upper = parts.length === 2 ? parts[1] : lower;
validateCode(lower);
validateCode(upper);
return { lower: parseInt(lower, 10), upper: parseInt(upper, 10) };
}
function validateCode(code) {
assert(/^[1-5][0-9]{2}$/.test(code), 'invalid status code: ' + code +
' must be between 100-599');
}