aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMatt Mayer <[email protected]>2023-01-21 02:24:06 +0700
committerGitHub <[email protected]>2023-01-20 20:24:06 +0100
commit1e4e8699e59f3b5b9c1e1d6ad9b89ee4cc254e95 (patch)
treee1f3fd268f970824639b0e94224e0dfc46c38c86
parentdfa647dc07ea25f962c4df913bae2de082e8acc4 (diff)
downloadfaker-1e4e8699e59f3b5b9c1e1d6ad9b89ee4cc254e95.tar.xz
faker-1e4e8699e59f3b5b9c1e1d6ad9b89ee4cc254e95.zip
fix(internet): fix invalid emails in some locales (#1746)
-rw-r--r--src/locales/el/person/last_name.ts4
-rw-r--r--src/locales/fa/person/last_name.ts2
-rw-r--r--src/modules/internet/index.ts7
-rw-r--r--test/internet.spec.ts19
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 70634100..79c0caff 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 c012140e..57d4abc9 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 f7f022ac..d1af8ebe 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 11cbb60e..16b6a804 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
+ 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
+ expect(email).toSatisfy(validator.isEmail);
+ });
+
it('should return an email with special characters', () => {
const email = faker.internet.email('Mike', 'Smith', null, {
allowSpecialCharacters: true,