Skip to content

Commit 71bbf64

Browse files
authored
Merge pull request #13851 from pshaddel/master
fix: Pass correct document type to required and default function
2 parents c27500b + 64cec02 commit 71bbf64

File tree

3 files changed

+32
-20
lines changed

3 files changed

+32
-20
lines changed

test/types/schema.test.ts

+12
Original file line numberDiff line numberDiff line change
@@ -1218,3 +1218,15 @@ function gh13800() {
12181218
expectError<string>(this.someOtherField);
12191219
});
12201220
}
1221+
1222+
async function gh13797() {
1223+
interface IUser {
1224+
name: string;
1225+
}
1226+
new Schema<IUser>({ name: { type: String, required: function() {
1227+
expectType<IUser>(this); return true;
1228+
} } });
1229+
new Schema<IUser>({ name: { type: String, default: function() {
1230+
expectType<IUser>(this); return '';
1231+
} } });
1232+
}

types/index.d.ts

+11-11
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ declare module 'mongoose' {
236236
/**
237237
* Create a new schema
238238
*/
239-
constructor(definition?: SchemaDefinition<SchemaDefinitionType<EnforcedDocType>> | DocType, options?: SchemaOptions<DocType, TInstanceMethods, TQueryHelpers, TStaticMethods, TVirtuals, THydratedDocumentType> | ResolveSchemaOptions<TSchemaOptions>);
239+
constructor(definition?: SchemaDefinition<SchemaDefinitionType<EnforcedDocType>, EnforcedDocType> | DocType, options?: SchemaOptions<DocType, TInstanceMethods, TQueryHelpers, TStaticMethods, TVirtuals, THydratedDocumentType> | ResolveSchemaOptions<TSchemaOptions>);
240240

241241
/** Adds key path / schema type pairs to this schema. */
242242
add(obj: SchemaDefinition<SchemaDefinitionType<EnforcedDocType>> | Schema, prefix?: string): this;
@@ -300,7 +300,7 @@ declare module 'mongoose' {
300300
methods: { [F in keyof TInstanceMethods]: TInstanceMethods[F] } & AnyObject;
301301

302302
/** The original object passed to the schema constructor */
303-
obj: SchemaDefinition<SchemaDefinitionType<EnforcedDocType>>;
303+
obj: SchemaDefinition<SchemaDefinitionType<EnforcedDocType>, EnforcedDocType>;
304304

305305
/** Gets/sets schema paths. */
306306
path<ResultType extends SchemaType = SchemaType<any, THydratedDocumentType>>(path: string): ResultType;
@@ -484,26 +484,26 @@ declare module 'mongoose' {
484484
? DateSchemaDefinition
485485
: (Function | string);
486486

487-
export type SchemaDefinitionProperty<T = undefined> = SchemaDefinitionWithBuiltInClass<T> |
488-
SchemaTypeOptions<T extends undefined ? any : T> |
487+
export type SchemaDefinitionProperty<T = undefined, EnforcedDocType = any> = SchemaDefinitionWithBuiltInClass<T> |
488+
SchemaTypeOptions<T extends undefined ? any : T, EnforcedDocType> |
489489
typeof SchemaType |
490490
Schema<any, any, any> |
491491
Schema<any, any, any>[] |
492-
SchemaTypeOptions<T extends undefined ? any : Unpacked<T>>[] |
492+
SchemaTypeOptions<T extends undefined ? any : Unpacked<T>, EnforcedDocType>[] |
493493
Function[] |
494-
SchemaDefinition<T> |
495-
SchemaDefinition<Unpacked<T>>[] |
494+
SchemaDefinition<T, EnforcedDocType> |
495+
SchemaDefinition<Unpacked<T>, EnforcedDocType>[] |
496496
typeof Schema.Types.Mixed |
497-
MixedSchemaTypeOptions;
497+
MixedSchemaTypeOptions<EnforcedDocType>;
498498

499-
export type SchemaDefinition<T = undefined> = T extends undefined
499+
export type SchemaDefinition<T = undefined, EnforcedDocType = any> = T extends undefined
500500
? { [path: string]: SchemaDefinitionProperty; }
501-
: { [path in keyof T]?: SchemaDefinitionProperty<T[path]>; };
501+
: { [path in keyof T]?: SchemaDefinitionProperty<T[path], EnforcedDocType>; };
502502

503503
export type AnyArray<T> = T[] | ReadonlyArray<T>;
504504
export type ExtractMongooseArray<T> = T extends Types.Array<any> ? AnyArray<Unpacked<T>> : T;
505505

506-
export interface MixedSchemaTypeOptions extends SchemaTypeOptions<Schema.Types.Mixed> {
506+
export interface MixedSchemaTypeOptions<EnforcedDocType> extends SchemaTypeOptions<Schema.Types.Mixed, EnforcedDocType> {
507507
type: typeof Schema.Types.Mixed;
508508
}
509509

types/schematypes.d.ts

+9-9
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ declare module 'mongoose' {
3939

4040
type DefaultType<T> = T extends Schema.Types.Mixed ? any : Partial<ExtractMongooseArray<T>>;
4141

42-
class SchemaTypeOptions<T> {
42+
class SchemaTypeOptions<T, EnforcedDocType = any> {
4343
type?:
4444
T extends string ? StringSchemaDefinition :
4545
T extends number ? NumberSchemaDefinition :
@@ -48,12 +48,12 @@ declare module 'mongoose' {
4848
T extends Map<any, any> ? SchemaDefinition<typeof Map> :
4949
T extends Buffer ? SchemaDefinition<typeof Buffer> :
5050
T extends Types.ObjectId ? ObjectIdSchemaDefinition :
51-
T extends Types.ObjectId[] ? AnyArray<ObjectIdSchemaDefinition> | AnyArray<SchemaTypeOptions<ObjectId>> :
52-
T extends object[] ? (AnyArray<Schema<any, any, any>> | AnyArray<SchemaDefinition<Unpacked<T>>> | AnyArray<SchemaTypeOptions<Unpacked<T>>>) :
53-
T extends string[] ? AnyArray<StringSchemaDefinition> | AnyArray<SchemaTypeOptions<string>> :
54-
T extends number[] ? AnyArray<NumberSchemaDefinition> | AnyArray<SchemaTypeOptions<number>> :
55-
T extends boolean[] ? AnyArray<BooleanSchemaDefinition> | AnyArray<SchemaTypeOptions<boolean>> :
56-
T extends Function[] ? AnyArray<Function | string> | AnyArray<SchemaTypeOptions<Unpacked<T>>> :
51+
T extends Types.ObjectId[] ? AnyArray<ObjectIdSchemaDefinition> | AnyArray<SchemaTypeOptions<ObjectId, EnforcedDocType>> :
52+
T extends object[] ? (AnyArray<Schema<any, any, any>> | AnyArray<SchemaDefinition<Unpacked<T>>> | AnyArray<SchemaTypeOptions<Unpacked<T>, EnforcedDocType>>) :
53+
T extends string[] ? AnyArray<StringSchemaDefinition> | AnyArray<SchemaTypeOptions<string, EnforcedDocType>> :
54+
T extends number[] ? AnyArray<NumberSchemaDefinition> | AnyArray<SchemaTypeOptions<number, EnforcedDocType>> :
55+
T extends boolean[] ? AnyArray<BooleanSchemaDefinition> | AnyArray<SchemaTypeOptions<boolean, EnforcedDocType>> :
56+
T extends Function[] ? AnyArray<Function | string> | AnyArray<SchemaTypeOptions<Unpacked<T>, EnforcedDocType>> :
5757
T | typeof SchemaType | Schema<any, any, any> | SchemaDefinition<T> | Function | AnyArray<Function>;
5858

5959
/** Defines a virtual with the given name that gets/sets this path. */
@@ -74,13 +74,13 @@ declare module 'mongoose' {
7474
* path cannot be set to a nullish value. If a function, Mongoose calls the
7575
* function and only checks for nullish values if the function returns a truthy value.
7676
*/
77-
required?: boolean | (() => boolean) | [boolean, string] | [() => boolean, string];
77+
required?: boolean | ((this: EnforcedDocType) => boolean) | [boolean, string] | [(this: EnforcedDocType) => boolean, string];
7878

7979
/**
8080
* The default value for this path. If a function, Mongoose executes the function
8181
* and uses the return value as the default.
8282
*/
83-
default?: DefaultType<T> | ((this: any, doc: any) => DefaultType<T>) | null;
83+
default?: DefaultType<T> | ((this: EnforcedDocType, doc: any) => DefaultType<T>) | null;
8484

8585
/**
8686
* The model that `populate()` should use if populating this path.

0 commit comments

Comments
 (0)