Skip to content

Commit

Permalink
fix(internet): fix invalid emails in some locales (#1746)
Browse files Browse the repository at this point in the history
  • Loading branch information
Matt Mayer authored Jan 20, 2023
1 parent dfa647d commit 1e4e869
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 3 deletions.
4 changes: 2 additions & 2 deletions src/locales/el/person/last_name.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export default [
'Αρβανίτης',
'Αργυριάδης',
'Ασπάσιος',
'Αυγερινός (επώνυμο)',
'Αυγερινός',
'Βάμβας',
'Βαμβακάς',
'Βαρνακιώτης',
Expand Down Expand Up @@ -147,7 +147,7 @@ export default [
'Λόντος',
'Λύτρας',
'Λαγός',
'Λαιμός (επώνυμο)',
'Λαιμός',
'Λαμέρας',
'Λαμπρόπουλος',
'Λειβαδάς',
Expand Down
2 changes: 1 addition & 1 deletion src/locales/fa/person/last_name.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ export default [
'کوشکی',
'کهنمویی',
'کیان',
'کیانی (نام خانوادگی)',
'کیانی',
'کیمیایی',
'گل محمدی',
'گلپایگانی',
Expand Down
7 changes: 7 additions & 0 deletions src/modules/internet/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,13 @@ export class InternetModule {
);

let localPart: string = this.userName(firstName, lastName);
// Strip any special characters from the local part of the email address
// This could happen if invalid chars are passed in manually in the firstName/lastName
localPart = localPart.replace(/[^A-Za-z0-9._+\-]+/g, '');

// The local part of an email address is limited to 64 chars per RFC 3696
// We limit to 50 chars to be more realistic
localPart = localPart.substring(0, 50);
if (options?.allowSpecialCharacters) {
const usernameChars: string[] = '._-'.split('');
const specialChars: string[] = ".!#$%&'*+-/=?^_`{|}~".split('');
Expand Down
19 changes: 19 additions & 0 deletions test/internet.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,25 @@ describe('internet', () => {
expect(faker.definitions.internet.free_email).toContain(suffix);
});

it('should return a valid email for very long names', () => {
const longFirstName =
'Elizabeth Alexandra Mary Jane Annabel Victoria';
const longSurname = 'Smith Jones Davidson Brown White Greene Black';
const email = faker.internet.email(longFirstName, longSurname);
// should truncate to 50 chars
// e.g. ElizabethAlexandraMaryJaneAnnabelVictoria.SmithJon@yahoo.com
expect(email).toSatisfy(validator.isEmail);
const localPart = email.split('@')[0];
expect(localPart.length).toBeLessThanOrEqual(50);
});

it('should return a valid email for names with invalid chars', () => {
const email = faker.internet.email('Matthew (Matt)', 'Smith');
// should strip invalid chars
// e.g. MatthewMatt_Smith@yahoo.com
expect(email).toSatisfy(validator.isEmail);
});

it('should return an email with special characters', () => {
const email = faker.internet.email('Mike', 'Smith', null, {
allowSpecialCharacters: true,
Expand Down

0 comments on commit 1e4e869

Please sign in to comment.