forked from Meteor-Community-Packages/meteor-simple-schema
-
Notifications
You must be signed in to change notification settings - Fork 0
/
simple-schema-context.js
516 lines (433 loc) · 15.9 KB
/
simple-schema-context.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
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
/*
* PUBLIC API
*/
SimpleSchemaValidationContext = function(ss) {
var self = this;
self._simpleSchema = ss;
self._schema = ss.schema();
self._schemaKeys = _.keys(self._schema);
self._invalidKeys = [];
//set up validation dependencies
self._deps = {};
self._depsAny = new Deps.Dependency;
_.each(self._schemaKeys, function(name) {
self._deps[name] = new Deps.Dependency;
});
};
//validates the object against the simple schema and sets a reactive array of error objects
SimpleSchemaValidationContext.prototype.validate = function(doc, options) {
var self = this;
options = _.extend({
modifier: false,
upsert: false
}, options || {});
var invalidKeys = doValidation(doc, options.modifier, options.upsert, null, self._simpleSchema);
//now update self._invalidKeys and dependencies
//note any currently invalid keys so that we can mark them as changed
//due to new validation (they may be valid now, or invalid in a different way)
var removedKeys = _.pluck(self._invalidKeys, "name");
//update
self._invalidKeys = invalidKeys;
//add newly invalid keys to changedKeys
var addedKeys = _.pluck(self._invalidKeys, "name");
//mark all changed keys as changed
var changedKeys = _.union(addedKeys, removedKeys);
_.each(changedKeys, function(name) {
var genericName = makeGeneric(name);
if (genericName in self._deps) {
self._deps[genericName].changed();
}
});
if (changedKeys.length) {
self._depsAny.changed();
}
// Return true if it was valid; otherwise, return false
return self._invalidKeys.length === 0;
};
//validates doc against self._schema for one key and sets a reactive array of error objects
SimpleSchemaValidationContext.prototype.validateOne = function(doc, keyName, options) {
var self = this;
options = _.extend({
modifier: false
}, options || {});
var invalidKeys = doValidation(doc, options.modifier, options.upsert, keyName, self._simpleSchema);
//now update self._invalidKeys and dependencies
//remove objects from self._invalidKeys where name = keyName
var newInvalidKeys = [];
for (var i = 0, ln = self._invalidKeys.length, k; i < ln; i++) {
k = self._invalidKeys[i];
if (k.name !== keyName) {
newInvalidKeys.push(k);
}
}
self._invalidKeys = newInvalidKeys;
//merge invalidKeys into self._invalidKeys
for (var i = 0, ln = invalidKeys.length, k; i < ln; i++) {
k = invalidKeys[i];
self._invalidKeys.push(k);
}
//mark key as changed due to new validation (they may be valid now, or invalid in a different way)
var genericName = makeGeneric(keyName);
if (genericName in self._deps) {
self._deps[genericName].changed();
}
self._depsAny.changed();
// Return true if it was valid; otherwise, return false
return !self._keyIsInvalid(keyName);
};
//reset the invalidKeys array
SimpleSchemaValidationContext.prototype.resetValidation = function() {
var self = this;
var removedKeys = _.pluck(self._invalidKeys, "name");
self._invalidKeys = [];
_.each(removedKeys, function(name) {
var genericName = makeGeneric(name);
if (genericName in self._deps) {
self._deps[genericName].changed();
}
});
};
SimpleSchemaValidationContext.prototype.isValid = function() {
var self = this;
self._depsAny.depend();
return !self._invalidKeys.length;
};
SimpleSchemaValidationContext.prototype.invalidKeys = function() {
var self = this;
self._depsAny.depend();
return self._invalidKeys;
};
SimpleSchemaValidationContext.prototype._keyIsInvalid = function(name, genericName) {
var self = this;
genericName = genericName || makeGeneric(name);
var specificIsInvalid = !!_.findWhere(self._invalidKeys, {name: name});
var genericIsInvalid = (genericName !== name) ? (!!_.findWhere(self._invalidKeys, {name: genericName})) : false;
return specificIsInvalid || genericIsInvalid;
};
SimpleSchemaValidationContext.prototype.keyIsInvalid = function(name) {
var self = this, genericName = makeGeneric(name);
self._deps[genericName].depend();
return self._keyIsInvalid(name, genericName);
};
SimpleSchemaValidationContext.prototype.keyErrorMessage = function(name) {
var self = this, genericName = makeGeneric(name);
self._deps[genericName].depend();
var errorObj = _.findWhere(self._invalidKeys, {name: name});
if (!errorObj) {
errorObj = _.findWhere(self._invalidKeys, {name: genericName});
}
return errorObj ? errorObj.message : "";
};
/*
* PRIVATE
*/
var doValidation = function(obj, isModifier, isUpsert, keyToValidate, ss) {
// First do some basic checks of the object, and throw errors if necessary
if (!_.isObject(obj)) {
throw new Error("The first argument of validate() or validateOne() must be an object");
}
if (isModifier) {
if (_.isEmpty(obj)) {
throw new Error("When the modifier option is true, validation object must have at least one operator");
} else {
var allKeysAreOperators = _.every(obj, function (v, k) {
return (k.substring(0, 1) === "$");
});
if (!allKeysAreOperators) {
throw new Error("When the modifier option is true, all validation object keys must be operators");
}
}
} else if (looksLikeModifier(obj)) {
throw new Error("When the validation object contains mongo operators, you must set the modifier option to true");
}
// If this is an upsert, add all the $setOnInsert keys to $set;
// since we don't know whether it will be an insert or update, we'll
// validate upserts as if they will be an insert.
// TODO It would be more secure to validate twice, once as
// an update and once as an insert, because $set validation does not
// consider missing required keys to be an issue.
if ("$setOnInsert" in obj) {
if (isUpsert) {
obj.$set = obj.$set || {};
obj.$set = _.extend(obj.$set, obj.$setOnInsert);
}
delete obj.$setOnInsert;
}
var invalidKeys = [];
// Validation function called for each affected key
function validate(val, affectedKey, affectedKeyGeneric, def, op) {
// Get the schema for this key, marking invalid if there isn't one.
if (!def) {
invalidKeys.push(errorObject("keyNotInSchema", affectedKey, val, def, ss));
return;
}
// Check for missing required values. The general logic is this:
// * If there's no operator, or if the operator is $set and it's an upsert,
// val must not be undefined, null, or an empty string.
// * If there is an operator other than $unset or $rename, val must
// not be null or an empty string, but undefined is OK.
// * If the operator is $unset or $rename, it's invalid.
if (!def.optional) {
if (op === "$unset" || op === "$rename" || isBlankNullOrUndefined(val)) {
invalidKeys.push(errorObject("required", affectedKey, null, def, ss));
return;
}
}
// For $rename, make sure that the new name is allowed by the schema
if (op === "$rename" && typeof val === "string" && !ss.allowsKey(val)) {
invalidKeys.push(errorObject("keyNotInSchema", val, null, null, ss));
return;
}
// Value checks are not necessary for null or undefined values,
// or for certain operators.
if (!_.contains(["$unset", "$rename", "$pull", "$pullAll", "$pop"], op)) {
if (isSet(val)) {
// Check that value is of the correct type
var typeError = doTypeChecks(def, val, op);
if (typeError) {
invalidKeys.push(errorObject(typeError, affectedKey, val, def, ss));
return;
}
// Check value against allowedValues array
if (def.allowedValues && !_.contains(def.allowedValues, val)) {
invalidKeys.push(errorObject("notAllowed", affectedKey, val, def, ss));
return;
}
}
// Check value using valusIsAllowed function
if (def.valueIsAllowed && !def.valueIsAllowed(val, obj, op)) {
invalidKeys.push(errorObject("notAllowed", affectedKey, val, def, ss));
return;
}
}
// Perform custom validation
_.every(ss._validators, function(validator) {
var errorType = validator(affectedKeyGeneric, val, def, op);
if (typeof errorType === "string") {
invalidKeys.push(errorObject(errorType, affectedKey, val, def, ss));
return false;
}
return true;
});
}
// The recursive function
function checkObj(val, affectedKey, operator, adjusted) {
var affectedKeyGeneric, def;
// Adjust for first-level modifier operators
if (!operator && affectedKey && affectedKey.substring(0, 1) === "$") {
operator = affectedKey;
affectedKey = null;
}
if (affectedKey) {
// Adjust for $push and $addToSet
if (! adjusted && (operator === "$push" || operator === "$addToSet")) {
// Adjust for $each
// We can simply jump forward and pretend like the $each array
// is the array for the field. This has the added benefit of
// skipping past any $slice, which we also don't care about.
if (isBasicObject(val) && "$each" in val) {
val = val.$each;
} else {
affectedKey = affectedKey + ".0";
}
adjusted = true;
}
// Make a generic version of the affected key, and use that
// to get the schema for this key.
affectedKeyGeneric = makeGeneric(affectedKey);
def = ss.schema(affectedKeyGeneric);
// Perform validation for this key
if (!keyToValidate || keyToValidate === affectedKey || keyToValidate === affectedKeyGeneric) {
validate(val, affectedKey, affectedKeyGeneric, def, operator);
}
}
// Temporarily convert missing objects to empty objects
// so that the looping code will be called and required
// descendent keys can be validated.
if ((val === void 0 || val === null) && (!def || (def.type === Object && !def.optional))) {
val = {};
}
// Loop through arrays
if (_.isArray(val)) {
_.each(val, function(v, i) {
checkObj(v, affectedKey + '.' + i, operator, adjusted);
});
}
// Loop through object keys
else if (isBasicObject(val)) {
// Get list of present keys
var presentKeys = _.keys(val);
// For required checks, we want to also loop through all keys expected
// based on the schema, in case any are missing.
var requiredKeys, valueIsAllowedKeys;
if (!isModifier || (isUpsert && operator === "$set") || (affectedKeyGeneric && affectedKeyGeneric.slice(-2) === ".$")) {
requiredKeys = ss.requiredObjectKeys(affectedKeyGeneric);
// Filter out required keys that are ancestors
// of those in $set
requiredKeys = _.filter(requiredKeys, function (k) {
return !_.some(presentKeys, function (pk) {
return (pk.slice(0, k.length + 1) === k + ".");
});
});
}
if (!isModifier || (operator === "$set") || (affectedKeyGeneric && affectedKeyGeneric.slice(-2) === ".$")) {
// We want to be sure to call any present valueIsAllowed functions
// even if the value isn't set, so they can be used for custom
// required errors, such as basing it on another field's value.
valueIsAllowedKeys = ss.valueIsAllowedObjectKeys(affectedKeyGeneric);
}
// Merge the lists
var keysToCheck = _.union(presentKeys, requiredKeys || [], valueIsAllowedKeys || []);
// Check all keys in the merged list
_.each(keysToCheck, function(key) {
if (shouldCheck(key)) {
checkObj(val[key], appendAffectedKey(affectedKey, key), operator, adjusted);
}
});
}
}
// Kick off the validation
checkObj(obj);
// Make sure there is only one error per fieldName
var addedFieldNames = [];
invalidKeys = _.filter(invalidKeys, function(errObj) {
if (!_.contains(addedFieldNames, errObj.name)) {
addedFieldNames.push(errObj.name);
return true;
}
return false;
});
return invalidKeys;
};
var doTypeChecks = function(def, keyValue, op) {
var expectedType = def.type;
// If min/max are functions, call them
var min = def.min;
var max = def.max;
if (typeof min === "function") {
min = min();
}
if (typeof max === "function") {
max = max();
}
// String checks
if (expectedType === String) {
if (typeof keyValue !== "string") {
return "expectedString";
} else if (max !== null && max < keyValue.length) {
return "maxString";
} else if (min !== null && min > keyValue.length) {
return "minString";
} else if (def.regEx instanceof RegExp && !def.regEx.test(keyValue)) {
return "regEx";
} else if (_.isArray(def.regEx)) {
var regExError;
_.every(def.regEx, function(re, i) {
if (!re.test(keyValue)) {
regExError = "regEx." + i;
return false;
}
return true;
});
if (regExError)
return regExError;
}
}
// Number checks
else if (expectedType === Number) {
if (typeof keyValue !== "number") {
return "expectedNumber";
} else if (op !== "$inc" && max !== null && max < keyValue) {
return "maxNumber";
} else if (op !== "$inc" && min !== null && min > keyValue) {
return "minNumber";
} else if (!def.decimal && keyValue.toString().indexOf(".") > -1) {
return "noDecimal";
}
}
// Boolean checks
else if (expectedType === Boolean) {
if (typeof keyValue !== "boolean") {
return "expectedBoolean";
}
}
// Object checks
else if (expectedType === Object) {
if (!isBasicObject(keyValue)) {
return "expectedObject";
}
}
// Array checks
else if (expectedType === Array) {
if (!_.isArray(keyValue)) {
return "expectedArray";
} else if (def.minCount !== null && keyValue.length < def.minCount) {
return "minCount";
} else if (def.maxCount !== null && keyValue.length > def.maxCount) {
return "maxCount";
}
}
// Constructor function checks
else if (expectedType instanceof Function || safariBugFix(expectedType)) {
// Generic constructor checks
if (!(keyValue instanceof expectedType)) {
return "expectedConstructor";
}
// Date checks
else if (expectedType === Date) {
if (_.isDate(min) && min.getTime() > keyValue.getTime()) {
return "minDate";
} else if (_.isDate(max) && max.getTime() < keyValue.getTime()) {
return "maxDate";
}
}
}
};
/*
* HELPERS
*/
var appendAffectedKey = function(affectedKey, key) {
if (key === "$each") {
return affectedKey;
} else {
return (affectedKey ? affectedKey + "." + key : key);
}
};
var shouldCheck = function(key) {
if (key === "$pushAll") {
throw new Error("$pushAll is not supported; use $push + $each");
}
return !_.contains(["$pull", "$pullAll", "$pop", "$slice"], key);
};
var isBlank = function(str) {
if (typeof str !== "string") {
return false;
}
return (/^\s*$/).test(str);
};
var isBlankNullOrUndefined = function(str) {
return (str === void 0 || str === null || isBlank(str));
};
var errorObject = function(errorType, keyName, keyValue, def, ss) {
return {name: keyName, type: errorType, message: ss.messageForError(errorType, keyName, def, keyValue)};
};
// Tests whether it's an Object as opposed to something that inherits from Object
var isBasicObject = function(obj) {
return _.isObject(obj) && Object.getPrototypeOf(obj) === Object.prototype;
};
// The latest Safari returns false for Uint8Array, etc. instanceof Function
// unlike other browsers.
var safariBugFix = function(type) {
return (typeof Uint8Array !== "undefined" && type === Uint8Array)
|| (typeof Uint16Array !== "undefined" && type === Uint16Array)
|| (typeof Uint32Array !== "undefined" && type === Uint32Array)
|| (typeof Uint8ClampedArray !== "undefined" && type === Uint8ClampedArray);
};
var isSet = function(val) {
return val !== void 0 && val !== null;
};
var makeGeneric = function(name) {
if (typeof name !== "string")
return null;
return name.replace(/\.[0-9]+\./g, '.$.').replace(/\.[0-9]+/g, '.$');
};