Skip to content
This repository has been archived by the owner on Apr 27, 2024. It is now read-only.

Commit

Permalink
feat: email validator (#526)
Browse files Browse the repository at this point in the history
  • Loading branch information
alandeev committed Feb 14, 2021
1 parent 3fe699e commit 3698458
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 3 deletions.
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/) and this p

## [Unreleased]

### :zap: Added

- `toBeEmail` - Expect a value to be an email (contributed by [@alandev2](https://github.com/alandev2))

## [1.0.4] - 2019-12-27

### :policeman: Security
Expand Down Expand Up @@ -34,7 +38,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/) and this p

## [1.0.0] - 2017-06-22

### Added
### :zap: Added

- `toBeTrue` - Expect a condition to be true
- `toBeFalse` - Expect a condition to be false
Expand Down
14 changes: 14 additions & 0 deletions src/Expect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,3 +79,17 @@ export function toBeCharCodes(value: string, minCharCode: number, maxCharCode: n
}
}
}

/**
* Expect a value to be an email.
* @param email The value expected to be an email.
* @param errorMessage The optional error message displayed if expectation fails.
* @throws {ExpectationError}
*/
export function toBeEmail(email: string, errorMessage?: string) {
const regex = /[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?/
const match = email.match(regex);
if(match === null || (match[0] !== email)) {
throw new ExpectationError(errorMessage);
}
}
45 changes: 43 additions & 2 deletions test/Expect.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ import {
toBeFalse,
toBeTrue,
toExist,
toNotExist
} from './../src';
toNotExist,
toBeEmail
} from '../src';

describe('expect', () => {

Expand Down Expand Up @@ -116,4 +117,44 @@ describe('expect', () => {
expect(() => toBeCharCodes(String.fromCharCode(64, 90), 65, 90, errorMessage)).toThrow(ExpectationError);
});
});

describe('#toBeEmail', () => {
test('should pass if expectation is fulfilled', () => {
toBeEmail('email@example.com')
toBeEmail('firstname.lastname@example.com')
toBeEmail('email@subdomain.example.com')
toBeEmail('firstname+lastname@example.com')
toBeEmail('email@123.123.123.123')
toBeEmail('1234567890@example.com')
toBeEmail('email@example-one.com')
toBeEmail('_______@example.com')
toBeEmail('email@example.web')
toBeEmail('email@example.name')
toBeEmail('email@example.museum')
toBeEmail('email@example.co.jp')
toBeEmail('firstname-lastname@example.com')
})

test('should fail if expectation is unfulfilled', () => {
expect(() => toBeEmail('email@[123.123.123.123]')).toThrow(ExpectationError);
expect(() => toBeEmail('"email"@example.com')).toThrow(ExpectationError);
expect(() => toBeEmail('plainaddress')).toThrow(ExpectationError);
expect(() => toBeEmail('#@%^%#$@#$@#.com')).toThrow(ExpectationError);
expect(() => toBeEmail('@example.com')).toThrow(ExpectationError);
expect(() => toBeEmail('Joe Smith <email@example.com>')).toThrow(ExpectationError);
expect(() => toBeEmail('email.example.com')).toThrow(ExpectationError);
expect(() => toBeEmail('email@example@example.com')).toThrow(ExpectationError);
expect(() => toBeEmail('.email@example.com')).toThrow(ExpectationError);
expect(() => toBeEmail('email.@example.com')).toThrow(ExpectationError);
expect(() => toBeEmail('email..email@example.com')).toThrow(ExpectationError);
expect(() => toBeEmail('email@example.com (Joe Smith)')).toThrow(ExpectationError);
expect(() => toBeEmail('email@example')).toThrow(ExpectationError);
expect(() => toBeEmail('email@-example.com')).toThrow(ExpectationError);
expect(() => toBeEmail('email@example..com')).toThrow(ExpectationError);
expect(() => toBeEmail('Abc..123@example.com')).toThrow(ExpectationError);
expect(() => toBeEmail('”(),:;<>[\]@example.com')).toThrow(ExpectationError);
expect(() => toBeEmail('just”not”right@example.com')).toThrow(ExpectationError);
expect(() => toBeEmail('this\ is"really"not\allowed@example.com')).toThrow(ExpectationError);
})
})
});

0 comments on commit 3698458

Please sign in to comment.