From 6d3ed14e900b7656c691c6a7f295a163920f8547 Mon Sep 17 00:00:00 2001 From: Jesse Palmer Date: Sun, 14 Apr 2024 07:52:40 -0700 Subject: [PATCH] add type check --- src/index.js | 2 +- test/index.test.js | 16 ++++++++++++++-- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/src/index.js b/src/index.js index 94fdc48..7a02f4b 100644 --- a/src/index.js +++ b/src/index.js @@ -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); }; /** diff --git a/test/index.test.js b/test/index.test.js index 6043363..d565224 100644 --- a/test/index.test.js +++ b/test/index.test.js @@ -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('test@example.com')).toBe(true); @@ -10,9 +9,15 @@ describe('Email Validator', () => { test('should reject email from domain without MX records', async () => { expect(await emailValidator('test@adafwefewsd.com')).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('test@example.com', false)).toBe(true); @@ -25,5 +30,12 @@ describe('Email Validator', () => { test('should validate email from domain without MX records', async () => { expect(await emailValidator('test@adafwefewsd.com', 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); + }); }); });