@@ -13,7 +13,8 @@ import {
13
13
DefaultTypeKey ,
14
14
ObjectIdSchemaDefinition ,
15
15
IfEquals ,
16
- DefaultSchemaOptions
16
+ DefaultSchemaOptions ,
17
+ IsItRecordAndNotAny
17
18
} from 'mongoose' ;
18
19
19
20
declare module 'mongoose' {
@@ -176,6 +177,30 @@ TypeKey
176
177
*/
177
178
type PathEnumOrString < T extends SchemaTypeOptions < string > [ 'enum' ] > = T extends ReadonlyArray < infer E > ? E : T extends { values : any } ? PathEnumOrString < T [ 'values' ] > : string ;
178
179
180
+ type IsSchemaTypeFromBuiltinClass < T > = T extends ( typeof String )
181
+ ? true
182
+ : T extends ( typeof Number )
183
+ ? true
184
+ : T extends ( typeof Boolean )
185
+ ? true
186
+ : T extends ( typeof Buffer )
187
+ ? true
188
+ : T extends ( typeof Schema . Types . ObjectId )
189
+ ? true
190
+ : T extends ( typeof Schema . Types . UUID )
191
+ ? true
192
+ : T extends ( typeof Schema . Types . Decimal128 )
193
+ ? true
194
+ : T extends Types . ObjectId
195
+ ? true
196
+ : T extends Types . Decimal128
197
+ ? true
198
+ : T extends Buffer
199
+ ? true
200
+ : T extends ( typeof Schema . Types . Mixed )
201
+ ? true
202
+ : IfEquals < T , Schema . Types . ObjectId , true , false > ;
203
+
179
204
/**
180
205
* @summary Resolve path type by returning the corresponding type.
181
206
* @param {PathValueType } PathValueType Document definition path type.
@@ -189,15 +214,21 @@ type ResolvePathType<PathValueType, Options extends SchemaTypeOptions<PathValueT
189
214
IfEquals < Item , never , any [ ] , Item extends Schema ?
190
215
// If Item is a schema, infer its type.
191
216
Types . DocumentArray < InferSchemaType < Item > > :
192
- Item extends Record < TypeKey , any > ?
217
+ Item extends Record < TypeKey , any > ?
193
218
Item [ TypeKey ] extends Function | String ?
194
219
// If Item has a type key that's a string or a callable, it must be a scalar,
195
220
// so we can directly obtain its path type.
196
221
ObtainDocumentPathType < Item , TypeKey > [ ] :
197
222
// If the type key isn't callable, then this is an array of objects, in which case
198
223
// we need to call ObtainDocumentType to correctly infer its type.
199
- ObtainDocumentType < Item , any , { typeKey : TypeKey } > [ ] :
200
- ObtainDocumentPathType < Item , TypeKey > [ ]
224
+ ObtainDocumentType < Item , any , { typeKey : TypeKey } > [ ] :
225
+ IsSchemaTypeFromBuiltinClass < Item > extends true ?
226
+ ObtainDocumentPathType < Item , TypeKey > [ ] :
227
+ IsItRecordAndNotAny < Item > extends true ?
228
+ Item extends Record < string , never > ?
229
+ ObtainDocumentPathType < Item , TypeKey > [ ] :
230
+ Types . DocumentArray < ObtainDocumentType < Item , any , { typeKey : TypeKey } > > :
231
+ ObtainDocumentPathType < Item , TypeKey > [ ]
201
232
> :
202
233
PathValueType extends ReadonlyArray < infer Item > ?
203
234
IfEquals < Item , never , any [ ] , Item extends Schema ?
@@ -206,7 +237,13 @@ type ResolvePathType<PathValueType, Options extends SchemaTypeOptions<PathValueT
206
237
Item [ TypeKey ] extends Function | String ?
207
238
ObtainDocumentPathType < Item , TypeKey > [ ] :
208
239
ObtainDocumentType < Item , any , { typeKey : TypeKey } > [ ] :
209
- ObtainDocumentPathType < Item , TypeKey > [ ]
240
+ IsSchemaTypeFromBuiltinClass < Item > extends true ?
241
+ ObtainDocumentPathType < Item , TypeKey > [ ] :
242
+ IsItRecordAndNotAny < Item > extends true ?
243
+ Item extends Record < string , never > ?
244
+ ObtainDocumentPathType < Item , TypeKey > [ ] :
245
+ Types . DocumentArray < ObtainDocumentType < Item , any , { typeKey : TypeKey } > > :
246
+ ObtainDocumentPathType < Item , TypeKey > [ ]
210
247
> :
211
248
PathValueType extends StringSchemaDefinition ? PathEnumOrString < Options [ 'enum' ] > :
212
249
IfEquals < PathValueType , Schema . Types . String > extends true ? PathEnumOrString < Options [ 'enum' ] > :
0 commit comments