-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathpaperwork.js
228 lines (166 loc) · 4.96 KB
/
paperwork.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
var _ = require('underscore');
function Visitor(path, errors) {
this.path = path || 'body';
this.errors = errors || [];
}
Visitor.prototype.enter = function(elem) {
return new Visitor(this.path + '.' + elem, this.errors);
};
Visitor.prototype.clone = function (elem) {
return new Visitor(this.path + '.' + elem);
};
Visitor.prototype.enterArray = function(i) {
return new Visitor(this.path + '[' + i + ']', this.errors);
};
Visitor.prototype.report = function (reason) {
this.errors.push(this.path + ': ' + reason);
return null;
};
Visitor.prototype.checkType = function (val, type) {
if (typeof(val) === type)
return val;
this.report('should be a ' + type);
return null;
};
Visitor.prototype.checkFun = function (val, fun, reason) {
if (fun(val))
return val;
this.report(reason || 'failed ' + getFunctionName(fun));
return null;
};
Visitor.prototype.hasErrors = function () {
return this.errors.length > 0;
};
function Optional(spec, defaultVal) {
this.opt = spec;
this.defaultVal = !_.isUndefined(defaultVal) ? defaultVal : null;
}
function Multiple(specs) {
this.mult = specs;
}
function Or(specs) {
this.or = specs;
}
function paperwork(spec, val, visitor) {
if (spec instanceof Optional)
return !_.isUndefined(val) && !_.isNull(val) ? paperwork(spec.opt, val, visitor) : spec.defaultVal;
if (val === undefined)
return visitor.report('missing');
if (_.isRegExp(spec)) {
if (typeof(val) !== 'string')
return visitor.report('should be a string');
if (!spec.test(val))
return visitor.report('should match ' + spec);
return val;
}
if (_.isString(spec)) {
if (typeof(val) !== 'string')
return visitor.report('should be a string');
if (spec !== val)
return visitor.report('should eql ' + spec);
return val;
}
if (spec === String)
return visitor.checkType(val, 'string');
if (spec === Boolean)
return visitor.checkType(val, 'boolean');
if (spec === Number)
return visitor.checkType(val, 'number');
if (spec === Array)
return visitor.checkType(val, _.isArray, 'should be an array');
if (spec instanceof Multiple) {
var allGood = _.all(spec.mult, function (spec) {
return paperwork(spec, val, visitor) !== null;
});
return allGood ? val : null;
}
if (spec instanceof Or) {
var cleaned = null;
var anyGood = _.any(spec.or, function (spec) {
var subvisitor = visitor.clone('or');
cleaned = paperwork(spec, val, subvisitor);
return !subvisitor.hasErrors();
});
if (anyGood)
return cleaned;
else {
visitor.report('should match at least 1 condition');
return null;
}
}
if (_.isFunction(spec))
return visitor.checkFun(val, spec, null);
if (_.isArray(spec)) {
var itemSpec = spec[0];
if (!visitor.checkFun(val, _.isArray, 'should be an array'))
return null;
return _(val).map(function (item, i) {
return paperwork(itemSpec, item, visitor.enterArray(i));
});
}
if (_.isObject(spec)) {
if (!visitor.checkFun(val, _.isObject, 'should be an object'))
return null;
var res = {};
_(spec).each(function (subspec, field) {
res[field] = paperwork(subspec, val[field], visitor.enter(field));
});
return res;
}
}
function getFunctionName(fun) {
var ret = fun.toString();
ret = ret.substr('function '.length);
ret = ret.substr(0, ret.indexOf('('));
return ret;
}
var paperworkEval = module.exports = function (spec, blob, done) {
var visitor = new Visitor(),
cleaned = paperwork(spec, blob, visitor);
if (visitor.hasErrors())
done(visitor.errors);
else
done(null, cleaned);
};
module.exports.wrapHttp = function (spec, blob, done) {
paperworkEval(spec, blob, function (errors, validated) {
if (errors)
return done({status: 'bad_request', reason: 'Body did not satisfy requirements.', errors: errors});
done(null, validated);
});
};
module.exports.accept = function (spec) {
return function (req, res, next) {
if (!req.body)
throw new Error('express.bodyParser() not enabled');
var visitor = new Visitor(),
cleaned = paperwork(spec, req.body, visitor);
if (!visitor.hasErrors()) {
req.body = cleaned;
return next();
}
var error = {
status: 'bad_request',
reason: 'Body did not satisfy requirements',
errors: visitor.errors
};
if (res.locals && res.locals.sendError)
return res.locals.sendError(error);
res.status(400).send(error);
};
};
module.exports.validator = function (spec) {
return module.exports.bind(null, spec);
};
module.exports.optional = function (spec, defaultVal) {
return new Optional(spec, defaultVal);
};
module.exports.all = function () {
return new Multiple(Array.prototype.slice.call(arguments));
};
module.exports.or = function () {
return new Or(Array.prototype.slice.call(arguments));
};
module.exports.any = function () {
return new Or(Array.prototype.slice.call(arguments));
};