Skip to content

Commit

Permalink
add type check
Browse files Browse the repository at this point in the history
  • Loading branch information
jesselpalmer committed Apr 14, 2024
1 parent 696ce2b commit 6d3ed14
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const resolveMx = util.promisify(dns.resolveMx);
* @return {boolean} - True if the email address is valid, false otherwise.
*/
const validateRfc5322 = (email) => {
return validator.isEmail(email);
return typeof email === 'string' && validator.isEmail(email);
};

/**
Expand Down
16 changes: 14 additions & 2 deletions test/index.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import emailValidator from '../src/index.js';

describe('Email Validator', () => {
// Testing with MX record check enabled
describe('with MX record check', () => {
test('should validate correct email format and MX record exists', async () => {
expect(await emailValidator('[email protected]')).toBe(true);
Expand All @@ -10,9 +9,15 @@ describe('Email Validator', () => {
test('should reject email from domain without MX records', async () => {
expect(await emailValidator('[email protected]')).toBe(false);
});

test('should reject non-string inputs', async () => {
expect(await emailValidator(undefined)).toBe(false);
expect(await emailValidator(null)).toBe(false);
expect(await emailValidator(1234)).toBe(false);
expect(await emailValidator({})).toBe(false);
});
});

// Testing without MX record check
describe('without MX record check', () => {
test('should validate correct email format regardless of MX records', async () => {
expect(await emailValidator('[email protected]', false)).toBe(true);
Expand All @@ -25,5 +30,12 @@ describe('Email Validator', () => {
test('should validate email from domain without MX records', async () => {
expect(await emailValidator('[email protected]', false)).toBe(true);
});

test('should reject non-string inputs', async () => {
expect(await emailValidator(undefined, false)).toBe(false);
expect(await emailValidator(null, false)).toBe(false);
expect(await emailValidator(1234, false)).toBe(false);
expect(await emailValidator({}, false)).toBe(false);
});
});
});

0 comments on commit 6d3ed14

Please sign in to comment.