Skip to content

Commit 14d09a8

Browse files
committed
remove regex lib
1 parent b7d8f0f commit 14d09a8

File tree

8 files changed

+68
-59
lines changed

8 files changed

+68
-59
lines changed

.vscode/settings.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"cSpell.words": [
3+
"smtp"
4+
]
5+
}

package.json

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,23 +41,21 @@
4141
"dependencies": {
4242
"axios": "^0.19.2",
4343
"lodash": "^4.17.15",
44-
"mailcheck": "^1.1.1",
45-
"validator": "^12.2.0"
44+
"mailcheck": "^1.1.1"
4645
},
4746
"devDependencies": {
48-
"typescript": "^3.8.3",
4947
"@types/eslint": "^6.1.1",
5048
"@types/jest": "^24.0.23",
5149
"@types/lodash": "^4.14.141",
5250
"@types/mailcheck": "^1.1.31",
5351
"@types/node": "^12.7.8",
54-
"@types/validator": "^12.0.1",
5552
"@typescript-eslint/eslint-plugin": "^2.3.1",
5653
"@typescript-eslint/parser": "^2.3.1",
5754
"eslint": "^6.4.0",
5855
"jest": "^24.9.0",
5956
"nodemon": "^1.19.2",
6057
"ts-jest": "^24.1.0",
61-
"ts-node": "^8.4.1"
58+
"ts-node": "^8.4.1",
59+
"typescript": "^3.8.3"
6260
}
6361
}

src/index.ts

Lines changed: 9 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,22 @@
1-
import isEmail from 'validator/lib/isEmail'
2-
import { OutputFormat, createOutput } from './output/output'
1+
import { isEmail } from './regex/regex'
32
import { checkTypo } from './typo/typo'
43
import { getBestMx } from './dns/dns'
54
import { checkSMTP } from './smtp/smtp'
65
import { checkDisposable } from './disposable/disposable'
6+
import { getOptions, ValidatorOptions } from './options/options'
7+
import { OutputFormat, createOutput } from './output/output'
78
import './types'
89

9-
const defaultOptions = {
10-
11-
sender: '[email protected]',
12-
validateRegex: true,
13-
validateMx: true,
14-
validateTypo: true,
15-
validateDisposable: true,
16-
validateSMTP: true,
17-
}
18-
19-
type ValidatorOptions = {
20-
email: string
21-
sender?: string
22-
validateRegex?: boolean
23-
validateMx?: boolean
24-
validateTypo?: boolean
25-
validateDisposable?: boolean
26-
validateSMTP?: boolean
27-
}
28-
29-
type ValidatorOptionsFinal = {
30-
email: string
31-
sender: string
32-
validateRegex: boolean
33-
validateMx: boolean
34-
validateTypo: boolean
35-
validateDisposable: boolean
36-
validateSMTP: boolean
37-
}
38-
3910
export async function validate(
4011
emailOrOptions: string | ValidatorOptions
4112
): Promise<OutputFormat> {
42-
let email: string
43-
let options: ValidatorOptionsFinal = defaultOptions
44-
if (typeof emailOrOptions === 'string') {
45-
email = emailOrOptions
46-
} else {
47-
email = emailOrOptions.email
48-
options = { ...options, ...emailOrOptions }
49-
}
13+
const options = getOptions(emailOrOptions)
14+
const email = options.email
5015

51-
if (options.validateRegex && !isEmail(email))
52-
return createOutput('regex', 'Invalid regex')
16+
if (options.validateRegex) {
17+
const regexResponse = isEmail(email)
18+
if (regexResponse) return createOutput('regex', regexResponse)
19+
}
5320

5421
if (options.validateTypo) {
5522
const typoResponse = await checkTypo(email)

src/options/options.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
const defaultOptions: ValidatorOptionsFinal = {
2+
3+
sender: '[email protected]',
4+
validateRegex: true,
5+
validateMx: true,
6+
validateTypo: true,
7+
validateDisposable: true,
8+
validateSMTP: true,
9+
}
10+
11+
type Options = {
12+
sender: string
13+
validateRegex: boolean
14+
validateMx: boolean
15+
validateTypo: boolean
16+
validateDisposable: boolean
17+
validateSMTP: boolean
18+
}
19+
20+
export type ValidatorOptions = Partial<Options> & { email: string }
21+
type ValidatorOptionsFinal = Options & { email: string }
22+
23+
export function getOptions(
24+
emailOrOptions: string | ValidatorOptions
25+
): ValidatorOptionsFinal {
26+
let options: ValidatorOptionsFinal = defaultOptions
27+
28+
if (typeof emailOrOptions === 'string') {
29+
options = { ...options, email: emailOrOptions }
30+
} else {
31+
options = { ...options, ...emailOrOptions }
32+
}
33+
return options
34+
}

src/regex/regex.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
export const isEmail = (email: string): string | undefined => {
2+
email = (email || '').trim()
3+
if (email.length === 0) {
4+
return 'Email not provided'
5+
}
6+
const split = email.split('@')
7+
if (split.length < 2) {
8+
return 'Email does not contain "@".'
9+
} else {
10+
const [domain] = split.slice(-1)
11+
if (domain.indexOf('.') === -1) {
12+
return 'Must contain a "." after the "@".'
13+
}
14+
}
15+
}

test/__snapshots__/index.test.ts.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ Object {
112112
"valid": false,
113113
},
114114
"regex": Object {
115-
"reason": "Invalid regex",
115+
"reason": "Email does not contain \\"@\\".",
116116
"valid": false,
117117
},
118118
"smtp": Object {

test/index.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ const elevenSeconds = 11 * 1000
66

77
describe('validation tests', () => {
88
it('fails with bad regex', async () => {
9-
const res = await validate('dav id@gmail.com')
9+
const res = await validate('david.gmail.com')
1010
expect(res.valid).toBe(false)
1111
expect(res.reason).toBe('regex')
1212
expect(res.validators.regex?.valid).toBe(false)

yarn.lock

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -404,11 +404,6 @@
404404
resolved "https://registry.yarnpkg.com/@types/stack-utils/-/stack-utils-1.0.1.tgz#0a851d3bd96498fa25c33ab7278ed3bd65f06c3e"
405405
integrity sha512-l42BggppR6zLmpfU6fq9HEa2oGPEI8yrSPL3GITjfRInppYFahObbIQOQK3UGxEnyQpltZLaPe75046NOZQikw==
406406

407-
"@types/validator@^12.0.1":
408-
version "12.0.1"
409-
resolved "https://registry.yarnpkg.com/@types/validator/-/validator-12.0.1.tgz#73dbc7f5f730ff7131754bca682824eb3c260b79"
410-
integrity sha512-l57fIANZLMe8DArz+SDb+7ATXnDm15P7u2wHBw5mb0aSMd+UuvmvhouBF2hdLgQPDMJ39sh9g2MJO4GkZ0VAdQ==
411-
412407
"@types/yargs-parser@*":
413408
version "15.0.0"
414409
resolved "https://registry.yarnpkg.com/@types/yargs-parser/-/yargs-parser-15.0.0.tgz#cb3f9f741869e20cce330ffbeb9271590483882d"
@@ -4197,11 +4192,6 @@ validate-npm-package-license@^3.0.1:
41974192
spdx-correct "^3.0.0"
41984193
spdx-expression-parse "^3.0.0"
41994194

4200-
validator@^12.2.0:
4201-
version "12.2.0"
4202-
resolved "https://registry.yarnpkg.com/validator/-/validator-12.2.0.tgz#660d47e96267033fd070096c3b1a6f2db4380a0a"
4203-
integrity sha512-jJfE/DW6tIK1Ek8nCfNFqt8Wb3nzMoAbocBF6/Icgg1ZFSBpObdnwVY2jQj6qUqzhx5jc71fpvBWyLGO7Xl+nQ==
4204-
42054195
42064196
version "1.10.0"
42074197
resolved "https://registry.yarnpkg.com/verror/-/verror-1.10.0.tgz#3a105ca17053af55d6e270c1f8288682e18da400"

0 commit comments

Comments
 (0)