forked from lperrin/paperwork
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpaperwork.js
More file actions
164 lines (120 loc) · 3.6 KB
/
paperwork.js
File metadata and controls
164 lines (120 loc) · 3.6 KB
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
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.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) {
this.opt = spec;
}
function Multiple(specs) {
this.mult = specs;
}
function paperwork(spec, val, visitor) {
if (spec instanceof Optional)
return val ? paperwork(spec.opt, val, visitor) : null;
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 (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 (_.isFunction(spec))
return visitor.checkFun(val, spec, null);
if (_.isArray(spec)) {
if (spec.length !== 1)
throw new Error('array must contain exactly 1 sample value');
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) {
return fun.name || 'custom validator';
}
module.exports = function (spec, blob, done) {
var visitor = new Visitor(),
validated = paperwork(spec, blob, visitor);
if (visitor.hasErrors())
done(visitor.errors);
else
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(),
validated = paperwork(spec, req.body, visitor);
if (!visitor.hasErrors()) {
req.body = validated;
return next();
}
res.statusCode = 400;
var response = {
status: 'bad_request',
reason: 'Body did not satisfy requirements',
errors: visitor.errors
}
res.end(JSON.stringify({status: 'bad_request', reason: 'Body did not satisfy requirements', errors: visitor.errors}, null, ' '))
};
};
module.exports.optional = function (spec) {
return new Optional(spec);
};
module.exports.all = function () {
return new Multiple(Array.prototype.slice.call(arguments));
};