Skip to content

Commit 0956d52

Browse files
committed
fix: email with alias char #392
1 parent ac92459 commit 0956d52

File tree

3 files changed

+50
-9
lines changed

3 files changed

+50
-9
lines changed

.vscode/settings.json

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,10 @@
1010
"typescript.format.insertSpaceAfterOpeningAndBeforeClosingNonemptyParenthesis": true,
1111
"typescript.format.insertSpaceAfterConstructor": true,
1212
"typescript.format.insertSpaceBeforeFunctionParenthesis": true,
13-
"cSpell.words": ["cnpj"]
13+
"cSpell.words": [
14+
"cnpj"
15+
],
16+
"[typescript]": {
17+
"editor.defaultFormatter": "vscode.typescript-language-features"
18+
}
1419
}

lib/utils/email.validator.util.ts

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* 64 = @
77
* 46 = .
88
*/
9-
const ValidChars = { min: 97, max: 122, specials: [95, 45, 64, 46] };
9+
const ValidChars = { min: 97, max: 122, specials: [95, 45, 64, 46, 43] };
1010

1111
const ValidCharsNum = { min: 48, max: 57 };
1212

@@ -81,13 +81,24 @@ const IsValidCountry = (country: string): boolean => {
8181
return true;
8282
};
8383

84+
const IsValidNick = (email: string): boolean => {
85+
const nick = email.split('@')?.[0];
86+
return (
87+
!nick.startsWith('+') &&
88+
!nick.endsWith('+') &&
89+
!nick.startsWith('-') &&
90+
!nick.endsWith('-')
91+
);
92+
};
93+
8494
const IsValidDomain = (email: string): boolean => {
8595
const domain = email.split('@');
8696
const parts = domain[1].split('.');
8797
if (parts.length === 1 || parts.length > 3) return false;
8898

8999
const isInValidStartAndEnd =
90100
domain[1].startsWith('-') || domain[1].endsWith('-');
101+
91102
if (isInValidStartAndEnd) return false;
92103

93104
const isLessThanMax = email.length <= 64;
@@ -117,24 +128,22 @@ const IsValidDomain = (email: string): boolean => {
117128
* @returns true if is a valid email and false if not
118129
*/
119130
export const IsValidEmail = (email: string): boolean => {
120-
const AtCode = 64;
121131
const isString: boolean = typeof email === 'string';
122132
if (!isString) return false;
123133

124134
const trimEmail = email.trim().toLowerCase();
125135

126136
const isValidFirsChar: boolean =
127137
IsAlphabet(trimEmail[0]) || IsNumber(trimEmail[0]);
138+
128139
if (!isValidFirsChar) return false;
129140

130141
const hasOnlyOneAt: boolean =
131-
trimEmail.split('').reduce((total, current): number => {
132-
const isAtCode = GetCharCode(current) === AtCode;
133-
if (isAtCode) return (total = total + 1);
134-
return total;
135-
}, 0) === 1;
142+
trimEmail.split('').filter((char) => char === '@').length === 1;
143+
const hasOnlyOnePlus: boolean =
144+
trimEmail.split('').filter((char) => char === '+').length > 1;
136145

137-
if (!hasOnlyOneAt) return false;
146+
if (!hasOnlyOneAt || hasOnlyOnePlus) return false;
138147

139148
const isValidLength = HasValidLength(trimEmail);
140149

@@ -144,6 +153,8 @@ export const IsValidEmail = (email: string): boolean => {
144153

145154
if (!isValidDomain) return false;
146155

156+
if (!IsValidNick(trimEmail)) return false;
157+
147158
const hasInvalidChar = trimEmail
148159
.split('')
149160
.map((char): boolean => IsValidChar(char))

tests/utils/email-value-object.util.spec.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,31 @@ describe('email-value-object.util', () => {
1111
expect(valueObject.isOk()).toBe(true);
1212
});
1313

14+
it('should create a valid email with success', () => {
15+
const valueObject = EmailValueObject.create('[email protected]');
16+
expect(valueObject.isOk()).toBe(true);
17+
});
18+
19+
it('should to be invalid email nick ends with +', () => {
20+
const valueObject = EmailValueObject.create('[email protected]');
21+
expect(valueObject.isFail()).toBe(true);
22+
});
23+
24+
it('should to be invalid email nick starts with +', () => {
25+
const valueObject = EmailValueObject.create('[email protected]');
26+
expect(valueObject.isFail()).toBe(true);
27+
});
28+
29+
it('should to be invalid email domain starts with +', () => {
30+
const valueObject = EmailValueObject.create('johndoe@+domain.com');
31+
expect(valueObject.isFail()).toBe(true);
32+
});
33+
34+
it('should to be invalid email domain ends with +', () => {
35+
const valueObject = EmailValueObject.create('[email protected]+');
36+
expect(valueObject.isFail()).toBe(true);
37+
});
38+
1439
it('should transform value to lower on create', () => {
1540
const valueObject = EmailValueObject.create(
1641

0 commit comments

Comments
 (0)