Skip to content

Commit d2b3ff3

Browse files
authored
feat: add disable MX check param
2 parents 599fde6 + 5325d43 commit d2b3ff3

File tree

3 files changed

+56
-25
lines changed

3 files changed

+56
-25
lines changed

README.md

+26-14
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,15 @@
33

44
# Node Email Verifier
55

6-
Node Email Verifier is a email validation library for Node.js that checks if an
7-
email address has a valid format and verifies the domain's MX (Mail Exchange)
6+
Node Email Verifier is an email validation library for Node.js that checks if an
7+
email address has a valid format and optionally verifies the domain's MX (Mail Exchange)
88
records to ensure it can receive emails.
99

1010
## Features
1111

12-
- **RFC 5322 Format Validation**: Validates email addresses against the standard
13-
email formatting rules.
14-
- **MX Record Checking**: Verifies that the domain of the email address has
15-
valid MX records indicating that it can receive emails.
12+
- **RFC 5322 Format Validation**: Validates email addresses against the standard email formatting rules.
13+
- **MX Record Checking**: Verifies that the domain of the email address has valid MX records indicating that it can receive emails. This check can be disabled using a parameter.
14+
1615

1716
## Installation
1817

@@ -24,36 +23,49 @@ npm install node-email-verifier --save
2423

2524
## Usage
2625

27-
Here's a simple example of how to use Node Email Verifier:
26+
Here's how to use Node Email Verifier, with and without MX record checking:
2827

2928
```javascript
3029
import emailValidator from 'node-email-verifier';
3130

32-
async function validateEmail(email) {
31+
// Example with MX record checking
32+
async function validateEmailWithMx(email) {
3333
try {
3434
const isValid = await emailValidator(email);
35-
console.log(`Is "${email}" a valid email address?`, isValid);
35+
console.log(`Is "${email}" a valid email address with MX checking?`, isValid);
36+
} catch (error) {
37+
console.error('Error validating email with MX checking:', error);
38+
}
39+
}
40+
41+
// Example without MX record checking
42+
async function validateEmailWithoutMx(email) {
43+
try {
44+
const isValid = await emailValidator(email, false);
45+
console.log(`Is "${email}" a valid email address without MX checking?`, isValid);
3646
} catch (error) {
37-
console.error('Error validating email:', error);
47+
console.error('Error validating email without MX checking:', error);
3848
}
3949
}
4050

41-
validateEmail('[email protected]').then();
51+
validateEmailWithMx('[email protected]').then();
52+
validateEmailWithoutMx('[email protected]').then();
4253
```
4354

4455
## API
4556

46-
### ```async emailValidator(email)```
57+
### ```async emailValidator(email, checkMx = true)```
4758

48-
Validates the given email address.
59+
Validates the given email address, with an option to skip MX record verification.
4960

5061
#### Parameters
5162

5263
- ```email``` (string): The email address to validate.
64+
- ```checkMx``` (boolean): Whether to check for MX records, this defaults to true.
5365

5466
#### Returns
5567

56-
- ```Promise<boolean>```: A promise that resolves to true if the email address is valid, false otherwise.
68+
- ```Promise<boolean>```: A promise that resolves to true if the email address is valid and, if checked, has MX records; false otherwise.
5769

5870
## Contributing
5971

src/index.js

+9-4
Original file line numberDiff line numberDiff line change
@@ -34,17 +34,22 @@ const checkMxRecords = async (email) => {
3434

3535
/**
3636
* A sophisticated email validator that checks both the format of the email
37-
* address and the existence of MX records for the domain.
37+
* address and the existence of MX records for the domain, depending on the
38+
* checkMx parameter.
3839
*
3940
* @param {string} email - The email address to validate.
41+
* @param {boolean} checkMx - Determines whether to check for MX records.
42+
* Defaults to true.
4043
* @return {Promise<boolean>} - Promise that resolves to true if the email is
4144
* valid, false otherwise.
4245
*/
43-
const emailValidator = async (email) => {
46+
const emailValidator = async (email, checkMx = true) => {
4447
if (!validateRfc5322(email)) return false;
4548

46-
const hasMxRecords = await checkMxRecords(email);
47-
if (!hasMxRecords) return false;
49+
if (checkMx) {
50+
const hasMxRecords = await checkMxRecords(email);
51+
if (!hasMxRecords) return false;
52+
}
4853

4954
return true;
5055
};

test/index.test.js

+21-7
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,29 @@
11
import emailValidator from '../src/index.js';
22

33
describe('Email Validator', () => {
4-
test('should validate correct email format', async () => {
5-
expect(await emailValidator('[email protected]')).toBe(true);
6-
});
4+
// Testing with MX record check enabled
5+
describe('with MX record check', () => {
6+
test('should validate correct email format and MX record exists', async () => {
7+
expect(await emailValidator('[email protected]')).toBe(true);
8+
});
79

8-
test('should reject incorrect email format', async () => {
9-
expect(await emailValidator('invalid-email')).toBe(false);
10+
test('should reject email from domain without MX records', async () => {
11+
expect(await emailValidator('[email protected]')).toBe(false);
12+
});
1013
});
1114

12-
test('should reject email from domain without MX records', async () => {
13-
expect(await emailValidator('[email protected]')).toBe(false);
15+
// Testing without MX record check
16+
describe('without MX record check', () => {
17+
test('should validate correct email format regardless of MX records', async () => {
18+
expect(await emailValidator('[email protected]', false)).toBe(true);
19+
});
20+
21+
test('should reject incorrect email format regardless of MX records', async () => {
22+
expect(await emailValidator('invalid-email', false)).toBe(false);
23+
});
24+
25+
test('should validate email from domain without MX records', async () => {
26+
expect(await emailValidator('[email protected]', false)).toBe(true);
27+
});
1428
});
1529
});

0 commit comments

Comments
 (0)