Skip to content

Commit 6d3ed14

Browse files
committed
add type check
1 parent 696ce2b commit 6d3ed14

File tree

2 files changed

+15
-3
lines changed

2 files changed

+15
-3
lines changed

src/index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ const resolveMx = util.promisify(dns.resolveMx);
1212
* @return {boolean} - True if the email address is valid, false otherwise.
1313
*/
1414
const validateRfc5322 = (email) => {
15-
return validator.isEmail(email);
15+
return typeof email === 'string' && validator.isEmail(email);
1616
};
1717

1818
/**

test/index.test.js

+14-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import emailValidator from '../src/index.js';
22

33
describe('Email Validator', () => {
4-
// Testing with MX record check enabled
54
describe('with MX record check', () => {
65
test('should validate correct email format and MX record exists', async () => {
76
expect(await emailValidator('[email protected]')).toBe(true);
@@ -10,9 +9,15 @@ describe('Email Validator', () => {
109
test('should reject email from domain without MX records', async () => {
1110
expect(await emailValidator('[email protected]')).toBe(false);
1211
});
12+
13+
test('should reject non-string inputs', async () => {
14+
expect(await emailValidator(undefined)).toBe(false);
15+
expect(await emailValidator(null)).toBe(false);
16+
expect(await emailValidator(1234)).toBe(false);
17+
expect(await emailValidator({})).toBe(false);
18+
});
1319
});
1420

15-
// Testing without MX record check
1621
describe('without MX record check', () => {
1722
test('should validate correct email format regardless of MX records', async () => {
1823
expect(await emailValidator('[email protected]', false)).toBe(true);
@@ -25,5 +30,12 @@ describe('Email Validator', () => {
2530
test('should validate email from domain without MX records', async () => {
2631
expect(await emailValidator('[email protected]', false)).toBe(true);
2732
});
33+
34+
test('should reject non-string inputs', async () => {
35+
expect(await emailValidator(undefined, false)).toBe(false);
36+
expect(await emailValidator(null, false)).toBe(false);
37+
expect(await emailValidator(1234, false)).toBe(false);
38+
expect(await emailValidator({}, false)).toBe(false);
39+
});
2840
});
2941
});

0 commit comments

Comments
 (0)