-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
implemented backend validation for valid email and domain
- Loading branch information
1 parent
0cf8aee
commit 37b3b2b
Showing
4 changed files
with
88 additions
and
0 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import { isValidEmail } from './emailValidator'; | ||
import dns from 'dns'; | ||
|
||
jest.mock('dns'); | ||
|
||
const mockedDns = dns as jest.Mocked<typeof dns>; | ||
|
||
describe('isValidEmail Function', () => { | ||
|
||
afterEach(() => { | ||
jest.resetAllMocks(); | ||
}); | ||
|
||
test('should return false for an invalid email format', async () => { | ||
const result = await isValidEmail('invalid-email'); | ||
expect(result).toBe(false); | ||
}); | ||
|
||
test('should return true for a valid email with MX records', async () => { | ||
mockedDns.resolveMx.mockImplementation((domain, callback) => { | ||
callback(null, [{ exchange: 'mail.google.com', priority: 10 }]); | ||
}); | ||
|
||
const result = await isValidEmail('[email protected]'); | ||
expect(result).toBe(true); | ||
}); | ||
|
||
test('should return false for a valid email but no MX records', async () => { | ||
mockedDns.resolveMx.mockImplementation((domain, callback) => { | ||
callback(null, []); | ||
}); | ||
|
||
const result = await isValidEmail('[email protected]'); | ||
expect(result).toBe(false); | ||
}); | ||
|
||
test('should return false for a domain with an error during MX lookup', async () => { | ||
mockedDns.resolveMx.mockImplementation((domain, callback) => { | ||
callback(new Error('Domain not found'), []); | ||
}); | ||
|
||
const result = await isValidEmail('[email protected]'); | ||
expect(result).toBe(false); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import dns from 'dns'; | ||
import * as EmailValidator from 'email-validator'; | ||
|
||
export const isValidEmail = async (email: string): Promise<boolean> => { | ||
if (!EmailValidator.validate(email)) { | ||
console.log("Invalid email format"); | ||
return false; | ||
} | ||
|
||
try { | ||
const isValidDomain = await validateDomainMX(email); | ||
return isValidDomain; | ||
} catch (error) { | ||
console.error("Error during MX record validation:", error); | ||
return false; | ||
} | ||
}; | ||
|
||
const validateDomainMX = (email: string): Promise<boolean> => { | ||
const domain = email.split('@')[1]; | ||
return new Promise((resolve) => { | ||
dns.resolveMx(domain, (err, addresses) => { | ||
if (err || addresses.length === 0) { | ||
console.log(`Invalid domain or no MX records for: ${domain}`); | ||
resolve(false); | ||
} else { | ||
console.log(`Valid MX records found for ${domain}:`, addresses); | ||
resolve(true); | ||
} | ||
}); | ||
}); | ||
}; | ||
|