-
-
Notifications
You must be signed in to change notification settings - Fork 186
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixes: #303
- Loading branch information
Showing
4 changed files
with
50 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { array } from './ArrayArbitrary'; | ||
import { buildLowerAlphaNumericArb } from './helpers/SpecificCharacterRange'; | ||
import { domain } from './HostArbitrary'; | ||
import { stringOf } from './StringArbitrary'; | ||
import { tuple } from './TupleArbitrary'; | ||
|
||
/** | ||
* For email address | ||
* | ||
* According to RFC 5322 - https://www.ietf.org/rfc/rfc5322.txt | ||
*/ | ||
export function emailAddress() { | ||
const others = ['!', '#', '$', '%', '&', "'", '*', '+', '-', '/', '=', '?', '^', '_', '`', '{', '|', '}', '~']; | ||
const atextArb = buildLowerAlphaNumericArb(others); | ||
const dotAtomArb = array(stringOf(atextArb, 1, 10), 1, 5).map(a => a.join('.')); | ||
return tuple(dotAtomArb, domain()).map(([lp, d]) => `${lp}@${d}`); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import { emailAddress } from '../../../../src/check/arbitrary/EmailArbitrary'; | ||
|
||
import * as genericHelper from './generic/GenericArbitraryHelper'; | ||
|
||
const isValidEmailRfc1123 = (t: string) => { | ||
// Taken from https://www.w3.org/TR/html5/forms.html#valid-e-mail-address | ||
const rfc1123 = /^[a-zA-Z0-9.!#$%&'*+\/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/; | ||
return rfc1123.test(t); | ||
}; | ||
|
||
const isValidEmailRfc5322 = (t: string) => { | ||
// Taken from https://stackoverflow.com/questions/201323/how-to-validate-an-email-address-using-a-regular-expression | ||
const rfc5322 = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/; | ||
return rfc5322.test(t); | ||
}; | ||
|
||
describe('EmailArbitrary', () => { | ||
describe('emailAddress', () => { | ||
describe('RFC 1123', () => { | ||
genericHelper.isValidArbitrary(() => emailAddress(), { | ||
isValidValue: (g: string) => isValidEmailRfc1123(g) | ||
}); | ||
}); | ||
describe('RFC 5322', () => { | ||
genericHelper.isValidArbitrary(() => emailAddress(), { | ||
isValidValue: (g: string) => isValidEmailRfc5322(g) | ||
}); | ||
}); | ||
}); | ||
}); |