Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(internet): fix invalid emails in some locales #1746

Merged
merged 8 commits into from
Jan 20, 2023
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -84,6 +84,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, '');
ST-DDT marked this conversation as resolved.
Show resolved Hide resolved

// 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);
xDivisionByZerox marked this conversation as resolved.
Show resolved Hide resolved
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. [email protected]
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. [email protected]
xDivisionByZerox marked this conversation as resolved.
Show resolved Hide resolved
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