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 f80c53a
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 5 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
10 changes: 6 additions & 4 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 Expand Up @@ -147,7 +148,8 @@ test('class', () => {
expect(is<User>({ id: 0, logins: 0, username: 'Peter' })).toBe(true);
expect(is<User>({ id: -1, logins: 0, username: 'Peter' })).toBe(false);
expect(is<User>({ id: 0, logins: 0, username: 'AB' })).toBe(false);
expect(is<User>({ id: 0, logins: 0, username: 'Peter', email: 'abc@abc' })).toBe(true);
expect(is<User>({ id: 0, logins: 0, username: 'Peter', email: '[email protected]' })).toBe(true);
expect(is<User>({ id: 0, logins: 0, username: 'Peter', email: 'abc@abc' })).toBe(false);
expect(is<User>({ id: 0, logins: 0, username: 'Peter', email: 'abc' })).toBe(false);
});

Expand Down

0 comments on commit f80c53a

Please sign in to comment.