mongoose 8.24.7 → 8.24.8

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.
package/lib/document.js CHANGED
@@ -26,6 +26,7 @@ const compile = require('./helpers/document/compile').compile;
26
26
  const defineKey = require('./helpers/document/compile').defineKey;
27
27
  const firstKey = require('./helpers/firstKey');
28
28
  const flatten = require('./helpers/common').flatten;
29
+ const getConstructorName = require('./helpers/getConstructorName');
29
30
  const getEmbeddedDiscriminatorPath = require('./helpers/document/getEmbeddedDiscriminatorPath');
30
31
  const getKeysInSchemaOrder = require('./helpers/schema/getKeysInSchemaOrder');
31
32
  const getSubdocumentStrictValue = require('./helpers/schema/getSubdocumentStrictValue');
@@ -3414,6 +3415,15 @@ Document.prototype.invalidate = function(path, err, val, kind) {
3414
3415
  return this.$__.validationError;
3415
3416
  }
3416
3417
 
3418
+ // Set the model on cast errors with a custom message so `{MODEL}` gets
3419
+ // replaced for document validation, not just query casting (gh-8300)
3420
+ if (typeof err.setModel === 'function' && err.messageFormat != null) {
3421
+ const owner = this.ownerDocument();
3422
+ if (getConstructorName(owner) === 'model') {
3423
+ err.setModel(owner.constructor);
3424
+ }
3425
+ }
3426
+
3417
3427
  this.$__.validationError.addError(path, err);
3418
3428
  return this.$__.validationError;
3419
3429
  };
@@ -59,6 +59,10 @@ class Union extends SchemaType {
59
59
 
60
60
  // Setters also need to be aware of casting - we need to apply the setters of the entry in the union we choose.
61
61
  applySetters(val, doc, init, prev, options) {
62
+ // Setters on the union path itself run first, so they can change which entry
63
+ // in the union the value resolves to.
64
+ val = this._applySetters(val, doc, init, prev, options);
65
+
62
66
  let firstValue = firstValueSymbol;
63
67
  let lastError;
64
68
  // Loop through each schema type in the union. If one of the schematypes returns a value that is `=== val`, then
package/lib/schemaType.js CHANGED
@@ -1625,7 +1625,7 @@ SchemaType.prototype._castRef = function _castRef(value, doc, init, options) {
1625
1625
  !doc.$__.populated[path].options ||
1626
1626
  !doc.$__.populated[path].options.options ||
1627
1627
  !doc.$__.populated[path].options.options.lean) {
1628
- const PopulatedModel = pop ? pop.options[populateModelSymbol] : doc.constructor.db.model(this.options.ref);
1628
+ const PopulatedModel = pop ? pop.options[populateModelSymbol] : owner.constructor.db.model(this.options.ref);
1629
1629
  ret = PopulatedModel.hydrate(value, null, options);
1630
1630
  ret.$__.wasPopulated = { value: ret._doc._id, options: { [populateModelSymbol]: PopulatedModel } };
1631
1631
  }
package/lib/types/map.js CHANGED
@@ -87,6 +87,22 @@ class MongooseMap extends Map {
87
87
  return this.$__schemaType.applyGetters(super.get(key), this.$__parent);
88
88
  }
89
89
 
90
+ /**
91
+ * Overwrites native Map's `has()` function to support ObjectIds as keys.
92
+ *
93
+ * @api public
94
+ * @method has
95
+ * @memberOf Map
96
+ */
97
+
98
+ has(key) {
99
+ if (isBsonType(key, 'ObjectId')) {
100
+ key = key.toString();
101
+ }
102
+
103
+ return super.has(key);
104
+ }
105
+
90
106
  /**
91
107
  * Overwrites native Map's `set()` function to support setters, `populate()`,
92
108
  * and change tracking. Note that Mongoose maps _only_ support strings and
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "mongoose",
3
3
  "description": "Mongoose MongoDB ODM",
4
- "version": "8.24.7",
4
+ "version": "8.24.8",
5
5
  "author": "Guillermo Rauch <guillermo@learnboost.com>",
6
6
  "keywords": [
7
7
  "mongodb",