array.js
15.7 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
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
'use strict';
/*!
* Module dependencies.
*/
const $exists = require('./operators/exists');
const $type = require('./operators/type');
const MongooseError = require('../error/mongooseError');
const SchemaArrayOptions = require('../options/SchemaArrayOptions');
const SchemaType = require('../schematype');
const CastError = SchemaType.CastError;
const Mixed = require('./mixed');
const arrayDepth = require('../helpers/arrayDepth');
const cast = require('../cast');
const get = require('../helpers/get');
const isOperator = require('../helpers/query/isOperator');
const util = require('util');
const utils = require('../utils');
const castToNumber = require('./operators/helpers').castToNumber;
const geospatial = require('./operators/geospatial');
const getDiscriminatorByValue = require('../helpers/discriminator/getDiscriminatorByValue');
let MongooseArray;
let EmbeddedDoc;
const isNestedArraySymbol = Symbol('mongoose#isNestedArray');
/**
* Array SchemaType constructor
*
* @param {String} key
* @param {SchemaType} cast
* @param {Object} options
* @inherits SchemaType
* @api public
*/
function SchemaArray(key, cast, options, schemaOptions) {
// lazy load
EmbeddedDoc || (EmbeddedDoc = require('../types').Embedded);
let typeKey = 'type';
if (schemaOptions && schemaOptions.typeKey) {
typeKey = schemaOptions.typeKey;
}
this.schemaOptions = schemaOptions;
if (cast) {
let castOptions = {};
if (utils.isPOJO(cast)) {
if (cast[typeKey]) {
// support { type: Woot }
castOptions = utils.clone(cast); // do not alter user arguments
delete castOptions[typeKey];
cast = cast[typeKey];
} else {
cast = Mixed;
}
}
if (cast === Object) {
cast = Mixed;
}
// support { type: 'String' }
const name = typeof cast === 'string'
? cast
: utils.getFunctionName(cast);
const Types = require('./index.js');
const caster = Types.hasOwnProperty(name) ? Types[name] : cast;
this.casterConstructor = caster;
if (this.casterConstructor instanceof SchemaArray) {
this.casterConstructor[isNestedArraySymbol] = true;
}
if (typeof caster === 'function' &&
!caster.$isArraySubdocument &&
!caster.$isSchemaMap) {
this.caster = new caster(null, castOptions);
} else {
this.caster = caster;
}
this.$embeddedSchemaType = this.caster;
if (!(this.caster instanceof EmbeddedDoc)) {
this.caster.path = key;
}
}
this.$isMongooseArray = true;
SchemaType.call(this, key, options, 'Array');
let defaultArr;
let fn;
if (this.defaultValue != null) {
defaultArr = this.defaultValue;
fn = typeof defaultArr === 'function';
}
if (!('defaultValue' in this) || this.defaultValue !== void 0) {
const defaultFn = function() {
let arr = [];
if (fn) {
arr = defaultArr.call(this);
} else if (defaultArr != null) {
arr = arr.concat(defaultArr);
}
// Leave it up to `cast()` to convert the array
return arr;
};
defaultFn.$runBeforeSetters = !fn;
this.default(defaultFn);
}
}
/**
* This schema type's name, to defend against minifiers that mangle
* function names.
*
* @api public
*/
SchemaArray.schemaName = 'Array';
/**
* Options for all arrays.
*
* - `castNonArrays`: `true` by default. If `false`, Mongoose will throw a CastError when a value isn't an array. If `true`, Mongoose will wrap the provided value in an array before casting.
*
* @static options
* @api public
*/
SchemaArray.options = { castNonArrays: true };
SchemaArray.defaultOptions = {};
/**
* Sets a default option for all Array instances.
*
* ####Example:
*
* // Make all Array instances have `required` of true by default.
* mongoose.Schema.Array.set('required', true);
*
* const User = mongoose.model('User', new Schema({ test: Array }));
* new User({ }).validateSync().errors.test.message; // Path `test` is required.
*
* @param {String} option - The option you'd like to set the value for
* @param {*} value - value for option
* @return {undefined}
* @function set
* @static
* @api public
*/
SchemaArray.set = SchemaType.set;
/*!
* Inherits from SchemaType.
*/
SchemaArray.prototype = Object.create(SchemaType.prototype);
SchemaArray.prototype.constructor = SchemaArray;
SchemaArray.prototype.OptionsConstructor = SchemaArrayOptions;
/*!
* ignore
*/
SchemaArray._checkRequired = SchemaType.prototype.checkRequired;
/**
* Override the function the required validator uses to check whether an array
* passes the `required` check.
*
* ####Example:
*
* // Require non-empty array to pass `required` check
* mongoose.Schema.Types.Array.checkRequired(v => Array.isArray(v) && v.length);
*
* const M = mongoose.model({ arr: { type: Array, required: true } });
* new M({ arr: [] }).validateSync(); // `null`, validation fails!
*
* @param {Function} fn
* @return {Function}
* @function checkRequired
* @static
* @api public
*/
SchemaArray.checkRequired = SchemaType.checkRequired;
/**
* Check if the given value satisfies the `required` validator.
*
* @param {Any} value
* @param {Document} doc
* @return {Boolean}
* @api public
*/
SchemaArray.prototype.checkRequired = function checkRequired(value, doc) {
if (SchemaType._isRef(this, value, doc, true)) {
return !!value;
}
// `require('util').inherits()` does **not** copy static properties, and
// plugins like mongoose-float use `inherits()` for pre-ES6.
const _checkRequired = typeof this.constructor.checkRequired == 'function' ?
this.constructor.checkRequired() :
SchemaArray.checkRequired();
return _checkRequired(value);
};
/**
* Adds an enum validator if this is an array of strings or numbers. Equivalent to
* `SchemaString.prototype.enum()` or `SchemaNumber.prototype.enum()`
*
* @param {String|Object} [args...] enumeration values
* @return {SchemaArray} this
*/
SchemaArray.prototype.enum = function() {
let arr = this;
while (true) {
const instance = get(arr, 'caster.instance');
if (instance === 'Array') {
arr = arr.caster;
continue;
}
if (instance !== 'String' && instance !== 'Number') {
throw new Error('`enum` can only be set on an array of strings or numbers ' +
', not ' + instance);
}
break;
}
arr.caster.enum.apply(arr.caster, arguments);
return this;
};
/**
* Overrides the getters application for the population special-case
*
* @param {Object} value
* @param {Object} scope
* @api private
*/
SchemaArray.prototype.applyGetters = function(value, scope) {
if (this.caster.options && this.caster.options.ref) {
// means the object id was populated
return value;
}
return SchemaType.prototype.applyGetters.call(this, value, scope);
};
SchemaArray.prototype._applySetters = function(value, scope, init, priorVal) {
if (this.casterConstructor instanceof SchemaArray &&
SchemaArray.options.castNonArrays &&
!this[isNestedArraySymbol]) {
// Check nesting levels and wrap in array if necessary
let depth = 0;
let arr = this;
while (arr != null &&
arr instanceof SchemaArray &&
!arr.$isMongooseDocumentArray) {
++depth;
arr = arr.casterConstructor;
}
// No need to wrap empty arrays
if (value != null && value.length > 0) {
const valueDepth = arrayDepth(value);
if (valueDepth.min === valueDepth.max && valueDepth.max < depth && valueDepth.containsNonArrayItem) {
for (let i = valueDepth.max; i < depth; ++i) {
value = [value];
}
}
}
}
return SchemaType.prototype._applySetters.call(this, value, scope, init, priorVal);
};
/**
* Casts values for set().
*
* @param {Object} value
* @param {Document} doc document that triggers the casting
* @param {Boolean} init whether this is an initialization cast
* @api private
*/
SchemaArray.prototype.cast = function(value, doc, init, prev, options) {
// lazy load
MongooseArray || (MongooseArray = require('../types').Array);
let i;
let l;
if (Array.isArray(value)) {
if (!value.length && doc) {
const indexes = doc.schema.indexedPaths();
const arrayPath = this.path;
for (i = 0, l = indexes.length; i < l; ++i) {
const pathIndex = indexes[i][0][arrayPath];
if (pathIndex === '2dsphere' || pathIndex === '2d') {
return;
}
}
// Special case: if this index is on the parent of what looks like
// GeoJSON, skip setting the default to empty array re: #1668, #3233
const arrayGeojsonPath = this.path.endsWith('.coordinates') ?
this.path.substr(0, this.path.lastIndexOf('.')) : null;
if (arrayGeojsonPath != null) {
for (i = 0, l = indexes.length; i < l; ++i) {
const pathIndex = indexes[i][0][arrayGeojsonPath];
if (pathIndex === '2dsphere') {
return;
}
}
}
}
if (!(value && value.isMongooseArray)) {
value = new MongooseArray(value, this._arrayPath || this.path, doc);
} else if (value && value.isMongooseArray) {
// We need to create a new array, otherwise change tracking will
// update the old doc (gh-4449)
value = new MongooseArray(value, this._arrayPath || this.path, doc);
}
const isPopulated = doc != null && doc.$__ != null && doc.populated(this.path);
if (isPopulated) {
return value;
}
if (this.caster && this.casterConstructor !== Mixed) {
try {
for (i = 0, l = value.length; i < l; i++) {
// Special case: number arrays disallow undefined.
// Re: gh-840
// See commit 1298fe92d2c790a90594bd08199e45a4a09162a6
if (this.caster.instance === 'Number' && value[i] === void 0) {
throw new MongooseError('Mongoose number arrays disallow storing undefined');
}
const opts = {};
if (options != null && options.arrayPath != null) {
opts.arrayPath = options.arrayPath + '.' + i;
} else if (this.caster._arrayPath != null) {
opts.arrayPath = this.caster._arrayPath.slice(0, -2) + '.' + i;
}
value[i] = this.caster.cast(value[i], doc, init, void 0, opts);
}
} catch (e) {
// rethrow
throw new CastError('[' + e.kind + ']', util.inspect(value), this.path, e, this);
}
}
return value;
}
if (init || SchemaArray.options.castNonArrays) {
// gh-2442: if we're loading this from the db and its not an array, mark
// the whole array as modified.
if (!!doc && !!init) {
doc.markModified(this.path);
}
return this.cast([value], doc, init);
}
throw new CastError('Array', util.inspect(value), this.path, null, this);
};
/*!
* Ignore
*/
SchemaArray.prototype.discriminator = function(name, schema) {
let arr = this; // eslint-disable-line consistent-this
while (arr.$isMongooseArray && !arr.$isMongooseDocumentArray) {
arr = arr.casterConstructor;
if (arr == null || typeof arr === 'function') {
throw new MongooseError('You can only add an embedded discriminator on ' +
'a document array, ' + this.path + ' is a plain array');
}
}
return arr.discriminator(name, schema);
};
/*!
* ignore
*/
SchemaArray.prototype.clone = function() {
const options = Object.assign({}, this.options);
const schematype = new this.constructor(this.path, this.caster, options, this.schemaOptions);
schematype.validators = this.validators.slice();
if (this.requiredValidator !== undefined) {
schematype.requiredValidator = this.requiredValidator;
}
return schematype;
};
/**
* Casts values for queries.
*
* @param {String} $conditional
* @param {any} [value]
* @api private
*/
SchemaArray.prototype.castForQuery = function($conditional, value) {
let handler;
let val;
if (arguments.length === 2) {
handler = this.$conditionalHandlers[$conditional];
if (!handler) {
throw new Error('Can\'t use ' + $conditional + ' with Array.');
}
val = handler.call(this, value);
} else {
val = $conditional;
let Constructor = this.casterConstructor;
if (val &&
Constructor.discriminators &&
Constructor.schema &&
Constructor.schema.options &&
Constructor.schema.options.discriminatorKey) {
if (typeof val[Constructor.schema.options.discriminatorKey] === 'string' &&
Constructor.discriminators[val[Constructor.schema.options.discriminatorKey]]) {
Constructor = Constructor.discriminators[val[Constructor.schema.options.discriminatorKey]];
} else {
const constructorByValue = getDiscriminatorByValue(Constructor, val[Constructor.schema.options.discriminatorKey]);
if (constructorByValue) {
Constructor = constructorByValue;
}
}
}
const proto = this.casterConstructor.prototype;
let method = proto && (proto.castForQuery || proto.cast);
if (!method && Constructor.castForQuery) {
method = Constructor.castForQuery;
}
const caster = this.caster;
if (Array.isArray(val)) {
this.setters.reverse().forEach(setter => {
val = setter.call(this, val, this);
});
val = val.map(function(v) {
if (utils.isObject(v) && v.$elemMatch) {
return v;
}
if (method) {
v = method.call(caster, v);
return v;
}
if (v != null) {
v = new Constructor(v);
return v;
}
return v;
});
} else if (method) {
val = method.call(caster, val);
} else if (val != null) {
val = new Constructor(val);
}
}
return val;
};
function cast$all(val) {
if (!Array.isArray(val)) {
val = [val];
}
val = val.map(function(v) {
if (utils.isObject(v)) {
const o = {};
o[this.path] = v;
return cast(this.casterConstructor.schema, o)[this.path];
}
return v;
}, this);
return this.castForQuery(val);
}
function cast$elemMatch(val) {
const keys = Object.keys(val);
const numKeys = keys.length;
for (let i = 0; i < numKeys; ++i) {
const key = keys[i];
const value = val[key];
if (isOperator(key) && value != null) {
val[key] = this.castForQuery(key, value);
}
}
// Is this an embedded discriminator and is the discriminator key set?
// If so, use the discriminator schema. See gh-7449
const discriminatorKey = get(this,
'casterConstructor.schema.options.discriminatorKey');
const discriminators = get(this, 'casterConstructor.schema.discriminators', {});
if (discriminatorKey != null &&
val[discriminatorKey] != null &&
discriminators[val[discriminatorKey]] != null) {
return cast(discriminators[val[discriminatorKey]], val);
}
return cast(this.casterConstructor.schema, val);
}
const handle = SchemaArray.prototype.$conditionalHandlers = {};
handle.$all = cast$all;
handle.$options = String;
handle.$elemMatch = cast$elemMatch;
handle.$geoIntersects = geospatial.cast$geoIntersects;
handle.$or = handle.$and = function(val) {
if (!Array.isArray(val)) {
throw new TypeError('conditional $or/$and require array');
}
const ret = [];
for (const obj of val) {
ret.push(cast(this.casterConstructor.schema, obj));
}
return ret;
};
handle.$near =
handle.$nearSphere = geospatial.cast$near;
handle.$within =
handle.$geoWithin = geospatial.cast$within;
handle.$size =
handle.$minDistance =
handle.$maxDistance = castToNumber;
handle.$exists = $exists;
handle.$type = $type;
handle.$eq =
handle.$gt =
handle.$gte =
handle.$lt =
handle.$lte =
handle.$ne =
handle.$regex = SchemaArray.prototype.castForQuery;
// `$in` is special because you can also include an empty array in the query
// like `$in: [1, []]`, see gh-5913
handle.$nin = SchemaType.prototype.$conditionalHandlers.$nin;
handle.$in = SchemaType.prototype.$conditionalHandlers.$in;
/*!
* Module exports.
*/
module.exports = SchemaArray;