forked from Automattic/mongoose
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathschematype.js
643 lines (578 loc) · 18 KB
/
schematype.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
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
/*!
* Module dependencies.
*/
var utils = require('./utils');
var CastError = require('./error').CastError;
var ValidatorError = require('./error').ValidatorError;
/**
* SchemaType constructor
*
* @param {String} path
* @param {Object} [options]
* @param {String} [instance]
* @api public
*/
function SchemaType (path, options, instance) {
this.path = path;
this.instance = instance;
this.validators = [];
this.setters = [];
this.getters = [];
this.options = options;
this._index = null;
this.selected;
for (var i in options) if (this[i] && 'function' == typeof this[i]) {
// { unique: true, index: true }
if ('index' == i && this._index) continue;
var opts = Array.isArray(options[i])
? options[i]
: [options[i]];
this[i].apply(this, opts);
}
};
/**
* Sets a default value for this SchemaType.
*
* ####Example:
*
* var schema = new Schema({ n: { type: Number, default: 10 })
* var M = db.model('M', schema)
* var m = new M;
* console.log(m.n) // 10
*
* Defaults can be either `functions` which return the value to use as the default or the literal value itself. Either way, the value will be cast based on its schema type before being set during document creation.
*
* ####Example:
*
* // values are cast:
* var schema = new Schema({ aNumber: Number, default: "4.815162342" })
* var M = db.model('M', schema)
* var m = new M;
* console.log(m.aNumber) // 4.815162342
*
* // default unique objects for Mixed types:
* var schema = new Schema({ mixed: Schema.Types.Mixed });
* schema.path('mixed').default(function () {
* return {};
* });
*
* // if we don't use a function to return object literals for Mixed defaults,
* // each document will receive a reference to the same object literal creating
* // a "shared" object instance:
* var schema = new Schema({ mixed: Schema.Types.Mixed });
* schema.path('mixed').default({});
* var M = db.model('M', schema);
* var m1 = new M;
* m1.mixed.added = 1;
* console.log(m1.mixed); // { added: 1 }
* var m2 = new M;
* console.log(m2.mixed); // { added: 1 }
*
* @param {Function|any} val the default value
* @return {defaultValue}
* @api public
*/
SchemaType.prototype.default = function (val) {
if (1 === arguments.length) {
this.defaultValue = typeof val === 'function'
? val
: this.cast(val);
return this;
} else if (arguments.length > 1) {
this.defaultValue = utils.args(arguments);
}
return this.defaultValue;
};
/**
* Declares the index options for this schematype.
*
* ####Example:
*
* var s = new Schema({ name: { type: String, index: true })
* var s = new Schema({ loc: { type: [Number], index: 'hashed' })
* var s = new Schema({ loc: { type: [Number], index: '2d', sparse: true })
* var s = new Schema({ loc: { type: [Number], index: { type: '2dsphere', sparse: true }})
* var s = new Schema({ date: { type: Date, index: { unique: true, expires: '1d' }})
* Schema.path('my.path').index(true);
* Schema.path('my.date').index({ expires: 60 });
* Schema.path('my.path').index({ unique: true, sparse: true });
*
* ####NOTE:
*
* _Indexes are created in the background by default. Specify `background: false` to override._
*
* [Direction doesn't matter for single key indexes](http://www.mongodb.org/display/DOCS/Indexes#Indexes-CompoundKeysIndexes)
*
* @param {Object|Boolean|String} options
* @return {SchemaType} this
* @api public
*/
SchemaType.prototype.index = function (options) {
this._index = options;
utils.expires(this._index);
return this;
};
/**
* Declares an unique index.
*
* ####Example:
*
* var s = new Schema({ name: { type: String, unique: true })
* Schema.path('name').index({ unique: true });
*
* _NOTE: violating the constraint returns an `E11000` error from MongoDB when saving, not a Mongoose validation error._
*
* @param {Boolean} bool
* @return {SchemaType} this
* @api public
*/
SchemaType.prototype.unique = function (bool) {
if (null == this._index || 'boolean' == typeof this._index) {
this._index = {};
} else if ('string' == typeof this._index) {
this._index = { type: this._index };
}
this._index.unique = bool;
return this;
};
/**
* Declares a sparse index.
*
* ####Example:
*
* var s = new Schema({ name: { type: String, sparse: true })
* Schema.path('name').index({ sparse: true });
*
* @param {Boolean} bool
* @return {SchemaType} this
* @api public
*/
SchemaType.prototype.sparse = function (bool) {
if (null == this._index || 'boolean' == typeof this._index) {
this._index = {};
} else if ('string' == typeof this._index) {
this._index = { type: this._index };
}
this._index.sparse = bool;
return this;
};
/**
* Adds a setter to this schematype.
*
* ####Example:
*
* function capitalize (val) {
* if ('string' != typeof val) val = '';
* return val.charAt(0).toUpperCase() + val.substring(1);
* }
*
* // defining within the schema
* var s = new Schema({ name: { type: String, set: capitalize }})
*
* // or by retreiving its SchemaType
* var s = new Schema({ name: String })
* s.path('name').set(capitalize)
*
* Setters allow you to transform the data before it gets to the raw mongodb document and is set as a value on an actual key.
*
* Suppose you are implementing user registration for a website. Users provide an email and password, which gets saved to mongodb. The email is a string that you will want to normalize to lower case, in order to avoid one email having more than one account -- e.g., otherwise, avenue@q.com can be registered for 2 accounts via avenue@q.com and AvEnUe@Q.CoM.
*
* You can set up email lower case normalization easily via a Mongoose setter.
*
* function toLower (v) {
* return v.toLowerCase();
* }
*
* var UserSchema = new Schema({
* email: { type: String, set: toLower }
* })
*
* var User = db.model('User', UserSchema)
*
* var user = new User({email: 'AVENUE@Q.COM'})
* console.log(user.email); // 'avenue@q.com'
*
* // or
* var user = new User
* user.email = 'Avenue@Q.com'
* console.log(user.email) // 'avenue@q.com'
*
* As you can see above, setters allow you to transform the data before it gets to the raw mongodb document and is set as a value on an actual key.
*
* _NOTE: we could have also just used the built-in `lowercase: true` SchemaType option instead of defining our own function._
*
* new Schema({ email: { type: String, lowercase: true }})
*
* Setters are also passed a second argument, the schematype on which the setter was defined. This allows for tailored behavior based on options passed in the schema.
*
* function inspector (val, schematype) {
* if (schematype.options.required) {
* return schematype.path + ' is required';
* } else {
* return val;
* }
* }
*
* var VirusSchema = new Schema({
* name: { type: String, required: true, set: inspector },
* taxonomy: { type: String, set: inspector }
* })
*
* var Virus = db.model('Virus', VirusSchema);
* var v = new Virus({ name: 'Parvoviridae', taxonomy: 'Parvovirinae' });
*
* console.log(v.name); // name is required
* console.log(v.taxonomy); // Parvovirinae
*
* @param {Function} fn
* @return {SchemaType} this
* @api public
*/
SchemaType.prototype.set = function (fn) {
if ('function' != typeof fn)
throw new TypeError('A setter must be a function.');
this.setters.push(fn);
return this;
};
/**
* Adds a getter to this schematype.
*
* ####Example:
*
* function dob (val) {
* if (!val) return val;
* return (val.getMonth() + 1) + "/" + val.getDate() + "/" + val.getFullYear();
* }
*
* // defining within the schema
* var s = new Schema({ born: { type: Date, get: dob })
*
* // or by retreiving its SchemaType
* var s = new Schema({ born: Date })
* s.path('born').get(dob)
*
* Getters allow you to transform the representation of the data as it travels from the raw mongodb document to the value that you see.
*
* Suppose you are storing credit card numbers and you want to hide everything except the last 4 digits to the mongoose user. You can do so by defining a getter in the following way:
*
* function obfuscate (cc) {
* return '****-****-****-' + cc.slice(cc.length-4, cc.length);
* }
*
* var AccountSchema = new Schema({
* creditCardNumber: { type: String, get: obfuscate }
* });
*
* var Account = db.model('Account', AccountSchema);
*
* Account.findById(id, function (err, found) {
* console.log(found.creditCardNumber); // '****-****-****-1234'
* });
*
* Getters are also passed a second argument, the schematype on which the getter was defined. This allows for tailored behavior based on options passed in the schema.
*
* function inspector (val, schematype) {
* if (schematype.options.required) {
* return schematype.path + ' is required';
* } else {
* return schematype.path + ' is not';
* }
* }
*
* var VirusSchema = new Schema({
* name: { type: String, required: true, get: inspector },
* taxonomy: { type: String, get: inspector }
* })
*
* var Virus = db.model('Virus', VirusSchema);
*
* Virus.findById(id, function (err, virus) {
* console.log(virus.name); // name is required
* console.log(virus.taxonomy); // taxonomy is not
* })
*
* @param {Function} fn
* @return {SchemaType} this
* @api public
*/
SchemaType.prototype.get = function (fn) {
if ('function' != typeof fn)
throw new TypeError('A getter must be a function.');
this.getters.push(fn);
return this;
};
/**
* Adds validator(s) for this document path.
*
* Validators always receive the value to validate as their first argument and must return `Boolean`. Returning false is interpreted as validation failure.
*
* ####Examples:
*
* function validator (val) {
* return val == 'something';
* }
*
* new Schema({ name: { type: String, validate: validator }});
*
* // with a custom error message
*
* var custom = [validator, 'validation failed']
* new Schema({ name: { type: String, validate: custom }});
*
* var many = [
* { validator: validator, msg: 'uh oh' }
* , { validator: fn, msg: 'failed' }
* ]
* new Schema({ name: { type: String, validate: many }});
*
* // or utilizing SchemaType methods directly:
*
* var schema = new Schema({ name: 'string' });
* schema.path('name').validate(validator, 'validation failed');
*
* ####Asynchronous validation:
*
* Passing a validator function that receives two arguments tells mongoose that the validator is an asynchronous validator. The second argument is an callback function that must be passed either `true` or `false` when validation is complete.
*
* schema.path('name').validate(function (value, respond) {
* doStuff(value, function () {
* ...
* respond(false); // validation failed
* })
* }, 'my error type');
*
* You might use asynchronous validators to retreive other documents from the database to validate against or to meet other I/O bound validation needs.
*
* Validation occurs `pre('save')` or whenever you manually execute [document#validate](#document_Document-validate).
*
* If validation fails during `pre('save')` and no callback was passed to receive the error, an `error` event will be emitted on your Models associated db [connection](#connection_Connection), passing the validation error object along.
*
* var conn = mongoose.createConnection(..);
* conn.on('error', handleError);
*
* var Product = conn.model('Product', yourSchema);
* var dvd = new Product(..);
* dvd.save(); // emits error on the `conn` above
*
* If you desire handling these errors at the Model level, attach an `error` listener to your Model and the event will instead be emitted there.
*
* // registering an error listener on the Model lets us handle errors more locally
* Product.on('error', handleError);
*
* @param {RegExp|Function|Object} obj validator
* @param {String} [error] optional error message
* @api public
*/
SchemaType.prototype.validate = function (obj, error) {
if ('function' == typeof obj || obj && 'RegExp' === obj.constructor.name) {
this.validators.push([obj, error]);
return this;
}
var i = arguments.length
, arg
while (i--) {
arg = arguments[i];
if (!(arg && 'Object' == arg.constructor.name)) {
var msg = 'Invalid validator. Received (' + typeof arg + ') '
+ arg
+ '. See http://mongoosejs.com/docs/api.html#schematype_SchemaType-validate';
throw new Error(msg);
}
this.validate(arg.validator, arg.msg);
}
return this;
};
/**
* Adds a required validator to this schematype.
*
* ####Example:
*
* var s = new Schema({ born: { type: Date, required: true })
* // or
* Schema.path('name').required(true);
*
*
* @param {Boolean} required enable/disable the validator
* @return {SchemaType} this
* @api public
*/
SchemaType.prototype.required = function (required) {
var self = this;
function __checkRequired (v) {
// in here, `this` refers to the validating document.
// no validation when this path wasn't selected in the query.
if ('isSelected' in this &&
!this.isSelected(self.path) &&
!this.isModified(self.path)) return true;
return self.checkRequired(v, this);
}
if (false === required) {
this.isRequired = false;
this.validators = this.validators.filter(function (v) {
return v[0].name !== '__checkRequired';
});
} else {
this.isRequired = true;
this.validators.push([__checkRequired, 'required']);
}
return this;
};
/**
* Gets the default value
*
* @param {Object} scope the scope which callback are executed
* @param {Boolean} init
* @api private
*/
SchemaType.prototype.getDefault = function (scope, init) {
var ret = 'function' === typeof this.defaultValue
? this.defaultValue.call(scope)
: this.defaultValue;
if (null !== ret && undefined !== ret) {
return this.cast(ret, scope, init);
} else {
return ret;
}
};
/**
* Applies setters
*
* @param {Object} value
* @param {Object} scope
* @param {Boolean} init
* @api private
*/
SchemaType.prototype.applySetters = function (value, scope, init, priorVal) {
if (SchemaType._isRef(this, value, scope, init)) {
return init
? value
: this.cast(value, scope, init, priorVal);
}
var v = value
, setters = this.setters
, len = setters.length
if (!len) {
if (null === v || undefined === v) return v;
return this.cast(v, scope, init, priorVal)
}
while (len--) {
v = setters[len].call(scope, v, this);
}
if (null === v || undefined === v) return v;
// do not cast until all setters are applied #665
v = this.cast(v, scope, init, priorVal);
return v;
};
/**
* Applies getters to a value
*
* @param {Object} value
* @param {Object} scope
* @api private
*/
SchemaType.prototype.applyGetters = function (value, scope) {
if (SchemaType._isRef(this, value, scope, true)) return value;
var v = value
, getters = this.getters
, len = getters.length;
if (!len) {
return v;
}
while (len--) {
v = getters[len].call(scope, v, this);
}
return v;
};
/**
* Sets default `select()` behavior for this path.
*
* Set to `true` if this path should always be included in the results, `false` if it should be excluded by default. This setting can be overridden at the query level.
*
* ####Example:
*
* T = db.model('T', new Schema({ x: { type: String, select: true }}));
* T.find(..); // field x will always be selected ..
* // .. unless overridden;
* T.find().select('-x').exec(callback);
*
* @param {Boolean} val
* @api public
*/
SchemaType.prototype.select = function select (val) {
this.selected = !! val;
}
/**
* Performs a validation of `value` using the validators declared for this SchemaType.
*
* @param {any} value
* @param {Function} callback
* @param {Object} scope
* @api private
*/
SchemaType.prototype.doValidate = function (value, fn, scope) {
var err = false
, path = this.path
, count = this.validators.length;
if (!count) return fn(null);
function validate (ok, msg, val) {
if (err) return;
if (ok === undefined || ok) {
--count || fn(null);
} else {
fn(err = new ValidatorError(path, msg, val));
}
}
this.validators.forEach(function (v) {
var validator = v[0]
, message = v[1];
if (validator instanceof RegExp) {
validate(validator.test(value), message, value);
} else if ('function' === typeof validator) {
if (2 === validator.length) {
validator.call(scope, value, function (ok) {
validate(ok, message, value);
});
} else {
validate(validator.call(scope, value), message, value);
}
}
});
};
/**
* Determines if value is a valid Reference.
*
* @param {SchemaType} self
* @param {Object} value
* @param {Document} doc
* @param {Boolean} init
* @return {Boolean}
* @api private
*/
SchemaType._isRef = function (self, value, doc, init) {
// fast path
var ref = init && self.options && self.options.ref;
if (!ref && doc && doc.$__fullPath) {
// checks for
// - this populated with adhoc model and no ref was set in schema OR
// - setting / pushing values after population
var path = doc.$__fullPath(self.path);
var owner = doc.ownerDocument ? doc.ownerDocument() : doc;
ref = owner.populated(path);
}
if (ref) {
if (null == value) return true;
if (!Buffer.isBuffer(value) && // buffers are objects too
'Binary' != value._bsontype // raw binary value from the db
&& utils.isObject(value) // might have deselected _id in population query
) {
return true;
}
}
return false;
}
/*!
* Module exports.
*/
module.exports = exports = SchemaType;
exports.CastError = CastError;
exports.ValidatorError = ValidatorError;