Skip to content

Commit

Permalink
Merge pull request #13680 from Automattic/vkarpov15/gh-13514
Browse files Browse the repository at this point in the history
types(schema): support `required: { isRequired: true }` syntax in schema definition
  • Loading branch information
vkarpov15 committed Aug 3, 2023
2 parents 9e0ca12 + ddd6ce9 commit 570be58
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
16 changes: 16 additions & 0 deletions test/types/schema.test.ts
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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

0 comments on commit 570be58

Please sign in to comment.