Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

types(schema): support required: { isRequired: true } syntax in schema definition #13680

Merged
merged 3 commits into from Aug 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 16 additions & 0 deletions test/types/schema.test.ts
Expand Up @@ -1134,3 +1134,19 @@ function maps() {
expectType<Map<string, number>>(doc.myMap);
expectType<number | undefined>(doc.myMap!.get('answer'));
}

function gh13514() {
const schema = new Schema({
email: {
type: String,
required: {
isRequired: true,
message: 'Email is required'
} as const
}
});
const Test = model('Test', schema);

const doc = new Test({ email: 'bar' });
const str: string = doc.email;
}
2 changes: 1 addition & 1 deletion types/inferschematype.d.ts
Expand Up @@ -86,7 +86,7 @@ type IsPathDefaultUndefined<PathType> = PathType extends { default: undefined }
* @param {TypeKey} TypeKey A generic of literal string type."Refers to the property used for path type definition".
*/
type IsPathRequired<P, TypeKey extends string = DefaultTypeKey> =
P extends { required: true | [true, string | undefined] } | ArrayConstructor | any[]
P extends { required: true | [true, string | undefined] | { isRequired: true } } | ArrayConstructor | any[]
? true
: P extends { required: boolean }
? P extends { required: false }
Expand Down