Skip to content

Commit 1e4e869

Browse files
author
Matt Mayer
authored
fix(internet): fix invalid emails in some locales (#1746)
1 parent dfa647d commit 1e4e869

File tree

4 files changed

+29
-3
lines changed

4 files changed

+29
-3
lines changed

src/locales/el/person/last_name.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ export default [
3232
'Αρβανίτης',
3333
'Αργυριάδης',
3434
'Ασπάσιος',
35-
'Αυγερινός (επώνυμο)',
35+
'Αυγερινός',
3636
'Βάμβας',
3737
'Βαμβακάς',
3838
'Βαρνακιώτης',
@@ -147,7 +147,7 @@ export default [
147147
'Λόντος',
148148
'Λύτρας',
149149
'Λαγός',
150-
'Λαιμός (επώνυμο)',
150+
'Λαιμός',
151151
'Λαμέρας',
152152
'Λαμπρόπουλος',
153153
'Λειβαδάς',

src/locales/fa/person/last_name.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ export default [
5757
'کوشکی',
5858
'کهنمویی',
5959
'کیان',
60-
'کیانی (نام خانوادگی)',
60+
'کیانی',
6161
'کیمیایی',
6262
'گل محمدی',
6363
'گلپایگانی',

src/modules/internet/index.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,13 @@ export class InternetModule {
9191
);
9292

9393
let localPart: string = this.userName(firstName, lastName);
94+
// Strip any special characters from the local part of the email address
95+
// This could happen if invalid chars are passed in manually in the firstName/lastName
96+
localPart = localPart.replace(/[^A-Za-z0-9._+\-]+/g, '');
97+
98+
// The local part of an email address is limited to 64 chars per RFC 3696
99+
// We limit to 50 chars to be more realistic
100+
localPart = localPart.substring(0, 50);
94101
if (options?.allowSpecialCharacters) {
95102
const usernameChars: string[] = '._-'.split('');
96103
const specialChars: string[] = ".!#$%&'*+-/=?^_`{|}~".split('');

test/internet.spec.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,25 @@ describe('internet', () => {
156156
expect(faker.definitions.internet.free_email).toContain(suffix);
157157
});
158158

159+
it('should return a valid email for very long names', () => {
160+
const longFirstName =
161+
'Elizabeth Alexandra Mary Jane Annabel Victoria';
162+
const longSurname = 'Smith Jones Davidson Brown White Greene Black';
163+
const email = faker.internet.email(longFirstName, longSurname);
164+
// should truncate to 50 chars
165+
// e.g. ElizabethAlexandraMaryJaneAnnabelVictoria.SmithJon@yahoo.com
166+
expect(email).toSatisfy(validator.isEmail);
167+
const localPart = email.split('@')[0];
168+
expect(localPart.length).toBeLessThanOrEqual(50);
169+
});
170+
171+
it('should return a valid email for names with invalid chars', () => {
172+
const email = faker.internet.email('Matthew (Matt)', 'Smith');
173+
// should strip invalid chars
174+
// e.g. MatthewMatt_Smith@yahoo.com
175+
expect(email).toSatisfy(validator.isEmail);
176+
});
177+
159178
it('should return an email with special characters', () => {
160179
const email = faker.internet.email('Mike', 'Smith', null, {
161180
allowSpecialCharacters: true,

0 commit comments

Comments
 (0)