mongoose 6.2.4 → 6.2.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,22 @@
1
+ 'use strict';
2
+
3
+ /*!
4
+ * Determines if `arg` is a flat object.
5
+ *
6
+ * @param {Object|Array|String|Function|RegExp|any} arg
7
+ * @api private
8
+ * @return {Boolean}
9
+ */
10
+
11
+ module.exports = function isSimpleValidator(obj) {
12
+ const keys = Object.keys(obj);
13
+ let result = true;
14
+ for (let i = 0, len = keys.length; i < len; ++i) {
15
+ if (typeof obj[keys[i]] === 'object' && obj[keys[i]] !== null) {
16
+ result = false;
17
+ break;
18
+ }
19
+ }
20
+
21
+ return result;
22
+ };
@@ -196,10 +196,18 @@ function numDocs(v) {
196
196
  if (Array.isArray(v)) {
197
197
  // If setting underneath an array of populated subdocs, we may have an
198
198
  // array of arrays. See gh-7573
199
- if (v.some(el => Array.isArray(el))) {
200
- return v.map(el => numDocs(el));
199
+ if (v.some(el => Array.isArray(el) || el === null)) {
200
+ return v.map(el => {
201
+ if (el == null) {
202
+ return 0;
203
+ }
204
+ if (Array.isArray(el)) {
205
+ return el.filter(el => el != null).length;
206
+ }
207
+ return 1;
208
+ });
201
209
  }
202
- return v.length;
210
+ return v.filter(el => el != null).length;
203
211
  }
204
212
  return v == null ? 0 : 1;
205
213
  }
@@ -389,7 +389,7 @@ function _virtualPopulate(model, docs, options, _virtualRes) {
389
389
  localField = localField.call(doc, doc);
390
390
  }
391
391
  if (typeof foreignField === 'function') {
392
- foreignField = foreignField.call(doc);
392
+ foreignField = foreignField.call(doc, doc);
393
393
  }
394
394
 
395
395
  data.isRefPath = false;
@@ -177,6 +177,8 @@ function walkUpdatePath(schema, obj, op, options, context, filter, pref) {
177
177
 
178
178
  let aggregatedError = null;
179
179
 
180
+ const strictMode = strict != null ? strict : schema.options.strict;
181
+
180
182
  while (i--) {
181
183
  key = keys[i];
182
184
  val = obj[key];
@@ -192,6 +194,17 @@ function walkUpdatePath(schema, obj, op, options, context, filter, pref) {
192
194
  }
193
195
  }
194
196
 
197
+ if (schema.discriminatorMapping != null && key === schema.options.discriminatorKey && !options.overwriteDiscriminatorKey) {
198
+ if (strictMode === 'throw') {
199
+ const err = new Error('Can\'t modify discriminator key "' + key + '" on discriminator model');
200
+ aggregatedError = _appendError(err, context, key, aggregatedError);
201
+ continue;
202
+ } else if (strictMode) {
203
+ delete obj[key];
204
+ continue;
205
+ }
206
+ }
207
+
195
208
  if (getConstructorName(val) === 'Object') {
196
209
  // watch for embedded doc schemas
197
210
  schematype = schema._getSchema(prefix + key);
@@ -217,7 +230,7 @@ function walkUpdatePath(schema, obj, op, options, context, filter, pref) {
217
230
  $each: castUpdateVal(schematype, val.$each, op, key, context, prefix + key)
218
231
  };
219
232
  } catch (error) {
220
- aggregatedError = _handleCastError(error, context, key, aggregatedError);
233
+ aggregatedError = _appendError(error, context, key, aggregatedError);
221
234
  }
222
235
 
223
236
  if (val.$slice != null) {
@@ -237,13 +250,13 @@ function walkUpdatePath(schema, obj, op, options, context, filter, pref) {
237
250
  try {
238
251
  obj[key] = schematype.castForQuery(val, context, { strict: _strict });
239
252
  } catch (error) {
240
- aggregatedError = _handleCastError(error, context, key, aggregatedError);
253
+ aggregatedError = _appendError(error, context, key, aggregatedError);
241
254
  }
242
255
  } else {
243
256
  try {
244
257
  obj[key] = castUpdateVal(schematype, val, op, key, context, prefix + key);
245
258
  } catch (error) {
246
- aggregatedError = _handleCastError(error, context, key, aggregatedError);
259
+ aggregatedError = _appendError(error, context, key, aggregatedError);
247
260
  }
248
261
  }
249
262
 
@@ -259,7 +272,7 @@ function walkUpdatePath(schema, obj, op, options, context, filter, pref) {
259
272
  try {
260
273
  obj[key] = castUpdateVal(schematype, val, op, key, context, prefix + key);
261
274
  } catch (error) {
262
- aggregatedError = _handleCastError(error, context, key, aggregatedError);
275
+ aggregatedError = _appendError(error, context, key, aggregatedError);
263
276
  }
264
277
 
265
278
  if (obj[key] === void 0) {
@@ -350,7 +363,7 @@ function walkUpdatePath(schema, obj, op, options, context, filter, pref) {
350
363
  delete obj[key];
351
364
  }
352
365
  } catch (error) {
353
- aggregatedError = _handleCastError(error, context, key, aggregatedError);
366
+ aggregatedError = _appendError(error, context, key, aggregatedError);
354
367
  }
355
368
 
356
369
  if (Array.isArray(obj[key]) && (op === '$addToSet' || op === '$push') && key !== '$each') {
@@ -383,7 +396,7 @@ function walkUpdatePath(schema, obj, op, options, context, filter, pref) {
383
396
  * ignore
384
397
  */
385
398
 
386
- function _handleCastError(error, query, key, aggregatedError) {
399
+ function _appendError(error, query, key, aggregatedError) {
387
400
  if (typeof query !== 'object' || !query.options.multipleCastError) {
388
401
  throw error;
389
402
  }
@@ -455,6 +468,8 @@ function castUpdateVal(schema, val, op, $conditional, context, path) {
455
468
  return val;
456
469
  }
457
470
 
471
+ // console.log('CastUpdateVal', path, op, val, schema);
472
+
458
473
  const cond = schema.caster && op in castOps &&
459
474
  (utils.isObject(val) || Array.isArray(val));
460
475
  if (cond && !overwriteOps[op]) {
package/lib/index.js CHANGED
@@ -40,6 +40,8 @@ const defaultMongooseSymbol = Symbol.for('mongoose:default');
40
40
 
41
41
  require('./helpers/printJestWarning');
42
42
 
43
+ const objectIdHexRegexp = /^[0-9A-Fa-f]{24}$/;
44
+
43
45
  /**
44
46
  * Mongoose constructor.
45
47
  *
@@ -945,11 +947,13 @@ Mongoose.prototype.ObjectId = SchemaTypes.ObjectId;
945
947
  * mongoose.isValidObjectId(new mongoose.Types.ObjectId()); // true
946
948
  * mongoose.isValidObjectId('0123456789ab'); // true
947
949
  * mongoose.isValidObjectId(6); // true
950
+ * mongoose.isValidObjectId(new User({ name: 'test' })); // true
951
+ *
948
952
  * mongoose.isValidObjectId({ test: 42 }); // false
949
953
  *
950
954
  * @method isValidObjectId
951
- * @param {Any} value
952
- * @returns {boolean} true if it is a valid ObjectId
955
+ * @param {Any} v
956
+ * @returns {boolean} true if `v` is something Mongoose can coerce to an ObjectId
953
957
  * @api public
954
958
  */
955
959
 
@@ -958,6 +962,38 @@ Mongoose.prototype.isValidObjectId = function(v) {
958
962
  return _mongoose.Types.ObjectId.isValid(v);
959
963
  };
960
964
 
965
+ /**
966
+ * Returns true if the given value is a Mongoose ObjectId (using `instanceof`) or if the
967
+ * given value is a 24 character hex string, which is the most commonly used string representation
968
+ * of an ObjectId.
969
+ *
970
+ * This function is similar to `isValidObjectId()`, but considerably more strict, because
971
+ * `isValidObjectId()` will return `true` for _any_ value that Mongoose can convert to an
972
+ * ObjectId. That includes Mongoose documents, any string of length 12, and any number.
973
+ * `isObjectIdOrHexString()` returns true only for `ObjectId` instances or 24 character hex
974
+ * strings, and will return false for numbers, documents, and strings of length 12.
975
+ *
976
+ * ####Example:
977
+ *
978
+ * mongoose.isObjectIdOrHexString(new mongoose.Types.ObjectId()); // true
979
+ * mongoose.isObjectIdOrHexString('62261a65d66c6be0a63c051f'); // true
980
+ *
981
+ * mongoose.isObjectIdOrHexString('0123456789ab'); // false
982
+ * mongoose.isObjectIdOrHexString(6); // false
983
+ * mongoose.isObjectIdOrHexString(new User({ name: 'test' })); // false
984
+ * mongoose.isObjectIdOrHexString({ test: 42 }); // false
985
+ *
986
+ * @method isObjectIdOrHexString
987
+ * @param {Any} v
988
+ * @returns {boolean} true if `v` is an ObjectId instance _or_ a 24 char hex string
989
+ * @api public
990
+ */
991
+
992
+ Mongoose.prototype.isObjectIdOrHexString = function(v) {
993
+ const _mongoose = this instanceof Mongoose ? this : mongoose;
994
+ return v instanceof _mongoose.Types.ObjectId || (typeof v === 'string' && objectIdHexRegexp.test(v));
995
+ };
996
+
961
997
  /**
962
998
  *
963
999
  * Syncs all the indexes for the models registered with this connection.
package/lib/model.js CHANGED
@@ -2481,7 +2481,7 @@ Model.$where = function $where() {
2481
2481
  * - `sort`: if multiple docs are found by the conditions, sets the sort order to choose which doc to update
2482
2482
  * - `runValidators`: if true, runs [update validators](/docs/validation.html#update-validators) on this command. Update validators validate the update operation against the model's schema.
2483
2483
  * - `setDefaultsOnInsert`: `true` by default. If `setDefaultsOnInsert` and `upsert` are true, mongoose will apply the [defaults](https://mongoosejs.com/docs/defaults.html) specified in the model's schema if a new document is created.
2484
- * - `rawResult`: if true, returns the [raw result from the MongoDB driver](https://mongodb.github.io/node-mongodb-native/2.0/api/Collection.html#findAndModify)
2484
+ * - `rawResult`: if true, returns the [raw result from the MongoDB driver](https://mongodb.github.io/node-mongodb-native/4.3/interfaces/ModifyResult.html)
2485
2485
  * - `strict`: overwrites the schema's [strict mode option](https://mongoosejs.com/docs/guide.html#strict) for this update
2486
2486
  *
2487
2487
  * ####Examples:
@@ -2617,7 +2617,7 @@ function _decorateUpdateWithVersionKey(update, options, versionKey) {
2617
2617
  * - `setDefaultsOnInsert`: `true` by default. If `setDefaultsOnInsert` and `upsert` are true, mongoose will apply the [defaults](https://mongoosejs.com/docs/defaults.html) specified in the model's schema if a new document is created.
2618
2618
  * - `sort`: if multiple docs are found by the conditions, sets the sort order to choose which doc to update
2619
2619
  * - `select`: sets the document fields to return
2620
- * - `rawResult`: if true, returns the [raw result from the MongoDB driver](https://mongodb.github.io/node-mongodb-native/2.0/api/Collection.html#findAndModify)
2620
+ * - `rawResult`: if true, returns the [raw result from the MongoDB driver](https://mongodb.github.io/node-mongodb-native/4.3/interfaces/ModifyResult.html)
2621
2621
  * - `strict`: overwrites the schema's [strict mode option](https://mongoosejs.com/docs/guide.html#strict) for this update
2622
2622
  *
2623
2623
  * ####Examples:
@@ -2718,7 +2718,7 @@ Model.findByIdAndUpdate = function(id, update, options, callback) {
2718
2718
  * - `maxTimeMS`: puts a time limit on the query - requires mongodb >= 2.6.0
2719
2719
  * - `select`: sets the document fields to return, ex. `{ projection: { _id: 0 } }`
2720
2720
  * - `projection`: equivalent to `select`
2721
- * - `rawResult`: if true, returns the [raw result from the MongoDB driver](https://mongodb.github.io/node-mongodb-native/2.0/api/Collection.html#findAndModify)
2721
+ * - `rawResult`: if true, returns the [raw result from the MongoDB driver](https://mongodb.github.io/node-mongodb-native/4.3/interfaces/ModifyResult.html)
2722
2722
  * - `strict`: overwrites the schema's [strict mode option](https://mongoosejs.com/docs/guide.html#strict) for this update
2723
2723
  *
2724
2724
  * ####Examples:
@@ -2831,7 +2831,7 @@ Model.findByIdAndDelete = function(id, options, callback) {
2831
2831
  * - `maxTimeMS`: puts a time limit on the query - requires mongodb >= 2.6.0
2832
2832
  * - `select`: sets the document fields to return
2833
2833
  * - `projection`: like select, it determines which fields to return, ex. `{ projection: { _id: 0 } }`
2834
- * - `rawResult`: if true, returns the [raw result from the MongoDB driver](https://mongodb.github.io/node-mongodb-native/2.0/api/Collection.html#findAndModify)
2834
+ * - `rawResult`: if true, returns the [raw result from the MongoDB driver](https://mongodb.github.io/node-mongodb-native/4.3/interfaces/ModifyResult.html)
2835
2835
  * - `strict`: overwrites the schema's [strict mode option](https://mongoosejs.com/docs/guide.html#strict) for this update
2836
2836
  *
2837
2837
  * ####Examples:
@@ -2908,7 +2908,7 @@ Model.findOneAndReplace = function(filter, replacement, options, callback) {
2908
2908
  * - `maxTimeMS`: puts a time limit on the query - requires mongodb >= 2.6.0
2909
2909
  * - `select`: sets the document fields to return
2910
2910
  * - `projection`: like select, it determines which fields to return, ex. `{ projection: { _id: 0 } }`
2911
- * - `rawResult`: if true, returns the [raw result from the MongoDB driver](https://mongodb.github.io/node-mongodb-native/2.0/api/Collection.html#findAndModify)
2911
+ * - `rawResult`: if true, returns the [raw result from the MongoDB driver](https://mongodb.github.io/node-mongodb-native/4.3/interfaces/ModifyResult.html)
2912
2912
  * - `strict`: overwrites the schema's [strict mode option](https://mongoosejs.com/docs/guide.html#strict) for this update
2913
2913
  *
2914
2914
  * ####Examples:
@@ -2986,7 +2986,7 @@ Model.findOneAndRemove = function(conditions, options, callback) {
2986
2986
  *
2987
2987
  * - `sort`: if multiple docs are found by the conditions, sets the sort order to choose which doc to update
2988
2988
  * - `select`: sets the document fields to return
2989
- * - `rawResult`: if true, returns the [raw result from the MongoDB driver](https://mongodb.github.io/node-mongodb-native/2.0/api/Collection.html#findAndModify)
2989
+ * - `rawResult`: if true, returns the [raw result from the MongoDB driver](https://mongodb.github.io/node-mongodb-native/4.3/interfaces/ModifyResult.html)
2990
2990
  * - `strict`: overwrites the schema's [strict mode option](https://mongoosejs.com/docs/guide.html#strict) for this update
2991
2991
  *
2992
2992
  * ####Examples:
package/lib/query.js CHANGED
@@ -3244,7 +3244,7 @@ function prepareDiscriminatorCriteria(query) {
3244
3244
  * - `maxTimeMS`: puts a time limit on the query - requires mongodb >= 2.6.0
3245
3245
  * - `runValidators`: if true, runs [update validators](/docs/validation.html#update-validators) on this command. Update validators validate the update operation against the model's schema.
3246
3246
  * - `setDefaultsOnInsert`: `true` by default. If `setDefaultsOnInsert` and `upsert` are true, mongoose will apply the [defaults](https://mongoosejs.com/docs/defaults.html) specified in the model's schema if a new document is created.
3247
- * - `rawResult`: if true, returns the [raw result from the MongoDB driver](https://mongodb.github.io/node-mongodb-native/2.0/api/Collection.html#findAndModify)
3247
+ * - `rawResult`: if true, returns the [raw result from the MongoDB driver](https://mongodb.github.io/node-mongodb-native/4.3/interfaces/ModifyResult.html)
3248
3248
  *
3249
3249
  * ####Callback Signature
3250
3250
  * function(error, doc) {
@@ -3269,7 +3269,7 @@ function prepareDiscriminatorCriteria(query) {
3269
3269
  * @param {Object|Query} [filter]
3270
3270
  * @param {Object} [doc]
3271
3271
  * @param {Object} [options]
3272
- * @param {Boolean} [options.rawResult] if true, returns the [raw result from the MongoDB driver](https://mongodb.github.io/node-mongodb-native/2.0/api/Collection.html#findAndModify)
3272
+ * @param {Boolean} [options.rawResult] if true, returns the [raw result from the MongoDB driver](https://mongodb.github.io/node-mongodb-native/4.3/interfaces/ModifyResult.html)
3273
3273
  * @param {Boolean|String} [options.strict] overwrites the schema's [strict mode option](https://mongoosejs.com/docs/guide.html#strict)
3274
3274
  * @param {ClientSession} [options.session=null] The session associated with this query. See [transactions docs](/docs/transactions.html).
3275
3275
  * @param {Boolean} [options.multipleCastError] by default, mongoose only returns the first error that occurred in casting the query. Turn on this option to aggregate all the cast errors.
@@ -3386,7 +3386,7 @@ Query.prototype._findOneAndUpdate = wrapThunk(function(callback) {
3386
3386
  *
3387
3387
  * - `sort`: if multiple docs are found by the conditions, sets the sort order to choose which doc to update
3388
3388
  * - `maxTimeMS`: puts a time limit on the query - requires mongodb >= 2.6.0
3389
- * - `rawResult`: if true, resolves to the [raw result from the MongoDB driver](https://mongodb.github.io/node-mongodb-native/2.0/api/Collection.html#findAndModify)
3389
+ * - `rawResult`: if true, resolves to the [raw result from the MongoDB driver](https://mongodb.github.io/node-mongodb-native/4.3/interfaces/ModifyResult.html)
3390
3390
  *
3391
3391
  * ####Callback Signature
3392
3392
  * function(error, doc) {
@@ -3408,7 +3408,7 @@ Query.prototype._findOneAndUpdate = wrapThunk(function(callback) {
3408
3408
  * @instance
3409
3409
  * @param {Object} [conditions]
3410
3410
  * @param {Object} [options]
3411
- * @param {Boolean} [options.rawResult] if true, returns the [raw result from the MongoDB driver](https://mongodb.github.io/node-mongodb-native/2.0/api/Collection.html#findAndModify)
3411
+ * @param {Boolean} [options.rawResult] if true, returns the [raw result from the MongoDB driver](https://mongodb.github.io/node-mongodb-native/4.3/interfaces/ModifyResult.html)
3412
3412
  * @param {ClientSession} [options.session=null] The session associated with this query. See [transactions docs](/docs/transactions.html).
3413
3413
  * @param {Boolean|String} [options.strict] overwrites the schema's [strict mode option](https://mongoosejs.com/docs/guide.html#strict)
3414
3414
  * @param {Function} [callback] optional params are (error, document)
@@ -3473,7 +3473,7 @@ Query.prototype.findOneAndRemove = function(conditions, options, callback) {
3473
3473
  *
3474
3474
  * - `sort`: if multiple docs are found by the conditions, sets the sort order to choose which doc to update
3475
3475
  * - `maxTimeMS`: puts a time limit on the query - requires mongodb >= 2.6.0
3476
- * - `rawResult`: if true, resolves to the [raw result from the MongoDB driver](https://mongodb.github.io/node-mongodb-native/2.0/api/Collection.html#findAndModify)
3476
+ * - `rawResult`: if true, resolves to the [raw result from the MongoDB driver](https://mongodb.github.io/node-mongodb-native/4.3/interfaces/ModifyResult.html)
3477
3477
  *
3478
3478
  * ####Callback Signature
3479
3479
  * function(error, doc) {
@@ -3494,7 +3494,7 @@ Query.prototype.findOneAndRemove = function(conditions, options, callback) {
3494
3494
  * @memberOf Query
3495
3495
  * @param {Object} [conditions]
3496
3496
  * @param {Object} [options]
3497
- * @param {Boolean} [options.rawResult] if true, returns the [raw result from the MongoDB driver](https://mongodb.github.io/node-mongodb-native/2.0/api/Collection.html#findAndModify)
3497
+ * @param {Boolean} [options.rawResult] if true, returns the [raw result from the MongoDB driver](https://mongodb.github.io/node-mongodb-native/4.3/interfaces/ModifyResult.html)
3498
3498
  * @param {ClientSession} [options.session=null] The session associated with this query. See [transactions docs](/docs/transactions.html).
3499
3499
  * @param {Boolean|String} [options.strict] overwrites the schema's [strict mode option](https://mongoosejs.com/docs/guide.html#strict)
3500
3500
  * @param {Function} [callback] optional params are (error, document)
@@ -3592,7 +3592,7 @@ Query.prototype._findOneAndDelete = wrapThunk(function(callback) {
3592
3592
  *
3593
3593
  * - `sort`: if multiple docs are found by the conditions, sets the sort order to choose which doc to update
3594
3594
  * - `maxTimeMS`: puts a time limit on the query - requires mongodb >= 2.6.0
3595
- * - `rawResult`: if true, resolves to the [raw result from the MongoDB driver](https://mongodb.github.io/node-mongodb-native/2.0/api/Collection.html#findAndModify)
3595
+ * - `rawResult`: if true, resolves to the [raw result from the MongoDB driver](https://mongodb.github.io/node-mongodb-native/4.3/interfaces/ModifyResult.html)
3596
3596
  *
3597
3597
  * ####Callback Signature
3598
3598
  * function(error, doc) {
@@ -3614,7 +3614,7 @@ Query.prototype._findOneAndDelete = wrapThunk(function(callback) {
3614
3614
  * @param {Object} [filter]
3615
3615
  * @param {Object} [replacement]
3616
3616
  * @param {Object} [options]
3617
- * @param {Boolean} [options.rawResult] if true, returns the [raw result from the MongoDB driver](https://mongodb.github.io/node-mongodb-native/2.0/api/Collection.html#findAndModify)
3617
+ * @param {Boolean} [options.rawResult] if true, returns the [raw result from the MongoDB driver](https://mongodb.github.io/node-mongodb-native/4.3/interfaces/ModifyResult.html)
3618
3618
  * @param {ClientSession} [options.session=null] The session associated with this query. See [transactions docs](/docs/transactions.html).
3619
3619
  * @param {Boolean|String} [options.strict] overwrites the schema's [strict mode option](https://mongoosejs.com/docs/guide.html#strict)
3620
3620
  * @param {Boolean} [options.new=false] By default, `findOneAndUpdate()` returns the document as it was **before** `update` was applied. If you set `new: true`, `findOneAndUpdate()` will instead give you the object after `update` was applied.
@@ -646,6 +646,7 @@ handle.$gte =
646
646
  handle.$lt =
647
647
  handle.$lte =
648
648
  handle.$ne =
649
+ handle.$not =
649
650
  handle.$regex = SchemaArray.prototype.castForQuery;
650
651
 
651
652
  // `$in` is special because you can also include an empty array in the query
package/lib/schematype.js CHANGED
@@ -10,6 +10,7 @@ const $exists = require('./schema/operators/exists');
10
10
  const $type = require('./schema/operators/type');
11
11
  const handleImmutable = require('./helpers/schematype/handleImmutable');
12
12
  const isAsyncFunction = require('./helpers/isAsyncFunction');
13
+ const isSimpleValidator = require('./helpers/isSimpleValidator');
13
14
  const immediate = require('./helpers/immediate');
14
15
  const schemaTypeSymbol = require('./helpers/symbols').schemaTypeSymbol;
15
16
  const utils = require('./utils');
@@ -886,7 +887,7 @@ SchemaType.prototype.validate = function(obj, message, type) {
886
887
  properties = { validator: obj, message: message };
887
888
  properties.type = type || 'user defined';
888
889
  } else if (message instanceof Object && !type) {
889
- properties = utils.clone(message);
890
+ properties = isSimpleValidator(message) ? Object.assign({}, message) : utils.clone(message);
890
891
  if (!properties.message) {
891
892
  properties.message = properties.msg;
892
893
  }
@@ -914,8 +915,8 @@ SchemaType.prototype.validate = function(obj, message, type) {
914
915
  arg = arguments[i];
915
916
  if (!utils.isPOJO(arg)) {
916
917
  const msg = 'Invalid validator. Received (' + typeof arg + ') '
917
- + arg
918
- + '. See https://mongoosejs.com/docs/api.html#schematype_SchemaType-validate';
918
+ + arg
919
+ + '. See https://mongoosejs.com/docs/api.html#schematype_SchemaType-validate';
919
920
 
920
921
  throw new Error(msg);
921
922
  }
@@ -1242,7 +1243,7 @@ SchemaType.prototype.doValidate = function(value, fn, scope, options) {
1242
1243
 
1243
1244
  // Avoid non-object `validators`
1244
1245
  const validators = this.validators.
1245
- filter(v => v != null && typeof v === 'object');
1246
+ filter(v => typeof v === 'object' && v !== null);
1246
1247
 
1247
1248
  let count = validators.length;
1248
1249
 
@@ -1251,15 +1252,15 @@ SchemaType.prototype.doValidate = function(value, fn, scope, options) {
1251
1252
  }
1252
1253
 
1253
1254
  for (let i = 0, len = validators.length; i < len; ++i) {
1254
- const v = validators[i];
1255
1255
  if (err) {
1256
1256
  break;
1257
1257
  }
1258
1258
 
1259
+ const v = validators[i];
1259
1260
  const validator = v.validator;
1260
1261
  let ok;
1261
1262
 
1262
- const validatorProperties = utils.clone(v);
1263
+ const validatorProperties = isSimpleValidator(v) ? Object.assign({}, v) : utils.clone(v);
1263
1264
  validatorProperties.path = options && options.path ? options.path : path;
1264
1265
  validatorProperties.value = value;
1265
1266
 
@@ -1373,12 +1374,12 @@ SchemaType.prototype.doValidateSync = function(value, scope, options) {
1373
1374
 
1374
1375
  const v = validators[i];
1375
1376
 
1376
- if (v == null || typeof v !== 'object') {
1377
+ if (v === null || typeof v !== 'object') {
1377
1378
  continue;
1378
1379
  }
1379
1380
 
1380
1381
  const validator = v.validator;
1381
- const validatorProperties = utils.clone(v);
1382
+ const validatorProperties = isSimpleValidator(v) ? Object.assign({}, v) : utils.clone(v);
1382
1383
  validatorProperties.path = options && options.path ? options.path : path;
1383
1384
  validatorProperties.value = value;
1384
1385
  let ok = false;
@@ -1453,8 +1454,8 @@ SchemaType._isRef = function(self, value, doc, init) {
1453
1454
  return true;
1454
1455
  }
1455
1456
  if (!Buffer.isBuffer(value) && // buffers are objects too
1456
- value._bsontype !== 'Binary' // raw binary value from the db
1457
- && utils.isObject(value) // might have deselected _id in population query
1457
+ value._bsontype !== 'Binary' // raw binary value from the db
1458
+ && utils.isObject(value) // might have deselected _id in population query
1458
1459
  ) {
1459
1460
  return true;
1460
1461
  }
@@ -1495,10 +1496,10 @@ SchemaType.prototype._castRef = function _castRef(value, doc, init) {
1495
1496
  const pop = owner.$populated(path, true);
1496
1497
  let ret = value;
1497
1498
  if (!doc.$__.populated ||
1498
- !doc.$__.populated[path] ||
1499
- !doc.$__.populated[path].options ||
1500
- !doc.$__.populated[path].options.options ||
1501
- !doc.$__.populated[path].options.options.lean) {
1499
+ !doc.$__.populated[path] ||
1500
+ !doc.$__.populated[path].options ||
1501
+ !doc.$__.populated[path].options.options ||
1502
+ !doc.$__.populated[path].options.options.lean) {
1502
1503
  ret = new pop.options[populateModelSymbol](value);
1503
1504
  ret.$__.wasPopulated = true;
1504
1505
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "mongoose",
3
3
  "description": "Mongoose MongoDB ODM",
4
- "version": "6.2.4",
4
+ "version": "6.2.5",
5
5
  "author": "Guillermo Rauch <guillermo@learnboost.com>",
6
6
  "keywords": [
7
7
  "mongodb",
File without changes
@@ -0,0 +1,48 @@
1
+ import stream = require('stream');
2
+
3
+ declare module 'mongoose' {
4
+ type CursorFlag = 'tailable' | 'oplogReplay' | 'noCursorTimeout' | 'awaitData' | 'partial';
5
+
6
+ class Cursor<DocType = any, Options = never> extends stream.Readable {
7
+ [Symbol.asyncIterator](): AsyncIterableIterator<DocType>;
8
+
9
+ /**
10
+ * Adds a [cursor flag](http://mongodb.github.io/node-mongodb-native/2.2/api/Cursor.html#addCursorFlag).
11
+ * Useful for setting the `noCursorTimeout` and `tailable` flags.
12
+ */
13
+ addCursorFlag(flag: CursorFlag, value: boolean): this;
14
+
15
+ /**
16
+ * Marks this cursor as closed. Will stop streaming and subsequent calls to
17
+ * `next()` will error.
18
+ */
19
+ close(callback: CallbackWithoutResult): void;
20
+ close(): Promise<void>;
21
+
22
+ /**
23
+ * Execute `fn` for every document(s) in the cursor. If batchSize is provided
24
+ * `fn` will be executed for each batch of documents. If `fn` returns a promise,
25
+ * will wait for the promise to resolve before iterating on to the next one.
26
+ * Returns a promise that resolves when done.
27
+ */
28
+ eachAsync(fn: (doc: DocType[]) => any, options: { parallel?: number, batchSize: number }, callback: CallbackWithoutResult): void;
29
+ eachAsync(fn: (doc: DocType) => any, options: { parallel?: number }, callback: CallbackWithoutResult): void;
30
+ eachAsync(fn: (doc: DocType[]) => any, options: { parallel?: number, batchSize: number }): Promise<void>;
31
+ eachAsync(fn: (doc: DocType) => any, options?: { parallel?: number }): Promise<void>;
32
+
33
+ /**
34
+ * Registers a transform function which subsequently maps documents retrieved
35
+ * via the streams interface or `.next()`
36
+ */
37
+ map<ResultType>(fn: (res: DocType) => ResultType): Cursor<ResultType, Options>;
38
+
39
+ /**
40
+ * Get the next document from this cursor. Will return `null` when there are
41
+ * no documents left.
42
+ */
43
+ next(callback: Callback<DocType | null>): void;
44
+ next(): Promise<DocType>;
45
+
46
+ options: Options;
47
+ }
48
+ }
File without changes