-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
408 lines (348 loc) · 11.1 KB
/
index.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
397
398
399
400
401
402
403
404
405
406
407
/*
Running documentation of ObjectSchema
possible settings per field:
----------------------------
ignored: the field and value will be silently dropped
required: must not be undefined - specifiy more settings to expand on what it
should be to pass
type: the type of object - i.e. the text string from typeof [object]
instanceOf: check that the value is an instance of the object in instanceOf
filters: filters to run on the value, before validating. Can be:
text string - will check if it's a native string function or if
ObjectSchema has such a filter built in
function - any custome function, that takes a value, manipulates it
and returns the result.
in: the value should exist in the given array.
default: a default value to set, if the field has no value.
regex: a regular expression to match
*/
var ObjectId = require('mongodb').ObjectID;
var DBRef = require('mongodb').DBRef;
require('extend-string');
var clone = require('clone');
/**
* @todo Validate the schema - e.g. required and ignored not set on the same field
*/
var ObjectSchema = function(schemaDefinition, options) {
if (options && options.strictness) {
this.strictness = options.strictness;
}
this.definition = schemaDefinition;
};
module.exports = ObjectSchema;
/**
* @todo A schema settings validator is needed here...
*/
ObjectSchema.prototype.setField = function(field, settings) {
this.definition[field] = settings;
};
ObjectSchema.prototype.setFields = function(fields) {
for (var field in fields) {
this.definition[field] = fields[field];
}
};
ObjectSchema.prototype.mergeFields = function(fields) {
for (var field in fields) {
this.addSettings(field, fields[field]);
}
this.definition = this._mergeObjects(this.definition, fields);
};
ObjectSchema.prototype.addSettings = function(field, settings) {
if (!this.definition[field]) {
this.definition[field] = settings;
} else {
this.definition[field] = this._mergeObjects(this.definition[field], settings);
}
};
/**
* Schema strictness
* Possible values are:
* strict: err on fields not defined in the schema
* relaxed: ignore fields not defined in the schema
* loose: let fields through, when they're not defined in the schema
*/
ObjectSchema.prototype.setStrictness = function(strictness) {
this.strictness = strictness || 'strict'; // strict | relaxed | loose
if (['strict', 'relaxed', 'loose'].indexOf(this.strictness) === -1) {
throw new Error('You need to supply a valid strictness or leave as strict');
}
};
/**
* Not all validators are validators - some are manipulators...
* Those should be run first, as they can change a value.
* We don't want to pass a value, then change it to a non-passable value.
*/
ObjectSchema.prototype.validate = function(dataObject, errors, callback) {
this.lastErrors = [];
if (arguments.length < 3) {
if (typeof errors === 'function') {
callback = errors;
errors = [];
}
}
errors = errors || [];
if (!dataObject) {
callback([new Error('No dataObject supplied')], null);
return false;
}
var testObject = clone(dataObject);
if (testObject instanceof Array) var result = [];
else var result = {};
for (var field in this.definition) {
var definition = this.definition[field];
if (field === '*') {
for (var dataField in testObject) {
var res = this.runValidations(dataField, definition, testObject, errors);
if (res !== undefined) {
result[dataField] = res
}
}
} else {
var res = this.runValidations(field, definition, testObject, errors);
if (res !== undefined) {
result[field] = res
}
}
}
;
/** Run through the fields that are not in the schema */
for (var field in testObject) {
if (!this.definition[field] && !this.definition['*']) {
if (this.strictness === 'strict') {
errors.push({
field: field,
error: 'field not in definition'
});
} else if (this.strictness === 'loose') {
result[field] = testObject[field];
}
}
}
if (errors.length) {
this.lastErrors = errors;
}
/** If there is anything defined as an error - don't return the result! */
if (this.strictness === 'strict' && errors.length) {
result = false;
}
if (!errors.length) {
errors = null;
}
delete testObject;
if (callback) {
callback(errors, result);
}
return result;
};
/** This is a seperated form validate, in order to only having it once */
ObjectSchema.prototype.runValidations = function(field, definition, testObject, errors) {
var passed = true;
if (definition.ignored) {
return;
}
if ((this.strictness !== 'strict' || definition.optional) &&
!definition.required &&
(!testObject || typeof testObject[field] === 'undefined')) {
if (typeof definition.default !== 'undefined') return definition.default;
else return;
}
if (definition.objectSchema && typeof definition.objectSchema === 'object') {
if (!(definition.objectSchema instanceof ObjectSchema)) {
definition.objectSchema = new ObjectSchema(definition.objectSchema);
}
var subErrors = [];
var result = definition.objectSchema.validate(testObject[field], subErrors);
if (!result) {
passed = false;
} else {
testObject[field] = result;
}
if (subErrors.length) {
for (var i in subErrors) {
subErrors[i].subField = subErrors[i].field;
subErrors[i].field = field;
errors.push(subErrors[i]);
}
}
}
/** Run theses before the tests, as these can render the value invalid */
if (definition.filters) {
this.definitionFilters(field, definition, testObject, errors);
}
if (typeof definition.default !== 'undefined') {
this.definitionDefault(field, definition, testObject, errors);
}
if (definition.required) {
if (!this.definitionRequired(field, definition, testObject, errors)) {
passed = false;
}
}
if (definition.in) {
if (!this.definitionIn(field, definition, testObject, errors)) {
passed = false;
}
}
if (definition.type) {
if (!this.definitionType(field, definition, testObject, errors)) {
passed = false;
}
}
if (definition.instanceOf && !definition.objectSchema) {
if (!this.definitionInstanceOf(field, definition, testObject, errors)) {
passed = false;
}
}
if (definition.regex) {
if (!this.definitionRegEx(field, definition, testObject, errors)) {
passed = false;
}
}
if (passed) {
return testObject[field];
}
};
/*************************\
* DEFINITION VALIDATORS *
\*************************/
ObjectSchema.prototype.definitionFilters = function(field, definition, testObject, errors) {
if (definition.filters) {
if (definition.filters && typeof definition.filters !== 'object') {
definition.filters = [definition.filters];
}
if (typeof definition.filters === 'object') {
for (var i in definition.filters) {
if (typeof definition.filters[i] === 'string') {
var filterName = definition.filters[i];
if (testObject[field][filterName] &&
typeof testObject[field][filterName] === 'function') {
testObject[field] = testObject[field][filterName]();
} else if (this['filter' + filterName.upperCaseFirst()] &&
typeof this['filter' + filterName.upperCaseFirst()] === 'function') {
testObject[field] = this['filter' + filterName.upperCaseFirst()](field, testObject);
}
} else if (typeof definition.filters[i] === 'function') {
testObject[field] = definition.filters[i](field, testObject);
}
}
}
}
};
/**
* What is an empty value?
* What it isn't: an empty value of the correct type...
*/
ObjectSchema.prototype.definitionDefault = function(field, definition, testObject, errors) {
/** If no value is found, even after running filters, set the default */
if (typeof definition.default !== 'undefined' && typeof testObject[field] === 'undefined') {
testObject[field] = definition.default;
}
};
ObjectSchema.prototype.definitionRequired = function(field, definition, testObject, errors) {
if (typeof testObject[field] === 'undefined') {
errors.push({
field: field,
error: 'does not exist'
});
return false;
}
return true;
};
ObjectSchema.prototype.definitionIn = function(field, definition, testObject, errors) {
if (typeof definition.in === 'object' && definition.in.length) {
if (definition.in.indexOf(testObject[field]) === -1) {
errors.push({
field: field,
error: 'value wasn\'t in array'
});
return false;
}
} else {
return false;
}
return true;
};
ObjectSchema.prototype.definitionType = function(field, definition, testObject, errors) {
if (typeof testObject[field] !== definition.type) {
errors.push({
field: field,
error: 'wrong type'
});
return false;
}
return true;
};
ObjectSchema.prototype.definitionInstanceOf = function(field, definition, testObject, errors) {
if (!(testObject[field] instanceof definition.instanceOf)) {
errors.push({
field: field,
error: 'incorrect instance type'
});
return false;
}
return true;
};
ObjectSchema.prototype.definitionRegEx = function(field, definition, testObject, errors) {
if (!testObject[field].match(definition.regex)) {
errors.push({
field: field,
value: testObject[field],
regex: definition.regex,
error: 'value does not match regex'
});
return false;
}
return true;
};
/***********\
* FILTERS *
\***********/
ObjectSchema.prototype.filterDate = function(field, dataObject) {
if (dataObject[field]) {
var date = new Date(dataObject[field]);
if (date.toString() !== 'Invalid Date') {
return date;
}
}
};
ObjectSchema.prototype.filterObjectID = ObjectSchema.prototype.filterObjectId = function(field, dataObject) {
var objectId = dataObject[field];
if (objectId instanceof ObjectId) {
return objectId;
}
if (objectId.constructor.name.match(/^ObjectI(d|D)$/)) {
return objectId;
}
if (ObjectId.isValid('' + objectId)) {
return new ObjectId('' + objectId);
}
return objectId;
};
ObjectSchema.prototype.filterDbref = ObjectSchema.prototype.filterDbRef = ObjectSchema.prototype.filterDBRef = function(field, dataObject) {
var dbRef = dataObject[field];
if (dbRef instanceof DBRef) {
return dbRef;
}
if (dbRef.constructor.name === 'DBRef') {
return dbRef;
}
try {
var namespace = dbRef.namespace || dbRef.collection || dbRef.$ref;
var oid = dbRef.oid || dbRef._id || dbRef.id || dbRef.$id;
var db = dbRef.db || dbRef.$db || dbRef.database || '';
oid = ObjectId('' + oid);
dbRef = new DBRef(namespace, oid, db);
} catch ( e ) {}
return dbRef;
};
/*************\
* UTILITIES *
\*************/
ObjectSchema.prototype._mergeObjects = function() {
var newObject = {};
for (var objectIndex in arguments) {
for (var objectKey in arguments[objectIndex]) {
newObject[objectKey] = arguments[objectIndex][objectKey];
}
}
return newObject;
};