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