Skip to content

Commit

Permalink
fix(type): require tld for email addresses
Browse files Browse the repository at this point in the history
  • Loading branch information
fergusean committed Mar 11, 2024
1 parent eb92e58 commit f818bc4
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 4 deletions.
2 changes: 1 addition & 1 deletion packages/type/src/validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export type AfterDate<T extends number> = ValidatorMeta<'afterDate', [T]>;
export type BeforeNow = ValidatorMeta<'beforeNow'>;
export type AfterNow = ValidatorMeta<'afterNow'>;

export const EMAIL_REGEX = /^\S+@\S+$/;
export const EMAIL_REGEX = /^\S+@\S+\.\S+$/;
export type Email = string & Pattern<typeof EMAIL_REGEX>;

/**
Expand Down
7 changes: 4 additions & 3 deletions packages/type/tests/validation.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,10 @@ test('email', () => {
expect(is<string & Email>('@')).toBe(false);

expect(validate<Email>('[email protected]')).toEqual([]);
expect(validate<Email>('nope')).toEqual([{ path: '', code: 'pattern', message: `Pattern ^\\S+@\\S+$ does not match`, value: 'nope' }]);
expect(validate<Email>('nope@')).toEqual([{ path: '', code: 'pattern', message: `Pattern ^\\S+@\\S+$ does not match`, value: 'nope@' }]);
expect(validate<Email>('@')).toEqual([{ path: '', code: 'pattern', message: `Pattern ^\\S+@\\S+$ does not match`, value: '@' }]);
expect(validate<Email>('nope')).toEqual([{ path: '', code: 'pattern', message: `Pattern ^\\S+@\\S+\\.\\S+$ does not match`, value: 'nope' }]);
expect(validate<Email>('nope@')).toEqual([{ path: '', code: 'pattern', message: `Pattern ^\\S+@\\S+\\.\\S+$ does not match`, value: 'nope@' }]);
expect(validate<Email>('nope@gmail')).toEqual([{ path: '', code: 'pattern', message: `Pattern ^\\S+@\\S+\\.\\S+$ does not match`, value: 'nope@gmail' }]);
expect(validate<Email>('@')).toEqual([{ path: '', code: 'pattern', message: `Pattern ^\\S+@\\S+\\.\\S+$ does not match`, value: '@' }]);
});

test('minLength', () => {
Expand Down

0 comments on commit f818bc4

Please sign in to comment.