@email-utils/validator-syntax 0.0.1-8 → 1.0.0-rc.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/bin/tag.sh DELETED
@@ -1,21 +0,0 @@
1
- #!/bin/bash
2
-
3
- if [ $1 ]; then
4
- echo "Creating a new $1 version."
5
-
6
- npm version $1
7
- version=`node -e "console.log(require('./package.json').version);"`
8
-
9
- npm run build
10
- git add -A
11
- git commit -m "Build for $version."
12
-
13
- echo "Publishing version $version."
14
-
15
- git push
16
- git push --tags
17
- npm publish --access public
18
- else
19
- echo "No version type provided. (ie. ./bin/tag.sh version)";
20
- fi
21
-
package/dist/index.js DELETED
@@ -1,273 +0,0 @@
1
- "use strict";
2
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
- return new (P || (P = Promise))(function (resolve, reject) {
5
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
- step((generator = generator.apply(thisArg, _arguments || [])).next());
9
- });
10
- };
11
- Object.defineProperty(exports, "__esModule", { value: true });
12
- const tlds = require("tlds/index.json");
13
- class EmailSyntaxValidator {
14
- constructor(configParam = {}) {
15
- this.defaultConfig = {
16
- local: {
17
- alphaUpper: true,
18
- alphaLower: true,
19
- numeric: true,
20
- period: true,
21
- printable: true,
22
- quote: true,
23
- hyphen: true,
24
- spaces: true,
25
- },
26
- domain: {
27
- alphaUpper: true,
28
- alphaLower: true,
29
- numeric: true,
30
- period: true,
31
- hyphen: true,
32
- tld: true,
33
- localhost: false,
34
- charsBeforeDot: 1,
35
- charsAfterDot: 2,
36
- },
37
- };
38
- this.config = this.defaultConfig;
39
- this.chars = {
40
- alphaLower: 'abcdefghijklmnopqrstuvwxyz',
41
- alphaUpper: 'ABCDEFGHIJKLMNOPQRSTUVWXYZ',
42
- numeric: '0123456789',
43
- dot: ',',
44
- printable: "!#$%&'*+/=?^_`{|}~",
45
- space: ' ',
46
- quote: '"',
47
- hyphen: '-',
48
- period: '.',
49
- };
50
- this.email = '';
51
- this.originalEmail = '';
52
- let key;
53
- console.log('validate config');
54
- for (key in configParam) {
55
- const configGroup = configParam[key];
56
- for (const subKey in configGroup) {
57
- const subKeyType = subKey;
58
- this.config[key][subKeyType] = configGroup[subKeyType];
59
- }
60
- }
61
- }
62
- gatherChars(charConfig) {
63
- let characters = '';
64
- if (charConfig.alphaUpper === true) {
65
- characters += this.chars.alphaUpper;
66
- }
67
- if (charConfig.alphaLower === true) {
68
- characters += this.chars.alphaLower;
69
- }
70
- if (charConfig.numeric === true) {
71
- characters += this.chars.numeric;
72
- }
73
- if (charConfig.hyphen === true) {
74
- characters += this.chars.hyphen;
75
- }
76
- if ('printable' in charConfig && charConfig.printable === true) {
77
- characters += this.chars.printable;
78
- }
79
- if ('spaces' in charConfig && charConfig.spaces === true) {
80
- characters += this.chars.space;
81
- }
82
- if ('quote' in charConfig && charConfig.quote === true) {
83
- characters += this.chars.quote;
84
- }
85
- if (charConfig.period === true) {
86
- characters += this.chars.period;
87
- }
88
- return characters.split('');
89
- }
90
- validateGeneral() {
91
- const atSymbolIndex = this.email.indexOf('@');
92
- if (atSymbolIndex < 1) {
93
- return false;
94
- }
95
- return true;
96
- }
97
- validateChars(chars, charsToValidate) {
98
- let quoteOpen = false;
99
- for (let l = 0; l < charsToValidate.length; l++) {
100
- const letter = charsToValidate[l];
101
- if (this.config.local.quote) {
102
- if (letter === '"') {
103
- quoteOpen = !quoteOpen;
104
- if (l !== 0 && l !== charsToValidate.length - 1) {
105
- if (quoteOpen && !(charsToValidate[l - 1] === '.')) {
106
- return false;
107
- }
108
- else if (!quoteOpen && !(charsToValidate[l + 1] === '.')) {
109
- return false;
110
- }
111
- }
112
- }
113
- }
114
- const letterInAllowed = chars.indexOf(letter) !== -1;
115
- if (!letterInAllowed) {
116
- return false;
117
- }
118
- }
119
- return true;
120
- }
121
- validateLocal() {
122
- const validChars = this.gatherChars(this.config.local);
123
- let localLetters = this.email.slice(0, this.email.indexOf('@')).split('');
124
- if (this.config.local.printable) {
125
- const percentInLocalIndex = localLetters.indexOf('%');
126
- if (percentInLocalIndex !== -1) {
127
- localLetters = localLetters.slice(0, percentInLocalIndex);
128
- }
129
- const bangInLocalIndex = localLetters.indexOf('!');
130
- if (bangInLocalIndex !== -1) {
131
- localLetters = localLetters.slice(bangInLocalIndex);
132
- }
133
- }
134
- if (localLetters.length > 64) {
135
- return false;
136
- }
137
- const charsValid = this.validateChars(validChars, localLetters);
138
- if (!charsValid) {
139
- return false;
140
- }
141
- const indexOfDoublePeriods = this.email.indexOf('..');
142
- if (indexOfDoublePeriods !== -1) {
143
- const atSymbolIndex = this.email.indexOf('@');
144
- if (indexOfDoublePeriods > atSymbolIndex) {
145
- return false;
146
- }
147
- const indexOfFirstQuoteInLocal = localLetters.indexOf('"');
148
- const indexOfLastQuoteInLocal = localLetters.length -
149
- `${localLetters}`.split('').reverse().join('').indexOf('"') -
150
- 1;
151
- if (indexOfFirstQuoteInLocal < 0 || indexOfLastQuoteInLocal < 0) {
152
- return false;
153
- }
154
- if (indexOfFirstQuoteInLocal >= indexOfDoublePeriods ||
155
- indexOfDoublePeriods >= indexOfLastQuoteInLocal) {
156
- return false;
157
- }
158
- }
159
- const indexOfSpace = localLetters.indexOf(' ');
160
- if (indexOfSpace !== -1 &&
161
- (localLetters[indexOfSpace + 1] !== '"' ||
162
- localLetters[indexOfSpace - 1] !== '"')) {
163
- return false;
164
- }
165
- return true;
166
- }
167
- validateDomainParts(domain) {
168
- const dotIndex = domain.lastIndexOf('.');
169
- if (dotIndex === -1 && this.config.domain.localhost === false) {
170
- return false;
171
- }
172
- if (dotIndex !== -1) {
173
- if (this.config.domain.charsBeforeDot !== -1) {
174
- if (dotIndex <= this.config.domain.charsBeforeDot) {
175
- return false;
176
- }
177
- }
178
- if (this.config.domain.charsAfterDot !== -1) {
179
- if (dotIndex === domain.length - this.config.domain.charsAfterDot) {
180
- return false;
181
- }
182
- }
183
- if (this.config.domain.tld) {
184
- const tld = domain.slice(dotIndex + 1);
185
- let tldExists = false;
186
- for (let t = 0; t < tlds.length; t++) {
187
- const currentTld = tlds[t];
188
- if (tld.toLowerCase() === currentTld) {
189
- tldExists = true;
190
- }
191
- if (tldExists) {
192
- break;
193
- }
194
- }
195
- if (!tldExists) {
196
- return false;
197
- }
198
- }
199
- }
200
- return true;
201
- }
202
- validateDomain() {
203
- const domain = this.email.slice(this.email.indexOf('@') + 1);
204
- const domainPartsValid = this.validateDomainParts(domain);
205
- if (!domainPartsValid) {
206
- return false;
207
- }
208
- const validChars = this.gatherChars(this.config.domain);
209
- const domainLetters = domain.split('');
210
- if (this.config.local.printable) {
211
- const localLetters = this.email
212
- .slice(0, this.email.indexOf('@'))
213
- .split('');
214
- const percentInLocalIndex = localLetters.indexOf('%');
215
- if (percentInLocalIndex !== -1) {
216
- const relayDomain = localLetters.slice(percentInLocalIndex + 1);
217
- const relayDomainPartsValid = this.validateDomainParts(relayDomain.join(''));
218
- if (!relayDomainPartsValid) {
219
- return false;
220
- }
221
- const charsValid = this.validateChars(validChars, relayDomain);
222
- if (!charsValid) {
223
- return false;
224
- }
225
- }
226
- const bangInLocalIndex = localLetters.indexOf('!');
227
- if (bangInLocalIndex !== -1) {
228
- const relayDomain = localLetters.slice(0, bangInLocalIndex);
229
- const relayDomainPartsValid = this.validateDomainParts(relayDomain.join(''));
230
- if (!relayDomainPartsValid) {
231
- return false;
232
- }
233
- const charsValid = this.validateChars(validChars, relayDomain);
234
- if (!charsValid) {
235
- return false;
236
- }
237
- }
238
- }
239
- const charsValid = this.validateChars(validChars, domainLetters);
240
- if (!charsValid) {
241
- return false;
242
- }
243
- return true;
244
- }
245
- validate(email) {
246
- return __awaiter(this, void 0, void 0, function* () {
247
- if (typeof email !== 'string') {
248
- console.error('Email not a string.', email);
249
- return false;
250
- }
251
- else if (!email) {
252
- console.error('Email not provided.', email);
253
- return false;
254
- }
255
- this.email = email;
256
- const generalValid = this.validateGeneral();
257
- if (!generalValid) {
258
- return false;
259
- }
260
- const localValid = this.validateLocal();
261
- if (!localValid) {
262
- return false;
263
- }
264
- const domainValid = this.validateDomain();
265
- if (!domainValid) {
266
- return false;
267
- }
268
- return true;
269
- });
270
- }
271
- }
272
- exports.default = EmailSyntaxValidator;
273
- //# sourceMappingURL=index.js.map
package/dist/index.js.map DELETED
@@ -1 +0,0 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;AAGA,wCAAuC;AACvC,MAAM,oBAAoB;IAuCxB,YAAY,cAAmC,EAAyB;QAtCxE,kBAAa,GAAyB;YACpC,KAAK,EAAE;gBACL,UAAU,EAAE,IAAI;gBAChB,UAAU,EAAE,IAAI;gBAChB,OAAO,EAAE,IAAI;gBACb,MAAM,EAAE,IAAI;gBACZ,SAAS,EAAE,IAAI;gBACf,KAAK,EAAE,IAAI;gBACX,MAAM,EAAE,IAAI;gBACZ,MAAM,EAAE,IAAI;aACb;YACD,MAAM,EAAE;gBACN,UAAU,EAAE,IAAI;gBAChB,UAAU,EAAE,IAAI;gBAChB,OAAO,EAAE,IAAI;gBACb,MAAM,EAAE,IAAI;gBACZ,MAAM,EAAE,IAAI;gBACZ,GAAG,EAAE,IAAI;gBACT,SAAS,EAAE,KAAK;gBAChB,cAAc,EAAE,CAAC;gBACjB,aAAa,EAAE,CAAC;aACjB;SACF,CAAA;QACD,WAAM,GAAyB,IAAI,CAAC,aAAa,CAAA;QACjD,UAAK,GAAG;YACN,UAAU,EAAE,4BAA4B;YACxC,UAAU,EAAE,4BAA4B;YACxC,OAAO,EAAE,YAAY;YACrB,GAAG,EAAE,GAAG;YACR,SAAS,EAAE,oBAAoB;YAC/B,KAAK,EAAE,GAAG;YACV,KAA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,OAAO,KAAK,CAAA;iBACb;gBAED,MAAM,UAAU,GAAG,IAAI,CAAC,aAAa,CAAC,UAAU,EAAE,WAAW,CAAC,CAAA;gBAE9D,IAAI,CAAC,UAAU,EAAE;oBACf,OAAO,KAAK,CAAA;iBACb;aACF;SACF;QAED,MAAM,UAAU,GAAG,IAAI,CAAC,aAAa,CAAC,UAAU,EAAE,aAAa,CAAC,CAAA;QAEhE,IAAI,CAAC,UAAU,EAAE;YACf,OAAO,KAAK,CAAA;SACb;QAED,OAAO,IAAI,CAAA;IACb,CAAC;IAEY,QAAQ,CAAC,KAAa;;YACjC,IAAI,OAAO,KAAK,KAAK,QAAQ,EAAE;gBAC7B,OAAO,CAAC,KAAK,CAAC,qBAAqB,EAAE,KAAK,CAAC,CAAA;gBAC3C,OAAO,KAAK,CAAA;aACb;iBAAM,IAAI,CAAC,KAAK,EAAE;gBACjB,OAAO,CAAC,KAAK,CAAC,qBAAqB,EAAE,KAAK,CAAC,CAAA;gBAC3C,OAAO,KAAK,CAAA;aACb;YAED,IAAI,CAAC,KAAK,GAAG,KAAK,CAAA;YAGlB,MAAM,YAAY,GAAG,IAAI,CAAC,eAAe,EAAE,CAAA;YAC3C,IAAI,CAAC,YAAY,EAAE;gBACjB,OAAO,KAAK,CAAA;aACb;YAED,MAAM,UAAU,GAAG,IAAI,CAAC,aAAa,EAAE,CAAA;YACvC,IAAI,CAAC,UAAU,EAAE;gBACf,OAAO,KAAK,CAAA;aACb;YAED,MAAM,WAAW,GAAG,IAAI,CAAC,cAAc,EAAE,CAAA;YACzC,IAAI,CAAC,WAAW,EAAE;gBAChB,OAAO,KAAK,CAAA;aACb;YAED,OAAO,IAAI,CAAA;QACb,CAAC;KAAA;CACF;AAED,kBAAe,oBAAoB,CAAA"}
package/src/index.ts DELETED
@@ -1,344 +0,0 @@
1
- // VALID EMAIL
2
- // Attempting to follow rules from https://en.wikipedia.org/wiki/Email_address.
3
- // Checks if the email is valid.
4
- import * as tlds from 'tlds/index.json'
5
- class EmailSyntaxValidator {
6
- defaultConfig: EmailValidatorConfig = {
7
- local: {
8
- alphaUpper: true,
9
- alphaLower: true,
10
- numeric: true,
11
- period: true,
12
- printable: true,
13
- quote: true,
14
- hyphen: true,
15
- spaces: true,
16
- },
17
- domain: {
18
- alphaUpper: true,
19
- alphaLower: true,
20
- numeric: true,
21
- period: true,
22
- hyphen: true,
23
- tld: true,
24
- localhost: false,
25
- charsBeforeDot: 1, // -1 means don't check.
26
- charsAfterDot: 2, // -1 means don't check.
27
- },
28
- }
29
- config: EmailValidatorConfig = this.defaultConfig
30
- chars = {
31
- alphaLower: 'abcdefghijklmnopqrstuvwxyz',
32
- alphaUpper: 'ABCDEFGHIJKLMNOPQRSTUVWXYZ',
33
- numeric: '0123456789',
34
- dot: ',',
35
- printable: "!#$%&'*+/=?^_`{|}~",
36
- space: ' ',
37
- quote: '"',
38
- hyphen: '-',
39
- period: '.',
40
- }
41
- email = ''
42
- originalEmail = ''
43
-
44
- constructor(configParam: EmailValidatorParam = {} as EmailValidatorParam) {
45
- let key: keyof EmailValidatorConfig
46
-
47
- console.log('validate config')
48
-
49
- for (key in configParam) {
50
- const configGroup = configParam[key]
51
- for (const subKey in configGroup) {
52
- const subKeyType = subKey as keyof typeof configGroup
53
- this.config[key][subKeyType] = configGroup[subKeyType]
54
- }
55
- }
56
- }
57
- private gatherChars(
58
- charConfig: LocalValidateConfig | DomainValidateConfig
59
- ): string[] {
60
- let characters = ''
61
-
62
- if (charConfig.alphaUpper === true) {
63
- characters += this.chars.alphaUpper
64
- }
65
- if (charConfig.alphaLower === true) {
66
- characters += this.chars.alphaLower
67
- }
68
- if (charConfig.numeric === true) {
69
- characters += this.chars.numeric
70
- }
71
-
72
- if (charConfig.hyphen === true) {
73
- characters += this.chars.hyphen
74
- }
75
- if ('printable' in charConfig && charConfig.printable === true) {
76
- characters += this.chars.printable
77
- }
78
-
79
- if ('spaces' in charConfig && charConfig.spaces === true) {
80
- characters += this.chars.space
81
- }
82
-
83
- if ('quote' in charConfig && charConfig.quote === true) {
84
- characters += this.chars.quote
85
- }
86
-
87
- if (charConfig.period === true) {
88
- characters += this.chars.period
89
- }
90
-
91
- return characters.split('')
92
- }
93
- private validateGeneral(): boolean {
94
- // Confirm that there is an at symbol and it isn't the first character.
95
- const atSymbolIndex = this.email.indexOf('@')
96
- if (atSymbolIndex < 1) {
97
- return false
98
- }
99
-
100
- return true
101
- }
102
- private validateChars(chars: string[], charsToValidate: string[]): boolean {
103
- let quoteOpen = false
104
- for (let l = 0; l < charsToValidate.length; l++) {
105
- const letter = charsToValidate[l]
106
- if (this.config.local.quote) {
107
- if (letter === '"') {
108
- quoteOpen = !quoteOpen
109
-
110
- if (l !== 0 && l !== charsToValidate.length - 1) {
111
- if (quoteOpen && !(charsToValidate[l - 1] === '.')) {
112
- return false
113
- } else if (!quoteOpen && !(charsToValidate[l + 1] === '.')) {
114
- return false
115
- }
116
- }
117
- }
118
- }
119
-
120
- const letterInAllowed = chars.indexOf(letter) !== -1
121
-
122
- if (!letterInAllowed) {
123
- return false
124
- }
125
- }
126
-
127
- return true
128
- }
129
- private validateLocal(): boolean {
130
- const validChars = this.gatherChars(this.config.local)
131
- let localLetters = this.email.slice(0, this.email.indexOf('@')).split('')
132
-
133
- if (this.config.local.printable) {
134
- const percentInLocalIndex = localLetters.indexOf('%')
135
-
136
- if (percentInLocalIndex !== -1) {
137
- localLetters = localLetters.slice(0, percentInLocalIndex)
138
- }
139
-
140
- const bangInLocalIndex = localLetters.indexOf('!')
141
-
142
- if (bangInLocalIndex !== -1) {
143
- localLetters = localLetters.slice(bangInLocalIndex)
144
- }
145
- }
146
-
147
- // Validate local length
148
- if (localLetters.length > 64) {
149
- return false
150
- }
151
-
152
- const charsValid = this.validateChars(validChars, localLetters)
153
-
154
- if (!charsValid) {
155
- return false
156
- }
157
-
158
- // Make sure there are not two periods together.
159
- const indexOfDoublePeriods = this.email.indexOf('..')
160
-
161
- if (indexOfDoublePeriods !== -1) {
162
- // If double periods before at symbol it's invalid.
163
- const atSymbolIndex = this.email.indexOf('@')
164
- if (indexOfDoublePeriods > atSymbolIndex) {
165
- return false
166
- }
167
-
168
- // If double period is not within quotes.
169
- const indexOfFirstQuoteInLocal = localLetters.indexOf('"')
170
- const indexOfLastQuoteInLocal =
171
- localLetters.length -
172
- `${localLetters}`.split('').reverse().join('').indexOf('"') -
173
- 1
174
-
175
- if (indexOfFirstQuoteInLocal < 0 || indexOfLastQuoteInLocal < 0) {
176
- return false
177
- }
178
-
179
- if (
180
- indexOfFirstQuoteInLocal >= indexOfDoublePeriods ||
181
- indexOfDoublePeriods >= indexOfLastQuoteInLocal
182
- ) {
183
- return false
184
- }
185
- }
186
-
187
- // Confirm that there is no spaces that are not surrounded by quotes.
188
- const indexOfSpace = localLetters.indexOf(' ')
189
-
190
- if (
191
- indexOfSpace !== -1 &&
192
- (localLetters[indexOfSpace + 1] !== '"' ||
193
- localLetters[indexOfSpace - 1] !== '"')
194
- ) {
195
- return false
196
- }
197
-
198
- return true
199
- }
200
- private validateDomainParts(domain: string): boolean {
201
- const dotIndex = domain.lastIndexOf('.')
202
-
203
- if (dotIndex === -1 && this.config.domain.localhost === false) {
204
- return false
205
- }
206
-
207
- if (dotIndex !== -1) {
208
- if (this.config.domain.charsBeforeDot !== -1) {
209
- if (dotIndex <= this.config.domain.charsBeforeDot) {
210
- return false
211
- }
212
- }
213
-
214
- if (this.config.domain.charsAfterDot !== -1) {
215
- if (dotIndex === domain.length - this.config.domain.charsAfterDot) {
216
- return false
217
- }
218
- }
219
-
220
- if (this.config.domain.tld) {
221
- const tld = domain.slice(dotIndex + 1)
222
-
223
- let tldExists = false
224
-
225
- for (let t = 0; t < tlds.length; t++) {
226
- const currentTld = tlds[t]
227
-
228
- if (tld.toLowerCase() === currentTld) {
229
- tldExists = true
230
- }
231
-
232
- if (tldExists) {
233
- break
234
- }
235
- }
236
-
237
- if (!tldExists) {
238
- return false
239
- }
240
- }
241
- }
242
-
243
- return true
244
- }
245
- private validateDomain(): boolean {
246
- // Confirm that there is a dot at least 1 character after the at and 2 characters before the end.
247
- const domain = this.email.slice(this.email.indexOf('@') + 1)
248
-
249
- const domainPartsValid = this.validateDomainParts(domain)
250
-
251
- if (!domainPartsValid) {
252
- return false
253
- }
254
-
255
- const validChars = this.gatherChars(this.config.domain)
256
- const domainLetters = domain.split('')
257
-
258
- if (this.config.local.printable) {
259
- const localLetters = this.email
260
- .slice(0, this.email.indexOf('@'))
261
- .split('')
262
-
263
- const percentInLocalIndex = localLetters.indexOf('%')
264
-
265
- if (percentInLocalIndex !== -1) {
266
- const relayDomain = localLetters.slice(percentInLocalIndex + 1)
267
-
268
- const relayDomainPartsValid = this.validateDomainParts(
269
- relayDomain.join('')
270
- )
271
-
272
- if (!relayDomainPartsValid) {
273
- return false
274
- }
275
-
276
- const charsValid = this.validateChars(validChars, relayDomain)
277
-
278
- if (!charsValid) {
279
- return false
280
- }
281
- }
282
-
283
- const bangInLocalIndex = localLetters.indexOf('!')
284
-
285
- if (bangInLocalIndex !== -1) {
286
- const relayDomain = localLetters.slice(0, bangInLocalIndex)
287
-
288
- const relayDomainPartsValid = this.validateDomainParts(
289
- relayDomain.join('')
290
- )
291
-
292
- if (!relayDomainPartsValid) {
293
- return false
294
- }
295
-
296
- const charsValid = this.validateChars(validChars, relayDomain)
297
-
298
- if (!charsValid) {
299
- return false
300
- }
301
- }
302
- }
303
-
304
- const charsValid = this.validateChars(validChars, domainLetters)
305
-
306
- if (!charsValid) {
307
- return false
308
- }
309
-
310
- return true
311
- }
312
-
313
- public async validate(email: string): Promise<boolean> {
314
- if (typeof email !== 'string') {
315
- console.error('Email not a string.', email)
316
- return false
317
- } else if (!email) {
318
- console.error('Email not provided.', email)
319
- return false
320
- }
321
-
322
- this.email = email
323
- // Need to add support for special local chars that are properly quoted.
324
- // Need to add support for quotes in ().
325
- const generalValid = this.validateGeneral()
326
- if (!generalValid) {
327
- return false
328
- }
329
-
330
- const localValid = this.validateLocal()
331
- if (!localValid) {
332
- return false
333
- }
334
-
335
- const domainValid = this.validateDomain()
336
- if (!domainValid) {
337
- return false
338
- }
339
-
340
- return true
341
- }
342
- }
343
-
344
- export default EmailSyntaxValidator