|
1 | | -import isEmail from 'validator/lib/isEmail' |
2 | | -import { OutputFormat, createOutput } from './output/output' |
| 1 | +import { isEmail } from './regex/regex' |
3 | 2 | import { checkTypo } from './typo/typo' |
4 | 3 | import { getBestMx } from './dns/dns' |
5 | 4 | import { checkSMTP } from './smtp/smtp' |
6 | 5 | import { checkDisposable } from './disposable/disposable' |
| 6 | +import { getOptions, ValidatorOptions } from './options/options' |
| 7 | +import { OutputFormat, createOutput } from './output/output' |
7 | 8 | import './types' |
8 | 9 |
|
9 | | -const defaultOptions = { |
10 | | - email: 'name@example.org', |
11 | | - sender: 'name@example.org', |
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 | | - |
39 | 10 | export async function validate( |
40 | 11 | emailOrOptions: string | ValidatorOptions |
41 | 12 | ): 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 |
50 | 15 |
|
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 | + } |
53 | 20 |
|
54 | 21 | if (options.validateTypo) { |
55 | 22 | const typoResponse = await checkTypo(email) |
|
0 commit comments