mongoose 8.19.4 → 8.20.0
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/dist/browser.umd.js +1 -1
- package/lib/cast.js +1 -1
- package/lib/error/divergentArray.js +2 -2
- package/lib/helpers/discriminator/mergeDiscriminatorSchema.js +10 -0
- package/lib/helpers/query/castUpdate.js +1 -1
- package/lib/schema/array.js +9 -4
- package/lib/schema/bigint.js +4 -2
- package/lib/schema/boolean.js +4 -2
- package/lib/schema/buffer.js +4 -2
- package/lib/schema/date.js +4 -2
- package/lib/schema/decimal128.js +4 -2
- package/lib/schema/documentArray.js +19 -7
- package/lib/schema/documentArrayElement.js +4 -2
- package/lib/schema/double.js +4 -2
- package/lib/schema/int32.js +4 -2
- package/lib/schema/map.js +87 -6
- package/lib/schema/mixed.js +4 -2
- package/lib/schema/number.js +4 -2
- package/lib/schema/objectId.js +4 -2
- package/lib/schema/string.js +4 -2
- package/lib/schema/subdocument.js +5 -3
- package/lib/schema/union.js +11 -3
- package/lib/schema/uuid.js +4 -2
- package/lib/schema.js +10 -52
- package/lib/schemaType.js +28 -2
- package/lib/types/documentArray/methods/index.js +28 -16
- package/lib/types/map.js +1 -2
- package/package.json +1 -1
- package/types/document.d.ts +1 -1
package/lib/cast.js
CHANGED
|
@@ -210,7 +210,7 @@ module.exports = function cast(schema, obj, options, context) {
|
|
|
210
210
|
}
|
|
211
211
|
|
|
212
212
|
if (geo) {
|
|
213
|
-
const numbertype = new Types.Number('__QueryCasting__');
|
|
213
|
+
const numbertype = new Types.Number('__QueryCasting__', null, null, schema);
|
|
214
214
|
let value = val[geo];
|
|
215
215
|
|
|
216
216
|
if (val.$maxDistance != null) {
|
|
@@ -23,8 +23,8 @@ class DivergentArrayError extends MongooseError {
|
|
|
23
23
|
+ 'the entire array is not supported. The following '
|
|
24
24
|
+ 'path(s) would have been modified unsafely:\n'
|
|
25
25
|
+ ' ' + paths.join('\n ') + '\n'
|
|
26
|
-
+ 'Use Model.updateOne() to update these arrays instead.'
|
|
27
|
-
|
|
26
|
+
+ 'Use Model.updateOne() to update these arrays instead. '
|
|
27
|
+
+ 'See https://mongoosejs.com/docs/faq.html#divergent-array-error for more information.';
|
|
28
28
|
super(msg);
|
|
29
29
|
}
|
|
30
30
|
}
|
|
@@ -3,6 +3,7 @@ const schemaMerge = require('../schema/merge');
|
|
|
3
3
|
const specialProperties = require('../../helpers/specialProperties');
|
|
4
4
|
const isBsonType = require('../../helpers/isBsonType');
|
|
5
5
|
const ObjectId = require('../../types/objectid');
|
|
6
|
+
const SchemaType = require('../../schemaType');
|
|
6
7
|
const isObject = require('../../helpers/isObject');
|
|
7
8
|
/**
|
|
8
9
|
* Merges `from` into `to` without overwriting existing properties.
|
|
@@ -69,6 +70,15 @@ module.exports = function mergeDiscriminatorSchema(to, from, path, seen) {
|
|
|
69
70
|
} else if (isBsonType(from[key], 'ObjectId')) {
|
|
70
71
|
to[key] = new ObjectId(from[key]);
|
|
71
72
|
continue;
|
|
73
|
+
} else if (from[key] instanceof SchemaType) {
|
|
74
|
+
if (to[key] == null) {
|
|
75
|
+
to[key] = from[key].clone();
|
|
76
|
+
}
|
|
77
|
+
// For container types with nested schemas, we need to continue to the
|
|
78
|
+
// recursive merge below to properly merge the nested schemas
|
|
79
|
+
if (!from[key].$isMongooseDocumentArray && !from[key].$isSingleNested) {
|
|
80
|
+
continue;
|
|
81
|
+
}
|
|
72
82
|
}
|
|
73
83
|
}
|
|
74
84
|
mergeDiscriminatorSchema(to[key], from[key], path ? path + '.' + key : key, seen);
|
|
@@ -428,7 +428,7 @@ function walkUpdatePath(schema, obj, op, options, context, filter, prefix) {
|
|
|
428
428
|
if (obj[key] == null) {
|
|
429
429
|
throw new CastError('String', obj[key], `${prefix}${key}.$rename`);
|
|
430
430
|
}
|
|
431
|
-
const schematype = new SchemaString(`${prefix}${key}.$rename
|
|
431
|
+
const schematype = new SchemaString(`${prefix}${key}.$rename`, null, null, schema);
|
|
432
432
|
obj[key] = schematype.castForQuery(null, obj[key], context);
|
|
433
433
|
continue;
|
|
434
434
|
}
|
package/lib/schema/array.js
CHANGED
|
@@ -38,11 +38,12 @@ const emptyOpts = Object.freeze({});
|
|
|
38
38
|
* @param {SchemaType} cast
|
|
39
39
|
* @param {Object} options
|
|
40
40
|
* @param {Object} schemaOptions
|
|
41
|
+
* @param {Schema} parentSchema
|
|
41
42
|
* @inherits SchemaType
|
|
42
43
|
* @api public
|
|
43
44
|
*/
|
|
44
45
|
|
|
45
|
-
function SchemaArray(key, cast, options, schemaOptions) {
|
|
46
|
+
function SchemaArray(key, cast, options, schemaOptions, parentSchema) {
|
|
46
47
|
// lazy load
|
|
47
48
|
EmbeddedDoc || (EmbeddedDoc = require('../types').Embedded);
|
|
48
49
|
|
|
@@ -92,7 +93,11 @@ function SchemaArray(key, cast, options, schemaOptions) {
|
|
|
92
93
|
!caster.$isArraySubdocument &&
|
|
93
94
|
!caster.$isSchemaMap) {
|
|
94
95
|
const path = this.caster instanceof EmbeddedDoc ? null : key;
|
|
95
|
-
|
|
96
|
+
if (caster === SchemaArray) {
|
|
97
|
+
this.caster = new caster(path, castOptions, schemaOptions, null, parentSchema);
|
|
98
|
+
} else {
|
|
99
|
+
this.caster = new caster(path, castOptions, schemaOptions, parentSchema);
|
|
100
|
+
}
|
|
96
101
|
} else {
|
|
97
102
|
this.caster = caster;
|
|
98
103
|
if (!(this.caster instanceof EmbeddedDoc)) {
|
|
@@ -105,7 +110,7 @@ function SchemaArray(key, cast, options, schemaOptions) {
|
|
|
105
110
|
|
|
106
111
|
this.$isMongooseArray = true;
|
|
107
112
|
|
|
108
|
-
SchemaType.call(this, key, options, 'Array');
|
|
113
|
+
SchemaType.call(this, key, options, 'Array', parentSchema);
|
|
109
114
|
|
|
110
115
|
let defaultArr;
|
|
111
116
|
let fn;
|
|
@@ -494,7 +499,7 @@ SchemaArray.prototype.discriminator = function(...args) {
|
|
|
494
499
|
|
|
495
500
|
SchemaArray.prototype.clone = function() {
|
|
496
501
|
const options = Object.assign({}, this.options);
|
|
497
|
-
const schematype = new this.constructor(this.path, this.caster, options, this.schemaOptions);
|
|
502
|
+
const schematype = new this.constructor(this.path, this.caster, options, this.schemaOptions, this.parentSchema);
|
|
498
503
|
schematype.validators = this.validators.slice();
|
|
499
504
|
if (this.requiredValidator !== undefined) {
|
|
500
505
|
schematype.requiredValidator = this.requiredValidator;
|
package/lib/schema/bigint.js
CHANGED
|
@@ -14,12 +14,14 @@ const createJSONSchemaTypeDefinition = require('../helpers/createJSONSchemaTypeD
|
|
|
14
14
|
*
|
|
15
15
|
* @param {String} path
|
|
16
16
|
* @param {Object} options
|
|
17
|
+
* @param {Object} schemaOptions
|
|
18
|
+
* @param {Schema} parentSchema
|
|
17
19
|
* @inherits SchemaType
|
|
18
20
|
* @api public
|
|
19
21
|
*/
|
|
20
22
|
|
|
21
|
-
function SchemaBigInt(path, options) {
|
|
22
|
-
SchemaType.call(this, path, options, 'BigInt');
|
|
23
|
+
function SchemaBigInt(path, options, _schemaOptions, parentSchema) {
|
|
24
|
+
SchemaType.call(this, path, options, 'BigInt', parentSchema);
|
|
23
25
|
}
|
|
24
26
|
|
|
25
27
|
/**
|
package/lib/schema/boolean.js
CHANGED
|
@@ -14,12 +14,14 @@ const createJSONSchemaTypeDefinition = require('../helpers/createJSONSchemaTypeD
|
|
|
14
14
|
*
|
|
15
15
|
* @param {String} path
|
|
16
16
|
* @param {Object} options
|
|
17
|
+
* @param {Object} schemaOptions
|
|
18
|
+
* @param {Schema} parentSchema
|
|
17
19
|
* @inherits SchemaType
|
|
18
20
|
* @api public
|
|
19
21
|
*/
|
|
20
22
|
|
|
21
|
-
function SchemaBoolean(path, options) {
|
|
22
|
-
SchemaType.call(this, path, options, 'Boolean');
|
|
23
|
+
function SchemaBoolean(path, options, _schemaOptions, parentSchema) {
|
|
24
|
+
SchemaType.call(this, path, options, 'Boolean', parentSchema);
|
|
23
25
|
}
|
|
24
26
|
|
|
25
27
|
/**
|
package/lib/schema/buffer.js
CHANGED
|
@@ -19,12 +19,14 @@ const CastError = SchemaType.CastError;
|
|
|
19
19
|
*
|
|
20
20
|
* @param {String} key
|
|
21
21
|
* @param {Object} options
|
|
22
|
+
* @param {Object} schemaOptions
|
|
23
|
+
* @param {Schema} parentSchema
|
|
22
24
|
* @inherits SchemaType
|
|
23
25
|
* @api public
|
|
24
26
|
*/
|
|
25
27
|
|
|
26
|
-
function SchemaBuffer(key, options) {
|
|
27
|
-
SchemaType.call(this, key, options, 'Buffer');
|
|
28
|
+
function SchemaBuffer(key, options, _schemaOptions, parentSchema) {
|
|
29
|
+
SchemaType.call(this, key, options, 'Buffer', parentSchema);
|
|
28
30
|
}
|
|
29
31
|
|
|
30
32
|
/**
|
package/lib/schema/date.js
CHANGED
|
@@ -19,12 +19,14 @@ const CastError = SchemaType.CastError;
|
|
|
19
19
|
*
|
|
20
20
|
* @param {String} key
|
|
21
21
|
* @param {Object} options
|
|
22
|
+
* @param {Object} schemaOptions
|
|
23
|
+
* @param {Schema} parentSchema
|
|
22
24
|
* @inherits SchemaType
|
|
23
25
|
* @api public
|
|
24
26
|
*/
|
|
25
27
|
|
|
26
|
-
function SchemaDate(key, options) {
|
|
27
|
-
SchemaType.call(this, key, options, 'Date');
|
|
28
|
+
function SchemaDate(key, options, _schemaOptions, parentSchema) {
|
|
29
|
+
SchemaType.call(this, key, options, 'Date', parentSchema);
|
|
28
30
|
}
|
|
29
31
|
|
|
30
32
|
/**
|
package/lib/schema/decimal128.js
CHANGED
|
@@ -15,12 +15,14 @@ const isBsonType = require('../helpers/isBsonType');
|
|
|
15
15
|
*
|
|
16
16
|
* @param {String} key
|
|
17
17
|
* @param {Object} options
|
|
18
|
+
* @param {Object} schemaOptions
|
|
19
|
+
* @param {Schema} parentSchema
|
|
18
20
|
* @inherits SchemaType
|
|
19
21
|
* @api public
|
|
20
22
|
*/
|
|
21
23
|
|
|
22
|
-
function SchemaDecimal128(key, options) {
|
|
23
|
-
SchemaType.call(this, key, options, 'Decimal128');
|
|
24
|
+
function SchemaDecimal128(key, options, _schemaOptions, parentSchema) {
|
|
25
|
+
SchemaType.call(this, key, options, 'Decimal128', parentSchema);
|
|
24
26
|
}
|
|
25
27
|
|
|
26
28
|
/**
|
|
@@ -35,11 +35,12 @@ let Subdocument;
|
|
|
35
35
|
* @param {Schema} schema
|
|
36
36
|
* @param {Object} options
|
|
37
37
|
* @param {Object} schemaOptions
|
|
38
|
+
* @param {Schema} parentSchema
|
|
38
39
|
* @inherits SchemaArray
|
|
39
40
|
* @api public
|
|
40
41
|
*/
|
|
41
42
|
|
|
42
|
-
function SchemaDocumentArray(key, schema, options, schemaOptions) {
|
|
43
|
+
function SchemaDocumentArray(key, schema, options, schemaOptions, parentSchema) {
|
|
43
44
|
if (schema.options && schema.options.timeseries) {
|
|
44
45
|
throw new InvalidSchemaOptionError(key, 'timeseries');
|
|
45
46
|
}
|
|
@@ -59,7 +60,7 @@ function SchemaDocumentArray(key, schema, options, schemaOptions) {
|
|
|
59
60
|
const EmbeddedDocument = _createConstructor(schema, options);
|
|
60
61
|
EmbeddedDocument.prototype.$basePath = key;
|
|
61
62
|
|
|
62
|
-
SchemaArray.call(this, key, EmbeddedDocument, options);
|
|
63
|
+
SchemaArray.call(this, key, EmbeddedDocument, options, null, parentSchema);
|
|
63
64
|
|
|
64
65
|
this.schema = schema;
|
|
65
66
|
// EmbeddedDocument schematype options
|
|
@@ -83,10 +84,15 @@ function SchemaDocumentArray(key, schema, options, schemaOptions) {
|
|
|
83
84
|
}
|
|
84
85
|
|
|
85
86
|
const $parentSchemaType = this;
|
|
86
|
-
this.$embeddedSchemaType = new DocumentArrayElement(
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
87
|
+
this.$embeddedSchemaType = new DocumentArrayElement(
|
|
88
|
+
key + '.$',
|
|
89
|
+
{
|
|
90
|
+
...(schemaOptions || {}),
|
|
91
|
+
$parentSchemaType
|
|
92
|
+
},
|
|
93
|
+
schemaOptions,
|
|
94
|
+
parentSchema
|
|
95
|
+
);
|
|
90
96
|
|
|
91
97
|
this.$embeddedSchemaType.caster = this.Constructor;
|
|
92
98
|
this.$embeddedSchemaType.schema = this.schema;
|
|
@@ -529,7 +535,13 @@ SchemaDocumentArray.prototype.cast = function(value, doc, init, prev, options) {
|
|
|
529
535
|
|
|
530
536
|
SchemaDocumentArray.prototype.clone = function() {
|
|
531
537
|
const options = Object.assign({}, this.options);
|
|
532
|
-
const schematype = new this.constructor(
|
|
538
|
+
const schematype = new this.constructor(
|
|
539
|
+
this.path,
|
|
540
|
+
this.schema,
|
|
541
|
+
options,
|
|
542
|
+
this.schemaOptions,
|
|
543
|
+
this.parentSchema
|
|
544
|
+
);
|
|
533
545
|
schematype.validators = this.validators.slice();
|
|
534
546
|
if (this.requiredValidator !== undefined) {
|
|
535
547
|
schematype.requiredValidator = this.requiredValidator;
|
|
@@ -14,18 +14,20 @@ const getConstructor = require('../helpers/discriminator/getConstructor');
|
|
|
14
14
|
*
|
|
15
15
|
* @param {String} path
|
|
16
16
|
* @param {Object} options
|
|
17
|
+
* @param {Object} schemaOptions
|
|
18
|
+
* @param {Schema} parentSchema
|
|
17
19
|
* @inherits SchemaType
|
|
18
20
|
* @api public
|
|
19
21
|
*/
|
|
20
22
|
|
|
21
|
-
function SchemaDocumentArrayElement(path, options) {
|
|
23
|
+
function SchemaDocumentArrayElement(path, options, _schemaOptions, parentSchema) {
|
|
22
24
|
this.$parentSchemaType = options && options.$parentSchemaType;
|
|
23
25
|
if (!this.$parentSchemaType) {
|
|
24
26
|
throw new MongooseError('Cannot create DocumentArrayElement schematype without a parent');
|
|
25
27
|
}
|
|
26
28
|
delete options.$parentSchemaType;
|
|
27
29
|
|
|
28
|
-
SchemaType.call(this, path, options, 'DocumentArrayElement');
|
|
30
|
+
SchemaType.call(this, path, options, 'DocumentArrayElement', parentSchema);
|
|
29
31
|
|
|
30
32
|
this.$isMongooseDocumentArrayElement = true;
|
|
31
33
|
}
|
package/lib/schema/double.js
CHANGED
|
@@ -14,12 +14,14 @@ const createJSONSchemaTypeDefinition = require('../helpers/createJSONSchemaTypeD
|
|
|
14
14
|
*
|
|
15
15
|
* @param {String} path
|
|
16
16
|
* @param {Object} options
|
|
17
|
+
* @param {Object} schemaOptions
|
|
18
|
+
* @param {Schema} parentSchema
|
|
17
19
|
* @inherits SchemaType
|
|
18
20
|
* @api public
|
|
19
21
|
*/
|
|
20
22
|
|
|
21
|
-
function SchemaDouble(path, options) {
|
|
22
|
-
SchemaType.call(this, path, options, 'Double');
|
|
23
|
+
function SchemaDouble(path, options, _schemaOptions, parentSchema) {
|
|
24
|
+
SchemaType.call(this, path, options, 'Double', parentSchema);
|
|
23
25
|
}
|
|
24
26
|
|
|
25
27
|
/**
|
package/lib/schema/int32.js
CHANGED
|
@@ -15,12 +15,14 @@ const handleBitwiseOperator = require('./operators/bitwise');
|
|
|
15
15
|
*
|
|
16
16
|
* @param {String} path
|
|
17
17
|
* @param {Object} options
|
|
18
|
+
* @param {Object} schemaOptions
|
|
19
|
+
* @param {Schema} parentSchema
|
|
18
20
|
* @inherits SchemaType
|
|
19
21
|
* @api public
|
|
20
22
|
*/
|
|
21
23
|
|
|
22
|
-
function SchemaInt32(path, options) {
|
|
23
|
-
SchemaType.call(this, path, options, 'Int32');
|
|
24
|
+
function SchemaInt32(path, options, _schemaOptions, parentSchema) {
|
|
25
|
+
SchemaType.call(this, path, options, 'Int32', parentSchema);
|
|
24
26
|
}
|
|
25
27
|
|
|
26
28
|
/**
|
package/lib/schema/map.js
CHANGED
|
@@ -8,21 +8,51 @@ const MongooseMap = require('../types/map');
|
|
|
8
8
|
const SchemaMapOptions = require('../options/schemaMapOptions');
|
|
9
9
|
const SchemaType = require('../schemaType');
|
|
10
10
|
const createJSONSchemaTypeDefinition = require('../helpers/createJSONSchemaTypeDefinition');
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
*/
|
|
11
|
+
const MongooseError = require('../error/mongooseError');
|
|
12
|
+
const Schema = require('../schema');
|
|
13
|
+
const utils = require('../utils');
|
|
15
14
|
|
|
16
15
|
class SchemaMap extends SchemaType {
|
|
17
|
-
|
|
18
|
-
|
|
16
|
+
/**
|
|
17
|
+
* Map SchemaType constructor.
|
|
18
|
+
*
|
|
19
|
+
* @param {String} path
|
|
20
|
+
* @param {Object} options
|
|
21
|
+
* @param {Object} schemaOptions
|
|
22
|
+
* @param {Schema} parentSchema
|
|
23
|
+
* @inherits SchemaType
|
|
24
|
+
* @api public
|
|
25
|
+
*/
|
|
26
|
+
|
|
27
|
+
constructor(key, options, schemaOptions, parentSchema) {
|
|
28
|
+
super(key, options, 'Map', parentSchema);
|
|
19
29
|
this.$isSchemaMap = true;
|
|
30
|
+
// Create the nested schema type for the map values
|
|
31
|
+
this._createNestedSchemaType(parentSchema, key, options, schemaOptions);
|
|
20
32
|
}
|
|
21
33
|
|
|
34
|
+
/**
|
|
35
|
+
* Sets a default option for all Map instances.
|
|
36
|
+
*
|
|
37
|
+
* @param {String} option The option you'd like to set the value for
|
|
38
|
+
* @param {Any} value value for option
|
|
39
|
+
* @return {undefined}
|
|
40
|
+
* @function set
|
|
41
|
+
* @api public
|
|
42
|
+
*/
|
|
43
|
+
|
|
22
44
|
set(option, value) {
|
|
23
45
|
return SchemaType.set(option, value);
|
|
24
46
|
}
|
|
25
47
|
|
|
48
|
+
/**
|
|
49
|
+
* Casts to Map
|
|
50
|
+
*
|
|
51
|
+
* @param {Object} value
|
|
52
|
+
* @param {Object} model this value is optional
|
|
53
|
+
* @api private
|
|
54
|
+
*/
|
|
55
|
+
|
|
26
56
|
cast(val, doc, init, prev, options) {
|
|
27
57
|
if (val instanceof MongooseMap) {
|
|
28
58
|
return val;
|
|
@@ -65,6 +95,12 @@ class SchemaMap extends SchemaType {
|
|
|
65
95
|
return new MongooseMap(val, path, doc, this.$__schemaType, options);
|
|
66
96
|
}
|
|
67
97
|
|
|
98
|
+
/**
|
|
99
|
+
* Creates a copy of this map schema type.
|
|
100
|
+
*
|
|
101
|
+
* @api private
|
|
102
|
+
*/
|
|
103
|
+
|
|
68
104
|
clone() {
|
|
69
105
|
const schematype = super.clone();
|
|
70
106
|
|
|
@@ -76,7 +112,10 @@ class SchemaMap extends SchemaType {
|
|
|
76
112
|
|
|
77
113
|
/**
|
|
78
114
|
* Returns the embedded schema type (i.e. the `.$*` path)
|
|
115
|
+
*
|
|
116
|
+
* @api public
|
|
79
117
|
*/
|
|
118
|
+
|
|
80
119
|
getEmbeddedSchemaType() {
|
|
81
120
|
return this.$__schemaType;
|
|
82
121
|
}
|
|
@@ -100,6 +139,12 @@ class SchemaMap extends SchemaType {
|
|
|
100
139
|
return result;
|
|
101
140
|
}
|
|
102
141
|
|
|
142
|
+
/**
|
|
143
|
+
* Returns the auto encryption type for this schema type.
|
|
144
|
+
*
|
|
145
|
+
* @api public
|
|
146
|
+
*/
|
|
147
|
+
|
|
103
148
|
autoEncryptionType() {
|
|
104
149
|
return 'object';
|
|
105
150
|
}
|
|
@@ -111,10 +156,46 @@ class SchemaMap extends SchemaType {
|
|
|
111
156
|
*
|
|
112
157
|
* @api public
|
|
113
158
|
*/
|
|
159
|
+
|
|
114
160
|
SchemaMap.schemaName = 'Map';
|
|
115
161
|
|
|
116
162
|
SchemaMap.prototype.OptionsConstructor = SchemaMapOptions;
|
|
117
163
|
|
|
118
164
|
SchemaMap.defaultOptions = {};
|
|
119
165
|
|
|
166
|
+
/*!
|
|
167
|
+
* ignore
|
|
168
|
+
*/
|
|
169
|
+
|
|
170
|
+
SchemaMap.prototype._createNestedSchemaType = function _createNestedSchemaType(schema, path, obj, options) {
|
|
171
|
+
const mapPath = path + '.$*';
|
|
172
|
+
let _mapType = { type: {} };
|
|
173
|
+
if (utils.hasUserDefinedProperty(obj, 'of')) {
|
|
174
|
+
const isInlineSchema = utils.isPOJO(obj.of) &&
|
|
175
|
+
Object.keys(obj.of).length > 0 &&
|
|
176
|
+
!utils.hasUserDefinedProperty(obj.of, schema.options.typeKey);
|
|
177
|
+
if (isInlineSchema) {
|
|
178
|
+
_mapType = { [schema.options.typeKey]: new Schema(obj.of) };
|
|
179
|
+
} else if (utils.isPOJO(obj.of)) {
|
|
180
|
+
_mapType = Object.assign({}, obj.of);
|
|
181
|
+
} else {
|
|
182
|
+
_mapType = { [schema.options.typeKey]: obj.of };
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
if (_mapType[schema.options.typeKey] && _mapType[schema.options.typeKey].instanceOfSchema) {
|
|
186
|
+
const subdocumentSchema = _mapType[schema.options.typeKey];
|
|
187
|
+
subdocumentSchema.eachPath((subpath, type) => {
|
|
188
|
+
if (type.options.select === true || type.options.select === false) {
|
|
189
|
+
throw new MongooseError('Cannot use schema-level projections (`select: true` or `select: false`) within maps at path "' + path + '.' + subpath + '"');
|
|
190
|
+
}
|
|
191
|
+
});
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
if (utils.hasUserDefinedProperty(obj, 'ref')) {
|
|
195
|
+
_mapType.ref = obj.ref;
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
this.$__schemaType = schema.interpretAsType(mapPath, _mapType, options);
|
|
199
|
+
};
|
|
200
|
+
|
|
120
201
|
module.exports = SchemaMap;
|
package/lib/schema/mixed.js
CHANGED
|
@@ -14,11 +14,13 @@ const utils = require('../utils');
|
|
|
14
14
|
*
|
|
15
15
|
* @param {String} path
|
|
16
16
|
* @param {Object} options
|
|
17
|
+
* @param {Object} _schemaOptions
|
|
18
|
+
* @param {Schema} parentSchema
|
|
17
19
|
* @inherits SchemaType
|
|
18
20
|
* @api public
|
|
19
21
|
*/
|
|
20
22
|
|
|
21
|
-
function SchemaMixed(path, options) {
|
|
23
|
+
function SchemaMixed(path, options, _schemaOptions, parentSchema) {
|
|
22
24
|
if (options && options.default) {
|
|
23
25
|
const def = options.default;
|
|
24
26
|
if (Array.isArray(def) && def.length === 0) {
|
|
@@ -32,7 +34,7 @@ function SchemaMixed(path, options) {
|
|
|
32
34
|
}
|
|
33
35
|
}
|
|
34
36
|
|
|
35
|
-
SchemaType.call(this, path, options, 'Mixed');
|
|
37
|
+
SchemaType.call(this, path, options, 'Mixed', parentSchema);
|
|
36
38
|
|
|
37
39
|
this[symbols.schemaMixedSymbol] = true;
|
|
38
40
|
}
|
package/lib/schema/number.js
CHANGED
|
@@ -19,12 +19,14 @@ const CastError = SchemaType.CastError;
|
|
|
19
19
|
*
|
|
20
20
|
* @param {String} key
|
|
21
21
|
* @param {Object} options
|
|
22
|
+
* @param {Object} schemaOptions
|
|
23
|
+
* @param {Schema} parentSchema
|
|
22
24
|
* @inherits SchemaType
|
|
23
25
|
* @api public
|
|
24
26
|
*/
|
|
25
27
|
|
|
26
|
-
function SchemaNumber(key, options) {
|
|
27
|
-
SchemaType.call(this, key, options, 'Number');
|
|
28
|
+
function SchemaNumber(key, options, _schemaOptions, parentSchema) {
|
|
29
|
+
SchemaType.call(this, key, options, 'Number', parentSchema);
|
|
28
30
|
}
|
|
29
31
|
|
|
30
32
|
/**
|
package/lib/schema/objectId.js
CHANGED
|
@@ -21,11 +21,13 @@ let Document;
|
|
|
21
21
|
*
|
|
22
22
|
* @param {String} key
|
|
23
23
|
* @param {Object} options
|
|
24
|
+
* @param {Object} schemaOptions
|
|
25
|
+
* @param {Schema} parentSchema
|
|
24
26
|
* @inherits SchemaType
|
|
25
27
|
* @api public
|
|
26
28
|
*/
|
|
27
29
|
|
|
28
|
-
function SchemaObjectId(key, options) {
|
|
30
|
+
function SchemaObjectId(key, options, _schemaOptions, parentSchema) {
|
|
29
31
|
const isKeyHexStr = typeof key === 'string' && key.length === 24 && /^[a-f0-9]+$/i.test(key);
|
|
30
32
|
const suppressWarning = options && options.suppressWarning;
|
|
31
33
|
if ((isKeyHexStr || typeof key === 'undefined') && !suppressWarning) {
|
|
@@ -34,7 +36,7 @@ function SchemaObjectId(key, options) {
|
|
|
34
36
|
'`Mongoose.Schema.ObjectId`. Set the `suppressWarning` option if ' +
|
|
35
37
|
'you\'re trying to create a hex char path in your schema.');
|
|
36
38
|
}
|
|
37
|
-
SchemaType.call(this, key, options, 'ObjectId');
|
|
39
|
+
SchemaType.call(this, key, options, 'ObjectId', parentSchema);
|
|
38
40
|
}
|
|
39
41
|
|
|
40
42
|
/**
|
package/lib/schema/string.js
CHANGED
|
@@ -19,14 +19,16 @@ const CastError = SchemaType.CastError;
|
|
|
19
19
|
*
|
|
20
20
|
* @param {String} key
|
|
21
21
|
* @param {Object} options
|
|
22
|
+
* @param {Object} schemaOptions
|
|
23
|
+
* @param {Schema} parentSchema
|
|
22
24
|
* @inherits SchemaType
|
|
23
25
|
* @api public
|
|
24
26
|
*/
|
|
25
27
|
|
|
26
|
-
function SchemaString(key, options) {
|
|
28
|
+
function SchemaString(key, options, _schemaOptions, parentSchema) {
|
|
27
29
|
this.enumValues = [];
|
|
28
30
|
this.regExp = null;
|
|
29
|
-
SchemaType.call(this, key, options, 'String');
|
|
31
|
+
SchemaType.call(this, key, options, 'String', parentSchema);
|
|
30
32
|
}
|
|
31
33
|
|
|
32
34
|
/**
|
|
@@ -32,11 +32,12 @@ module.exports = SchemaSubdocument;
|
|
|
32
32
|
* @param {Schema} schema
|
|
33
33
|
* @param {String} path
|
|
34
34
|
* @param {Object} options
|
|
35
|
+
* @param {Schema} parentSchema
|
|
35
36
|
* @inherits SchemaType
|
|
36
37
|
* @api public
|
|
37
38
|
*/
|
|
38
39
|
|
|
39
|
-
function SchemaSubdocument(schema, path, options) {
|
|
40
|
+
function SchemaSubdocument(schema, path, options, parentSchema) {
|
|
40
41
|
if (schema.options.timeseries) {
|
|
41
42
|
throw new InvalidSchemaOptionError(path, 'timeseries');
|
|
42
43
|
}
|
|
@@ -55,7 +56,7 @@ function SchemaSubdocument(schema, path, options) {
|
|
|
55
56
|
this.schema = schema;
|
|
56
57
|
this.$isSingleNested = true;
|
|
57
58
|
this.base = schema.base;
|
|
58
|
-
SchemaType.call(this, path, options, 'Embedded');
|
|
59
|
+
SchemaType.call(this, path, options, 'Embedded', parentSchema);
|
|
59
60
|
}
|
|
60
61
|
|
|
61
62
|
/*!
|
|
@@ -415,7 +416,8 @@ SchemaSubdocument.prototype.clone = function() {
|
|
|
415
416
|
const schematype = new this.constructor(
|
|
416
417
|
this.schema,
|
|
417
418
|
this.path,
|
|
418
|
-
{ ...this.options, _skipApplyDiscriminators: true }
|
|
419
|
+
{ ...this.options, _skipApplyDiscriminators: true },
|
|
420
|
+
this.parentSchema
|
|
419
421
|
);
|
|
420
422
|
schematype.validators = this.validators.slice();
|
|
421
423
|
if (this.requiredValidator !== undefined) {
|
package/lib/schema/union.js
CHANGED
|
@@ -14,12 +14,20 @@ const firstValueSymbol = Symbol('firstValue');
|
|
|
14
14
|
*/
|
|
15
15
|
|
|
16
16
|
class Union extends SchemaType {
|
|
17
|
-
|
|
18
|
-
|
|
17
|
+
/**
|
|
18
|
+
* Create a Union schema type.
|
|
19
|
+
*
|
|
20
|
+
* @param {String} key the path in the schema for this schema type
|
|
21
|
+
* @param {Object} options SchemaType-specific options (must have 'of' as array)
|
|
22
|
+
* @param {Object} schemaOptions additional options from the schema this schematype belongs to
|
|
23
|
+
* @param {Schema} parentSchema the schema this schematype belongs to
|
|
24
|
+
*/
|
|
25
|
+
constructor(key, options, schemaOptions, parentSchema) {
|
|
26
|
+
super(key, options, 'Union', parentSchema);
|
|
19
27
|
if (!options || !Array.isArray(options.of) || options.of.length === 0) {
|
|
20
28
|
throw new Error('Union schema type requires an array of types');
|
|
21
29
|
}
|
|
22
|
-
this.schemaTypes = options.of.map(obj =>
|
|
30
|
+
this.schemaTypes = options.of.map(obj => parentSchema.interpretAsType(key, obj, schemaOptions));
|
|
23
31
|
}
|
|
24
32
|
|
|
25
33
|
cast(val, doc, init, prev, options) {
|
package/lib/schema/uuid.js
CHANGED
|
@@ -37,12 +37,14 @@ function binaryToString(uuidBin) {
|
|
|
37
37
|
*
|
|
38
38
|
* @param {String} key
|
|
39
39
|
* @param {Object} options
|
|
40
|
+
* @param {Object} _schemaOptions
|
|
41
|
+
* @param {Schema} parentSchema
|
|
40
42
|
* @inherits SchemaType
|
|
41
43
|
* @api public
|
|
42
44
|
*/
|
|
43
45
|
|
|
44
|
-
function SchemaUUID(key, options) {
|
|
45
|
-
SchemaType.call(this, key, options, 'UUID');
|
|
46
|
+
function SchemaUUID(key, options, _schemaOptions, parentSchema) {
|
|
47
|
+
SchemaType.call(this, key, options, 'UUID', parentSchema);
|
|
46
48
|
this.getters.push(function(value) {
|
|
47
49
|
// For populated
|
|
48
50
|
if (value != null && value.$__ != null) {
|