mongoose 5.0.5 → 5.0.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/History.md CHANGED
@@ -1,3 +1,12 @@
1
+ 5.0.6 / 2018-02-15
2
+ ==================
3
+ * refactor(query.castUpdate): avoid creating error until necessary #6137
4
+ * docs(api): fix missing api docs #6136 [lineus](https://github.com/lineus)
5
+ * fix(schema): copy virtuals when using `clone()` #6133
6
+ * fix(update): avoid digging into buffers with upsert and replaceOne #6124
7
+ * fix(schema): support `enum` on arrays of strings #6102
8
+ * fix(update): cast `$addToSet: [1, 2]` -> `$addToSet: { $each: [1, 2] }` #6086
9
+
1
10
  5.0.5 / 2018-02-13
2
11
  ==================
3
12
  * docs: make > show up correctly in API docs #6114
@@ -17,6 +17,7 @@ var Types = {
17
17
  };
18
18
  var Mixed = require('./mixed');
19
19
  var cast = require('../cast');
20
+ var get = require('lodash.get');
20
21
  var util = require('util');
21
22
  var utils = require('../utils');
22
23
  var castToNumber = require('./operators/helpers').castToNumber;
@@ -119,6 +120,23 @@ SchemaArray.schemaName = 'Array';
119
120
  SchemaArray.prototype = Object.create(SchemaType.prototype);
120
121
  SchemaArray.prototype.constructor = SchemaArray;
121
122
 
123
+ /**
124
+ * Adds an enum validator if this is an array of strings. Equivalent to
125
+ * `SchemaString.prototype.enum()`
126
+ *
127
+ * @param {String|Object} [args...] enumeration values
128
+ * @return {SchemaType} this
129
+ */
130
+
131
+ SchemaArray.prototype.enum = function() {
132
+ const instance = get(this, 'caster.instance');
133
+ if (instance !== 'String') {
134
+ throw new Error('`enum` can only be set on an array of strings, not ' + instance);
135
+ }
136
+ this.caster.enum.apply(this.caster, arguments);
137
+ return this;
138
+ };
139
+
122
140
  /**
123
141
  * Check if the given value satisfies a required validator. The given value
124
142
  * must be not null nor undefined, and have a positive length.
package/lib/schema.js CHANGED
@@ -235,7 +235,6 @@ Schema.prototype.tree;
235
235
 
236
236
  Schema.prototype.clone = function() {
237
237
  var s = new Schema(this.paths, this.options);
238
- // Clone the call queue
239
238
  var cloneOpts = {};
240
239
  s.callQueue = this.callQueue.map(function(f) { return f; });
241
240
  s.methods = utils.clone(this.methods, cloneOpts);
@@ -244,6 +243,7 @@ Schema.prototype.clone = function() {
244
243
  s.plugins = Array.prototype.slice.call(this.plugins);
245
244
  s._indexes = utils.clone(this._indexes, cloneOpts);
246
245
  s.s.hooks = this.s.hooks.clone();
246
+ s.virtuals = utils.clone(this.virtuals, cloneOpts);
247
247
  return s;
248
248
  };
249
249
 
@@ -66,8 +66,10 @@ module.exports = function castUpdate(schema, obj, options, context) {
66
66
  op = ops[i];
67
67
  val = ret[op];
68
68
  hasDollarKey = hasDollarKey || op.charAt(0) === '$';
69
+
69
70
  if (val &&
70
71
  typeof val === 'object' &&
72
+ !Buffer.isBuffer(val) &&
71
73
  (!overwrite || hasDollarKey)) {
72
74
  hasKeys |= walkUpdatePath(schema, val, op, options.strict, context);
73
75
  } else if (overwrite && ret && typeof ret === 'object') {
@@ -111,8 +113,7 @@ function walkUpdatePath(schema, obj, op, strict, context, pref) {
111
113
  var key;
112
114
  var val;
113
115
 
114
- var hasError = false;
115
- var aggregatedError = new ValidationError();
116
+ var aggregatedError = null;
116
117
 
117
118
  var useNestedStrict = schema.options.useNestedStrict;
118
119
 
@@ -133,8 +134,7 @@ function walkUpdatePath(schema, obj, op, strict, context, pref) {
133
134
  $each: castUpdateVal(schematype, val.$each, op, context)
134
135
  };
135
136
  } catch (error) {
136
- hasError = true;
137
- _handleCastError(error, context, key, aggregatedError);
137
+ aggregatedError = _handleCastError(error, context, key, aggregatedError);
138
138
  }
139
139
 
140
140
  if (val.$slice != null) {
@@ -152,8 +152,7 @@ function walkUpdatePath(schema, obj, op, strict, context, pref) {
152
152
  try {
153
153
  obj[key] = castUpdateVal(schematype, val, op, context);
154
154
  } catch (error) {
155
- hasError = true;
156
- _handleCastError(error, context, key, aggregatedError);
155
+ aggregatedError = _handleCastError(error, context, key, aggregatedError);
157
156
  }
158
157
  }
159
158
  } else if ((op === '$currentDate') || (op in castOps && schematype)) {
@@ -161,8 +160,7 @@ function walkUpdatePath(schema, obj, op, strict, context, pref) {
161
160
  try {
162
161
  obj[key] = castUpdateVal(schematype, val, op, context);
163
162
  } catch (error) {
164
- hasError = true;
165
- _handleCastError(error, context, key, aggregatedError);
163
+ aggregatedError = _handleCastError(error, context, key, aggregatedError);
166
164
  }
167
165
 
168
166
  hasKeys = true;
@@ -228,14 +226,17 @@ function walkUpdatePath(schema, obj, op, strict, context, pref) {
228
226
  try {
229
227
  obj[key] = castUpdateVal(schematype, val, op, key, context);
230
228
  } catch (error) {
231
- hasError = true;
232
- _handleCastError(error, context, key, aggregatedError);
229
+ aggregatedError = _handleCastError(error, context, key, aggregatedError);
230
+ }
231
+
232
+ if (Array.isArray(obj[key]) && (op === '$addToSet' || op === '$push') && key !== '$each') {
233
+ obj[key] = { $each: obj[key] };
233
234
  }
234
235
  }
235
236
  }
236
237
  }
237
238
 
238
- if (hasError) {
239
+ if (aggregatedError != null) {
239
240
  throw aggregatedError;
240
241
  }
241
242
 
@@ -250,7 +251,9 @@ function _handleCastError(error, query, key, aggregatedError) {
250
251
  if (typeof query !== 'object' || !query.options.multipleCastError) {
251
252
  throw error;
252
253
  }
254
+ aggregatedError = aggregatedError || new ValidationError();
253
255
  aggregatedError.addError(key, error);
256
+ return aggregatedError;
254
257
  }
255
258
 
256
259
  /*!
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "mongoose",
3
3
  "description": "Mongoose MongoDB ODM",
4
- "version": "5.0.5",
4
+ "version": "5.0.6",
5
5
  "author": "Guillermo Rauch <guillermo@learnboost.com>",
6
6
  "keywords": [
7
7
  "mongodb",