Skip to content

Commit

Permalink
Merge pull request #12650 from Automattic/vkarpov15/gh-12593
Browse files Browse the repository at this point in the history
fix(types): add UUID to types
  • Loading branch information
vkarpov15 authored Nov 4, 2022
2 parents 5a40613 + e559695 commit cdfc84f
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 8 deletions.
23 changes: 23 additions & 0 deletions test/types/schema.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -873,6 +873,29 @@ function gh12431() {
expectType<{ testDate?: Date, testDecimal?: Types.Decimal128 }>({} as Example);
}

async function gh12593() {
const testSchema = new Schema({ x: { type: Schema.Types.UUID } });

type Example = InferSchemaType<typeof testSchema>;
expectType<{ x?: Buffer }>({} as Example);

const Test = model('Test', testSchema);

const doc = await Test.findOne({ x: '4709e6d9-61fd-435e-b594-d748eb196d8f' }).orFail();
expectType<Buffer | undefined>(doc.x);

const doc2 = new Test({ x: '4709e6d9-61fd-435e-b594-d748eb196d8f' });
expectType<Buffer | undefined>(doc2.x);

const doc3 = await Test.findOne({}).orFail().lean();
expectType<Buffer | undefined>(doc3.x);

const arrSchema = new Schema({ arr: [{ type: Schema.Types.UUID }] });

type ExampleArr = InferSchemaType<typeof arrSchema>;
expectType<{ arr: Buffer[] }>({} as ExampleArr);
}

function gh12562() {
const emailRegExp = /@/;
const userSchema = new Schema(
Expand Down
18 changes: 10 additions & 8 deletions types/inferschematype.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,11 +177,13 @@ type ResolvePathType<PathValueType, Options extends SchemaTypeOptions<PathValueT
PathValueType extends 'decimal128' | 'Decimal128' | typeof Schema.Types.Decimal128 ? Types.Decimal128 :
IfEquals<PathValueType, Schema.Types.Decimal128> extends true ? Types.Decimal128 :
IfEquals<PathValueType, Types.Decimal128> extends true ? Types.Decimal128 :
PathValueType extends MapConstructor ? Map<string, ResolvePathType<Options['of']>> :
PathValueType extends ArrayConstructor ? any[] :
PathValueType extends typeof Schema.Types.Mixed ? any:
IfEquals<PathValueType, ObjectConstructor> extends true ? any:
IfEquals<PathValueType, {}> extends true ? any:
PathValueType extends typeof SchemaType ? PathValueType['prototype'] :
PathValueType extends Record<string, any> ? ObtainDocumentType<PathValueType, any, TypeKey> :
unknown;
PathValueType extends 'uuid' | 'UUID' | typeof Schema.Types.UUID ? Buffer :
IfEquals<PathValueType, Schema.Types.UUID> extends true ? Buffer :
PathValueType extends MapConstructor ? Map<string, ResolvePathType<Options['of']>> :
PathValueType extends ArrayConstructor ? any[] :
PathValueType extends typeof Schema.Types.Mixed ? any:
IfEquals<PathValueType, ObjectConstructor> extends true ? any:
IfEquals<PathValueType, {}> extends true ? any:
PathValueType extends typeof SchemaType ? PathValueType['prototype'] :
PathValueType extends Record<string, any> ? ObtainDocumentType<PathValueType, any, TypeKey> :
unknown;
5 changes: 5 additions & 0 deletions types/schematypes.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -422,6 +422,11 @@ declare module 'mongoose' {
/** Adds an uppercase [setter](http://mongoosejs.com/docs/api.html#schematype_SchemaType-set). */
uppercase(shouldApply?: boolean): this;
}

class UUID extends SchemaType {
/** This schema type's name, to defend against minifiers that mangle function names. */
static schemaName: 'UUID';
}
}
}
}

0 comments on commit cdfc84f

Please sign in to comment.