-
Notifications
You must be signed in to change notification settings - Fork 3
/
schema.js
396 lines (328 loc) · 7.79 KB
/
schema.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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
'use strict';
var stringify = JSON.stringify;
/**
* Start a new schema validation.
*
* @constructor
* @api public
*/
function Schema () {
this.validations = [
// small handy helper function for type checking
'var toString = function (val) {'
, 'var rs = Object.prototype.toString.call(val);'
, 'return rs.slice(8, rs.length - 1).toLowerCase();'
, '};'
];
}
/**
* The value should be a string.
*
* @api public
*/
Schema.prototype.string = function string () {
this.validations.push('if (toString(value) !== "string") return false;');
return this;
};
/**
* The value should be a number.
*
* @param {Boolean} loose turn off strict validations
* @api public
*/
Schema.prototype.number = function number (loose) {
if (!loose) {
this.validations.push('if (toString(value) !== "number") return false;');
} else {
this.validations.push('if (toString(+value) !== "number") return false;');
}
return this;
};
/**
* The value should be an array.
*
* @api public
*/
Schema.prototype.array = function array () {
this.validations.push('if (toString(value) !== "array") return false;');
return this;
};
/**
* The value should be a object.
*
* @api public
*/
Schema.prototype.object = function object () {
this.validations.push('if (toString(value) !== "object") return false;');
return this;
};
/**
* The value should be a date.
*
* @api public
*/
Schema.prototype.date = function date () {
this.validations.push('if (toString(value) !== "date") return false;');
return this;
};
/**
* The value should be a regexp.
*
* @api public
*/
Schema.prototype.regexp = function regexp () {
this.validations.push('if (toString(value) !== "regexp") return false;');
return this;
};
/**
* The value should be a function.
*
* @api public
*/
Schema.prototype.function = function fn () {
this.validations.push('if (toString(value) !== "function") return false;');
return this;
};
/**
* The value should be undefined
*
* @api public
*/
Schema.prototype.undefined = function undef () {
this.validations.push('var undef; if (value !== undef) return false;');
return this;
};
/**
* The value should be null.
*
* @api public
*/
Schema.prototype.null = function nul () {
this.validations.push('if (value !== null) return false;');
return this;
};
/**
* The value should be a boolean.
*
* @api public
*/
Schema.prototype.boolean = function bool () {
this.validations.push('if (toString(value) !== "boolean") return false;');
return this;
};
/**
* The value should be true.
*
* @api public
*/
Schema.prototype.true = function truely () {
this.validations.push('if (value !== true) return false;');
return this;
};
/**
* The value should be false.
*
* @api public
*/
Schema.prototype.false = function falsely () {
this.validations.push('if (value !== false) return false;');
return this;
};
/**
* When only argument is given the length should be exact, if 2 arguments are
* given the length should be between those values
*
* @param {Number} amount
* @param {Number} maximum
* @api public
*/
Schema.prototype.length = function length (amount, maximum) {
if (!maximum) {
this.validations.push('if (value.length !== ' + amount + ') return false;');
} else {
this.validations.push('if (!(value.length >= ' + amount
+ ' && value.length <= ' + maximum + ')) return false;');
}
return this;
};
/**
* The value should be above the given amount.
*
* @param {Number} amount
* @api public
*/
Schema.prototype.above = function above (amount) {
this.validations.push('if (!(value > ' + amount + ')) return false;');
return this;
};
/**
* The value should be below the given amount.
*
* @param {Number} amount
* @api public
*/
Schema.prototype.below = function below (amount) {
this.validations.push('if (!(value < ' + amount + ')) return false;');
return this;
};
/**
* The value should be between low and high
*
* @param {Number} low
* @param {Number} high
* @api public
*/
Schema.prototype.between = function between (low, high) {
this.below(high);
this.above(low);
return this;
};
/**
* Loops over a array, and applies the pattern for each value in the array
*
* @param {Arguments} args Either arguments or a array
* @param {String} pattern The pattern that needs replacement %arg%
* @api private
*/
Schema.prototype.each = function each (args, pattern) {
var i = args.length;
while (i--) {
this.validations.push(pattern.replace(
/%\s?arg\s?%/g
, stringify(args[i])
));
}
return this;
};
/**
* The value should be either one of these, at least one should match.
*
* @api pubic
*/
Schema.prototype.either = function either () {
var args = arguments
, i = args.length
, statements = [];
while (i--) {
statements.push('value === ' + stringify(args[i]));
}
this.validations.push('if (!(' + statements.join(' || ') + ')) return false;');
return this;
};
/**
* The value should have these values
*
* @api public
*/
Schema.prototype.have = function have () {
this.each(arguments, 'if (value.indexOf(% arg %) === -1) return false;');
return this;
};
/**
* The values should not have these values
*
* @api public
*/
Schema.prototype.not = function not () {
this.each(arguments, 'if (value.indexOf(% arg %) !== -1) return false;');
return this;
};
/**
* If arguments are passed in the function it will esure that those values are
* unique in the array. If no options are given it will tests that all items in
* the array must be unique.
*
* @TODO
* @api public
*/
Schema.prototype.unique = function unique () {
this.each(arguments, [
'var counter = 0, position = value.indexOf(% arg %);'
, 'while (position !== -1) {'
, 'if (++counter > 1) return false;'
, 'position = value.indexOf(% arg %, position + 1);'
, '}'
].join(''));
return this;
};
/**
* The value should divide by this amount
*
* @api public
*/
Schema.prototype.dividesby = function devidesby (amount) {
this.validations.push('if (value % ' + amount + ' !== 0) return false;');
return this;
};
/**
* The value should equal the given value
*
* @param {Mixed} value
* @api public
*/
Schema.prototype.equal = function equal (value) {
this.validations.push('if (value !== ' + stringify(value) + ') return false;');
return this;
};
/**
* The value should match against this regular expression
*
* @param {RegExp} regex
* @api public
*/
Schema.prototype.match = function match (regex) {
this.validations.push('if (!'+ regex + '.test(value)) return false;');
return this;
};
/**
* The value should be lowercase.
*
* @api public
*/
Schema.prototype.lowercase = function lowercase () {
this.validations.push('if (value.toLowerCase() !== value) return false;');
return this;
};
/**
* The value should be uppercase.
*
* @api public
*/
Schema.prototype.uppercase = function uppercase () {
this.validations.push('if (value.toUpperCase() !== value) return false;');
return this;
};
/**
* The value should be a float.
*
* @api public
*/
Schema.prototype.float = function float () {
return this.dividesby(1);
};
/**
* This value is optional and not required, but when it exists we are going to
* validate against it.
*
* @api public
*/
Schema.prototype.optional = function optional () {
this.validations.push('if (!value) return true;');
return this;
};
/**
* This should be placed on the last line of the scheme just like you would
* with a regular expression. When the JS parser gets this property we are
* going to generate the assembled statements and compile one single validation
* function out of it.
*
* @api public
*/
Schema.prototype.__defineGetter__('$', function $ () {
this.validations.push('return true;');
return new Function('value', this.validations.join('\n'));
});
/**
* Expose the Schema.
*/
module.exports = Schema;