From 1e4e8699e59f3b5b9c1e1d6ad9b89ee4cc254e95 Mon Sep 17 00:00:00 2001 From: Matt Mayer Date: Sat, 21 Jan 2023 02:24:06 +0700 Subject: [PATCH] fix(internet): fix invalid emails in some locales (#1746) --- src/locales/el/person/last_name.ts | 4 ++-- src/locales/fa/person/last_name.ts | 2 +- src/modules/internet/index.ts | 7 +++++++ test/internet.spec.ts | 19 +++++++++++++++++++ 4 files changed, 29 insertions(+), 3 deletions(-) diff --git a/src/locales/el/person/last_name.ts b/src/locales/el/person/last_name.ts index 70634100e55..79c0caff415 100644 --- a/src/locales/el/person/last_name.ts +++ b/src/locales/el/person/last_name.ts @@ -32,7 +32,7 @@ export default [ 'Αρβανίτης', 'Αργυριάδης', 'Ασπάσιος', - 'Αυγερινός (επώνυμο)', + 'Αυγερινός', 'Βάμβας', 'Βαμβακάς', 'Βαρνακιώτης', @@ -147,7 +147,7 @@ export default [ 'Λόντος', 'Λύτρας', 'Λαγός', - 'Λαιμός (επώνυμο)', + 'Λαιμός', 'Λαμέρας', 'Λαμπρόπουλος', 'Λειβαδάς', diff --git a/src/locales/fa/person/last_name.ts b/src/locales/fa/person/last_name.ts index c012140eeb9..57d4abc996b 100644 --- a/src/locales/fa/person/last_name.ts +++ b/src/locales/fa/person/last_name.ts @@ -57,7 +57,7 @@ export default [ 'کوشکی', 'کهنمویی', 'کیان', - 'کیانی (نام خانوادگی)', + 'کیانی', 'کیمیایی', 'گل محمدی', 'گلپایگانی', diff --git a/src/modules/internet/index.ts b/src/modules/internet/index.ts index f7f022ac64d..d1af8ebe960 100644 --- a/src/modules/internet/index.ts +++ b/src/modules/internet/index.ts @@ -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(''); diff --git a/test/internet.spec.ts b/test/internet.spec.ts index 11cbb60e7c2..16b6a804196 100644 --- a/test/internet.spec.ts +++ b/test/internet.spec.ts @@ -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,