@@ -29,7 +29,7 @@ let Subdocument;
29
29
* @api public
30
30
*/
31
31
32
- function DocumentArray ( key , schema , options , schemaOptions ) {
32
+ function DocumentArrayPath ( key , schema , options , schemaOptions ) {
33
33
const EmbeddedDocument = _createConstructor ( schema , options ) ;
34
34
EmbeddedDocument . prototype . $basePath = key ;
35
35
@@ -62,24 +62,23 @@ function DocumentArray(key, schema, options, schemaOptions) {
62
62
*
63
63
* @api public
64
64
*/
65
- DocumentArray . schemaName = 'DocumentArray' ;
65
+ DocumentArrayPath . schemaName = 'DocumentArray' ;
66
66
67
67
/**
68
68
* Options for all document arrays.
69
69
*
70
70
* - `castNonArrays`: `true` by default. If `false`, Mongoose will throw a CastError when a value isn't an array. If `true`, Mongoose will wrap the provided value in an array before casting.
71
71
*
72
- * @static options
73
72
* @api public
74
73
*/
75
74
76
- DocumentArray . options = { castNonArrays : true } ;
75
+ DocumentArrayPath . options = { castNonArrays : true } ;
77
76
78
77
/*!
79
78
* Inherits from ArrayType.
80
79
*/
81
- DocumentArray . prototype = Object . create ( ArrayType . prototype ) ;
82
- DocumentArray . prototype . constructor = DocumentArray ;
80
+ DocumentArrayPath . prototype = Object . create ( ArrayType . prototype ) ;
81
+ DocumentArrayPath . prototype . constructor = DocumentArrayPath ;
83
82
84
83
/*!
85
84
* Ignore
@@ -122,11 +121,24 @@ function _createConstructor(schema, options, baseClass) {
122
121
return EmbeddedDocument ;
123
122
}
124
123
125
- /*!
126
- * Ignore
124
+ /**
125
+ * Adds a discriminator to this document array.
126
+ *
127
+ * ####Example:
128
+ * const shapeSchema = Schema({ name: String }, { discriminatorKey: 'kind' });
129
+ * const schema = Schema({ shapes: [shapeSchema] });
130
+ *
131
+ * const docArrayPath = parentSchema.path('shapes');
132
+ * docArrayPath.discriminator('Circle', Schema({ radius: Number }));
133
+ *
134
+ * @param {String } name
135
+ * @param {Schema } schema fields to add to the schema for instances of this sub-class
136
+ * @param {String } [value] the string stored in the `discriminatorKey` property. If not specified, Mongoose uses the `name` parameter.
137
+ * @see discriminators /docs/discriminators.html
138
+ * @api public
127
139
*/
128
140
129
- DocumentArray . prototype . discriminator = function ( name , schema , tiedValue ) {
141
+ DocumentArrayPath . prototype . discriminator = function ( name , schema , tiedValue ) {
130
142
if ( typeof name === 'function' ) {
131
143
name = utils . getFunctionName ( name ) ;
132
144
}
@@ -155,7 +167,7 @@ DocumentArray.prototype.discriminator = function(name, schema, tiedValue) {
155
167
* @api private
156
168
*/
157
169
158
- DocumentArray . prototype . doValidate = function ( array , fn , scope , options ) {
170
+ DocumentArrayPath . prototype . doValidate = function ( array , fn , scope , options ) {
159
171
// lazy load
160
172
MongooseDocumentArray || ( MongooseDocumentArray = require ( '../types/documentarray' ) ) ;
161
173
@@ -231,7 +243,7 @@ DocumentArray.prototype.doValidate = function(array, fn, scope, options) {
231
243
* @api private
232
244
*/
233
245
234
- DocumentArray . prototype . doValidateSync = function ( array , scope ) {
246
+ DocumentArrayPath . prototype . doValidateSync = function ( array , scope ) {
235
247
const schemaTypeError = SchemaType . prototype . doValidateSync . call ( this , array , scope ) ;
236
248
if ( schemaTypeError != null ) {
237
249
schemaTypeError . $isArrayValidatorError = true ;
@@ -277,7 +289,7 @@ DocumentArray.prototype.doValidateSync = function(array, scope) {
277
289
* ignore
278
290
*/
279
291
280
- DocumentArray . prototype . getDefault = function ( scope ) {
292
+ DocumentArrayPath . prototype . getDefault = function ( scope ) {
281
293
let ret = typeof this . defaultValue === 'function'
282
294
? this . defaultValue . call ( scope )
283
295
: this . defaultValue ;
@@ -316,7 +328,7 @@ DocumentArray.prototype.getDefault = function(scope) {
316
328
* @api private
317
329
*/
318
330
319
- DocumentArray . prototype . cast = function ( value , doc , init , prev , options ) {
331
+ DocumentArrayPath . prototype . cast = function ( value , doc , init , prev , options ) {
320
332
// lazy load
321
333
MongooseDocumentArray || ( MongooseDocumentArray = require ( '../types/documentarray' ) ) ;
322
334
@@ -326,7 +338,7 @@ DocumentArray.prototype.cast = function(value, doc, init, prev, options) {
326
338
const _opts = { transform : false , virtuals : false } ;
327
339
328
340
if ( ! Array . isArray ( value ) ) {
329
- if ( ! init && ! DocumentArray . options . castNonArrays ) {
341
+ if ( ! init && ! DocumentArrayPath . options . castNonArrays ) {
330
342
throw new CastError ( 'DocumentArray' , util . inspect ( value ) , this . path ) ;
331
343
}
332
344
// gh-2442 mark whole array as modified if we're initializing a doc from
@@ -416,7 +428,7 @@ DocumentArray.prototype.cast = function(value, doc, init, prev, options) {
416
428
* ignore
417
429
*/
418
430
419
- DocumentArray . prototype . clone = function ( ) {
431
+ DocumentArrayPath . prototype . clone = function ( ) {
420
432
const options = Object . assign ( { } , this . options ) ;
421
433
const schematype = new this . constructor ( this . path , this . schema , options , this . schemaOptions ) ;
422
434
schematype . validators = this . validators . slice ( ) ;
@@ -429,7 +441,7 @@ DocumentArray.prototype.clone = function() {
429
441
* Scopes paths selected in a query to this array.
430
442
* Necessary for proper default application of subdocument values.
431
443
*
432
- * @param {DocumentArray } array - the array to scope `fields` paths
444
+ * @param {DocumentArrayPath } array - the array to scope `fields` paths
433
445
* @param {Object|undefined } fields - the root fields selected in the query
434
446
* @param {Boolean|undefined } init - if we are being created part of a query result
435
447
*/
@@ -469,4 +481,4 @@ function scopePaths(array, fields, init) {
469
481
* Module exports.
470
482
*/
471
483
472
- module . exports = DocumentArray ;
484
+ module . exports = DocumentArrayPath ;
0 commit comments