mongoose 8.9.5 → 8.9.6

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/aggregate.js CHANGED
@@ -392,11 +392,23 @@ Aggregate.prototype.project = function(arg) {
392
392
  * @memberOf Aggregate
393
393
  * @instance
394
394
  * @param {Object} arg
395
+ * @param {Object|Array<Number>} arg.near GeoJSON point or coordinates array
395
396
  * @return {Aggregate}
396
397
  * @api public
397
398
  */
398
399
 
399
400
  Aggregate.prototype.near = function(arg) {
401
+ if (arg == null) {
402
+ throw new MongooseError('Aggregate `near()` must be called with non-nullish argument');
403
+ }
404
+ if (arg.near == null) {
405
+ throw new MongooseError('Aggregate `near()` argument must have a `near` property');
406
+ }
407
+ const coordinates = Array.isArray(arg.near) ? arg.near : arg.near.coordinates;
408
+ if (typeof arg.near === 'object' && (!Array.isArray(coordinates) || coordinates.length < 2 || coordinates.find(c => typeof c !== 'number'))) {
409
+ throw new MongooseError(`Aggregate \`near()\` argument has invalid coordinates, got "${coordinates}"`);
410
+ }
411
+
400
412
  const op = {};
401
413
  op.$geoNear = arg;
402
414
  return this.append(op);
@@ -0,0 +1,78 @@
1
+ 'use strict';
2
+
3
+ const MongooseBuffer = require('../types/buffer');
4
+
5
+ const UUID_FORMAT = /[0-9a-f]{8}-[0-9a-f]{4}-[0-9][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}/i;
6
+ const Binary = MongooseBuffer.Binary;
7
+
8
+ module.exports = function castUUID(value) {
9
+ if (value == null) {
10
+ return value;
11
+ }
12
+
13
+ function newBuffer(initbuff) {
14
+ const buff = new MongooseBuffer(initbuff);
15
+ buff._subtype = 4;
16
+ return buff;
17
+ }
18
+
19
+ if (typeof value === 'string') {
20
+ if (UUID_FORMAT.test(value)) {
21
+ return stringToBinary(value);
22
+ } else {
23
+ throw new Error(`"${value}" is not a valid UUID string`);
24
+ }
25
+ }
26
+
27
+ if (Buffer.isBuffer(value)) {
28
+ return newBuffer(value);
29
+ }
30
+
31
+ if (value instanceof Binary) {
32
+ return newBuffer(value.value(true));
33
+ }
34
+
35
+ // Re: gh-647 and gh-3030, we're ok with casting using `toString()`
36
+ // **unless** its the default Object.toString, because "[object Object]"
37
+ // doesn't really qualify as useful data
38
+ if (value.toString && value.toString !== Object.prototype.toString) {
39
+ if (UUID_FORMAT.test(value.toString())) {
40
+ return stringToBinary(value.toString());
41
+ }
42
+ }
43
+
44
+ throw new Error(`"${value}" cannot be casted to a UUID`);
45
+ };
46
+
47
+ module.exports.UUID_FORMAT = UUID_FORMAT;
48
+
49
+ /**
50
+ * Helper function to convert the input hex-string to a buffer
51
+ * @param {String} hex The hex string to convert
52
+ * @returns {Buffer} The hex as buffer
53
+ * @api private
54
+ */
55
+
56
+ function hex2buffer(hex) {
57
+ // use buffer built-in function to convert from hex-string to buffer
58
+ const buff = hex != null && Buffer.from(hex, 'hex');
59
+ return buff;
60
+ }
61
+
62
+ /**
63
+ * Convert a String to Binary
64
+ * @param {String} uuidStr The value to process
65
+ * @returns {MongooseBuffer} The binary to store
66
+ * @api private
67
+ */
68
+
69
+ function stringToBinary(uuidStr) {
70
+ // Protect against undefined & throwing err
71
+ if (typeof uuidStr !== 'string') uuidStr = '';
72
+ const hex = uuidStr.replace(/[{}-]/g, ''); // remove extra characters
73
+ const bytes = hex2buffer(hex);
74
+ const buff = new MongooseBuffer(bytes);
75
+ buff._subtype = 4;
76
+
77
+ return buff;
78
+ }
package/lib/document.js CHANGED
@@ -1147,7 +1147,7 @@ Document.prototype.$set = function $set(path, val, type, options) {
1147
1147
  } else if (pathtype === 'nested' && valForKey == null) {
1148
1148
  this.$set(pathName, valForKey, constructing, options);
1149
1149
  }
1150
- } else if (valForKey !== void 0) {
1150
+ } else {
1151
1151
  this.$set(pathName, valForKey, constructing, options);
1152
1152
  }
1153
1153
  }
@@ -7,43 +7,13 @@
7
7
  const MongooseBuffer = require('../types/buffer');
8
8
  const SchemaType = require('../schemaType');
9
9
  const CastError = SchemaType.CastError;
10
+ const castUUID = require('../cast/uuid');
10
11
  const utils = require('../utils');
11
12
  const handleBitwiseOperator = require('./operators/bitwise');
12
13
 
13
- const UUID_FORMAT = /[0-9a-f]{8}-[0-9a-f]{4}-[0-9][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}/i;
14
+ const UUID_FORMAT = castUUID.UUID_FORMAT;
14
15
  const Binary = MongooseBuffer.Binary;
15
16
 
16
- /**
17
- * Helper function to convert the input hex-string to a buffer
18
- * @param {String} hex The hex string to convert
19
- * @returns {Buffer} The hex as buffer
20
- * @api private
21
- */
22
-
23
- function hex2buffer(hex) {
24
- // use buffer built-in function to convert from hex-string to buffer
25
- const buff = hex != null && Buffer.from(hex, 'hex');
26
- return buff;
27
- }
28
-
29
- /**
30
- * Convert a String to Binary
31
- * @param {String} uuidStr The value to process
32
- * @returns {MongooseBuffer} The binary to store
33
- * @api private
34
- */
35
-
36
- function stringToBinary(uuidStr) {
37
- // Protect against undefined & throwing err
38
- if (typeof uuidStr !== 'string') uuidStr = '';
39
- const hex = uuidStr.replace(/[{}-]/g, ''); // remove extra characters
40
- const bytes = hex2buffer(hex);
41
- const buff = new MongooseBuffer(bytes);
42
- buff._subtype = 4;
43
-
44
- return buff;
45
- }
46
-
47
17
  /**
48
18
  * Convert binary to a uuid string
49
19
  * @param {Buffer|Binary|String} uuidBin The value to process
@@ -109,44 +79,7 @@ SchemaUUID.prototype.constructor = SchemaUUID;
109
79
  * ignore
110
80
  */
111
81
 
112
- SchemaUUID._cast = function(value) {
113
- if (value == null) {
114
- return value;
115
- }
116
-
117
- function newBuffer(initbuff) {
118
- const buff = new MongooseBuffer(initbuff);
119
- buff._subtype = 4;
120
- return buff;
121
- }
122
-
123
- if (typeof value === 'string') {
124
- if (UUID_FORMAT.test(value)) {
125
- return stringToBinary(value);
126
- } else {
127
- throw new CastError(SchemaUUID.schemaName, value, this.path);
128
- }
129
- }
130
-
131
- if (Buffer.isBuffer(value)) {
132
- return newBuffer(value);
133
- }
134
-
135
- if (value instanceof Binary) {
136
- return newBuffer(value.value(true));
137
- }
138
-
139
- // Re: gh-647 and gh-3030, we're ok with casting using `toString()`
140
- // **unless** its the default Object.toString, because "[object Object]"
141
- // doesn't really qualify as useful data
142
- if (value.toString && value.toString !== Object.prototype.toString) {
143
- if (UUID_FORMAT.test(value.toString())) {
144
- return stringToBinary(value.toString());
145
- }
146
- }
147
-
148
- throw new CastError(SchemaUUID.schemaName, value, this.path);
149
- };
82
+ SchemaUUID._cast = castUUID;
150
83
 
151
84
  /**
152
85
  * Attaches a getter for all UUID instances.
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "mongoose",
3
3
  "description": "Mongoose MongoDB ODM",
4
- "version": "8.9.5",
4
+ "version": "8.9.6",
5
5
  "author": "Guillermo Rauch <guillermo@learnboost.com>",
6
6
  "keywords": [
7
7
  "mongodb",
package/valnotes.md DELETED
@@ -1,85 +0,0 @@
1
- ## sift where
2
-
3
- ```
4
- it('match prevents using $where', async function() {
5
- const ParentSchema = new Schema({
6
- name: String,
7
- child: {
8
- type: mongoose.Schema.Types.ObjectId,
9
- ref: 'Child'
10
- },
11
- children: [{
12
- type: mongoose.Schema.Types.ObjectId,
13
- ref: 'Child'
14
- }]
15
- });
16
-
17
- const ChildSchema = new Schema({
18
- name: String
19
- });
20
- ChildSchema.virtual('parent', {
21
- ref: 'Parent',
22
- localField: '_id',
23
- foreignField: 'parent'
24
- });
25
-
26
- const Parent = db.model('Parent', ParentSchema);
27
- const Child = db.model('Child', ChildSchema);
28
-
29
- const child = await Child.create({ name: 'Luke' });
30
- const parent = await Parent.create({ name: 'Anakin', child: child._id });
31
-
32
- await assert.rejects(
33
- () => Parent.findOne().populate({ path: 'child', match: () => ({ $where: 'typeof console !== "undefined" ? doesNotExist("foo") : true;' }) }),
34
- /Cannot use \$where filter with populate\(\) match/
35
- );
36
- await assert.rejects(
37
- () => Parent.find().populate({ path: 'child', match: () => ({ $where: 'typeof console !== "undefined" ? doesNotExist("foo") : true;' }) }),
38
- /Cannot use \$where filter with populate\(\) match/
39
- );
40
- await assert.rejects(
41
- () => parent.populate({ path: 'child', match: () => ({ $where: 'typeof console !== "undefined" ? doesNotExist("foo") : true;' }) }),
42
- /Cannot use \$where filter with populate\(\) match/
43
- );
44
- await assert.rejects(
45
- () => Child.find().populate({ path: 'parent', match: () => ({ $where: 'typeof console !== "undefined" ? doesNotExist("foo") : true;' }) }),
46
- /Cannot use \$where filter with populate\(\) match/
47
- );
48
- await assert.rejects(
49
- () => Child.find().populate({ path: 'parent', match: () => ({ $or: [{ $where: 'typeof console !== "undefined" ? doesNotExist("foo") : true;' }] }) }),
50
- /Cannot use \$where filter with populate\(\) match/
51
- );
52
- await assert.rejects(
53
- () => Child.find().populate({ path: 'parent', match: () => ({ $and: [{ $where: 'typeof console !== "undefined" ? doesNotExist("foo") : true;' }] }) }),
54
- /Cannot use \$where filter with populate\(\) match/
55
- );
56
-
57
- class MyClass {}
58
- MyClass.prototype.$where = 'typeof console !== "undefined" ? doesNotExist("foo") : true;';
59
- // OK because sift only looks through own properties
60
- await Child.find().populate({ path: 'parent', match: () => new MyClass() });
61
- });
62
- ```
63
-
64
- ```
65
- /**
66
- * Throw an error if there are any $where keys
67
- */
68
-
69
- function throwOn$where(match) {
70
- if (match == null) {
71
- return;
72
- }
73
- if (typeof match !== 'object') {
74
- return;
75
- }
76
- for (const key of Object.keys(match)) {
77
- if (key === '$where') {
78
- throw new MongooseError('Cannot use $where filter with populate() match');
79
- }
80
- if (match[key] != null && typeof match[key] === 'object') {
81
- throwOn$where(match[key]);
82
- }
83
- }
84
- }
85
- ```