* Array SchemaType constructor * * @param {String} key * @param {SchemaType} cast * @param {Object} options * @param {Object} schemaOptions * @inherits SchemaType * @api public
(key, cast, options, schemaOptions)
| 39 | */ |
| 40 | |
| 41 | function SchemaArray(key, cast, options, schemaOptions) { |
| 42 | // lazy load |
| 43 | EmbeddedDoc || (EmbeddedDoc = require('../types').Embedded); |
| 44 | |
| 45 | let typeKey = 'type'; |
| 46 | if (schemaOptions && schemaOptions.typeKey) { |
| 47 | typeKey = schemaOptions.typeKey; |
| 48 | } |
| 49 | this.schemaOptions = schemaOptions; |
| 50 | |
| 51 | if (cast) { |
| 52 | let castOptions = {}; |
| 53 | |
| 54 | if (utils.isPOJO(cast)) { |
| 55 | if (cast[typeKey]) { |
| 56 | // support { type: Woot } |
| 57 | castOptions = clone(cast); // do not alter user arguments |
| 58 | delete castOptions[typeKey]; |
| 59 | cast = cast[typeKey]; |
| 60 | } else { |
| 61 | cast = Mixed; |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | if (options != null && options.ref != null && castOptions.ref == null) { |
| 66 | castOptions.ref = options.ref; |
| 67 | } |
| 68 | |
| 69 | if (cast === Object) { |
| 70 | cast = Mixed; |
| 71 | } |
| 72 | |
| 73 | // support { type: 'String' } |
| 74 | const name = typeof cast === 'string' |
| 75 | ? cast |
| 76 | : utils.getFunctionName(cast); |
| 77 | |
| 78 | const Types = require('./index.js'); |
| 79 | const caster = Types.hasOwnProperty(name) ? Types[name] : cast; |
| 80 | |
| 81 | this.casterConstructor = caster; |
| 82 | |
| 83 | if (this.casterConstructor instanceof SchemaArray) { |
| 84 | this.casterConstructor[isNestedArraySymbol] = true; |
| 85 | } |
| 86 | |
| 87 | if (typeof caster === 'function' && |
| 88 | !caster.$isArraySubdocument && |
| 89 | !caster.$isSchemaMap) { |
| 90 | const path = this.caster instanceof EmbeddedDoc ? null : key; |
| 91 | this.caster = new caster(path, castOptions); |
| 92 | } else { |
| 93 | this.caster = caster; |
| 94 | if (!(this.caster instanceof EmbeddedDoc)) { |
| 95 | this.caster.path = key; |
| 96 | } |
| 97 | } |
| 98 |