Skip to content

Commit 32d8872

Browse files
committed
add options
1 parent 60814a0 commit 32d8872

File tree

3 files changed

+63
-14
lines changed

3 files changed

+63
-14
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
Validates emails based on regex, common typos, disposable email blacklists, DNS records and SMTP server response.
44

5-
- Validates email is compliant with RFC standard using regex.
5+
- Validates email is compliant with [RFC standard](https://tools.ietf.org/html/rfc2822#appendix-A.1.2) using regex.
66
- Validates common typos e.g. [email protected].
77
- Validates email was not generated by disposable email service.
88
- Validates MX records are present on DNS.
@@ -26,6 +26,7 @@ const main = async () => {
2626
let res = await validate('[email protected]')
2727
// {
2828
// "valid": false,
29+
// "reason": "smtp",
2930
// "validators": {
3031
// "regex": {
3132
// "valid": true

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "deep-email-validator",
3-
"version": "0.1.8",
3+
"version": "0.1.9",
44
"files": [
55
"dist/**/*"
66
],

src/index.ts

Lines changed: 60 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,25 +6,73 @@ import { checkSMTP } from './smtp/smtp'
66
import { checkDisposable } from './disposable/disposable'
77
import './types'
88

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+
939
export async function validate(
10-
recipient: string,
11-
sender: string = '[email protected]'
40+
emailOrOptions: string | ValidatorOptions
1241
): Promise<OutputFormat> {
13-
console.log('recipient', recipient)
14-
if (!isEmail(recipient)) return createOutput('regex', 'Invalid regex')
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+
}
1550

16-
const typoResponse = await checkTypo(recipient)
17-
if (typoResponse) return createOutput('typo', typoResponse)
51+
if (options.validateRegex && !isEmail(email))
52+
return createOutput('regex', 'Invalid regex')
1853

19-
const domain = recipient.split('@')[1]
54+
if (options.validateTypo) {
55+
const typoResponse = await checkTypo(email)
56+
if (typoResponse) return createOutput('typo', typoResponse)
57+
}
2058

21-
const disposableResponse = await checkDisposable(domain)
22-
if (disposableResponse) return createOutput('disposable', disposableResponse)
59+
const domain = email.split('@')[1]
2360

24-
const mx = await getBestMx(domain)
25-
if (!mx) return createOutput('mx', 'MX record not found')
61+
if (options.validateDisposable) {
62+
const disposableResponse = await checkDisposable(domain)
63+
if (disposableResponse)
64+
return createOutput('disposable', disposableResponse)
65+
}
66+
67+
if (options.validateMx) {
68+
const mx = await getBestMx(domain)
69+
if (!mx) return createOutput('mx', 'MX record not found')
70+
if (options.validateSMTP) {
71+
return checkSMTP(options.sender, email, mx.exchange)
72+
}
73+
}
2674

27-
return checkSMTP(sender, recipient, mx.exchange)
75+
return createOutput()
2876
}
2977

3078
async function main() {

0 commit comments

Comments
 (0)