Skip to content

Commit

Permalink
Merge pull request #180 from kazizi55/feature/fix-email-regex
Browse files Browse the repository at this point in the history
fix: improve email regex
  • Loading branch information
fabian-hiller authored Oct 6, 2023
2 parents 149974e + 2bb7428 commit 3ec0c7f
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 19 deletions.
31 changes: 15 additions & 16 deletions library/src/validations/email/email.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,32 +4,31 @@ import { email } from './email.ts';
describe('email', () => {
test('should pass only emails', () => {
const validate = email();

const value1 = '[email protected]';
expect(validate(value1).output).toBe(value1);
const value2 = 'firstname.lastname@example.com';
const value2 = '1234567890@example.com';
expect(validate(value2).output).toBe(value2);
const value3 = 'email@subdomain.example.com';
const value3 = '_______@example.com';
expect(validate(value3).output).toBe(value3);
const value4 = 'firstname+[email protected]';
const value4 = 'firstname.[email protected]';
expect(validate(value4).output).toBe(value4);
const value5 = '[email protected]';
const value5 = '[email protected]';
expect(validate(value5).output).toBe(value5);
const value6 = '“email”@example.com';
const value6 = 'firstname+lastname@example.com';
expect(validate(value6).output).toBe(value6);
const value7 = '1234567890@example.com';
const value7 = 'email@aaa-bbb.com';
expect(validate(value7).output).toBe(value7);
const value8 = 'email@example-one.com';
const value8 = 'email@example.name';
expect(validate(value8).output).toBe(value8);
const value9 = '_______@example.com';
const value9 = 'email@example.co.uk';
expect(validate(value9).output).toBe(value9);
const value10 = 'email@example.name';
const value10 = 'email@subdomain.example.com';
expect(validate(value10).output).toBe(value10);
const value11 = 'email@example.co.uk';
const value11 = 'email@subdomain.aaa-bbb.com';
expect(validate(value11).output).toBe(value11);
const value12 = 'firstname-lastname@example.com';
const value12 = 'email@subdomain.example.co.uk';
expect(validate(value12).output).toBe(value12);
const value13 = '[email protected]';
expect(validate(value13).output).toBe(value13);

expect(validate('plainaddress').issues).toBeTruthy();
expect(validate('#@%^%#$@#$@#.com').issues).toBeTruthy();
Expand All @@ -40,11 +39,11 @@ describe('email', () => {
expect(validate('[email protected]').issues).toBeTruthy();
expect(validate('[email protected]').issues).toBeTruthy();
expect(validate('[email protected]').issues).toBeTruthy();
// FIXME: expect(validate('あいうえお@example.com').issues).toBeTruthy();
expect(validate('あいうえお@example.com').issues).toBeTruthy();
expect(validate('[email protected] (Joe Smith)').issues).toBeTruthy();
expect(validate('email@example').issues).toBeTruthy();
// FIXME: expect(validate('[email protected]').issues).toBeTruthy();
// FIXME: expect(validate('[email protected]').issues).toBeTruthy();
expect(validate('[email protected]').issues).toBeTruthy();
expect(validate('[email protected]').issues).toBeTruthy();
expect(validate('[email protected]').issues).toBeTruthy();
expect(validate('[email protected]').issues).toBeTruthy();
});
Expand Down
4 changes: 1 addition & 3 deletions library/src/validations/email/email.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,13 @@ import { getOutput, getPipeIssues } from '../../utils/index.ts';
/**
* Creates a validation functions that validates a email.
*
* Hint: The regex used is not perfect, but should work for most emails.
*
* @param error The error message.
*
* @returns A validation function.
*/
export function email<TInput extends string>(error?: ErrorMessage) {
return (input: TInput): PipeResult<TInput> =>
!/^(([^<>()[\].,;:\s@"]+(\.[^<>()[\].,;:\s@"]+)*)|(".+"))@(([^<>()[\].,;:\s@"]+\.)+[^<>()[\].,;:\s@"]{2,})$/i.test(
!/^([A-Z0-9_+-]+\.?)*[A-Z0-9_+-]@([A-Z0-9][A-Z0-9-]*\.)+[A-Z]{2,}$/i.test(
input
)
? getPipeIssues('email', error || 'Invalid email', input)
Expand Down

0 comments on commit 3ec0c7f

Please sign in to comment.