This email validation module provides comprehensive validation for email addresses. It includes syntax validation, domain checks, blacklist checks, disposable email detection, role-based email detection, and SMTP server validation. Additionally, it supports custom validation rules and provides suggestions for correcting common email syntax errors.
• Validates the syntax of an email address using a regular expression. • Provides suggestions for correcting common syntax errors.
• Checks if the domain of an email address has MX (Mail Exchange) records. • Skips SMTP validation for blacklisted domains.
• Checks if the domain of an email address is blacklisted. • Supports custom blacklisted domains.
• Detects if an email address is from a disposable email provider. • Supports custom disposable domains.
• Detects if an email address is role-based (e.g., admin@example.com). • Supports custom role-based emails.
• Validates the SMTP server for the email domain. • Supports custom SMTP ports, connection timeout, and retry settings.
Allows adding custom validation rules to extend the validation logic.
Normalizes international email addresses to ASCII format using the punycode module.
Here's an example of how to use the email validation module:
const {
sakshValidateEmail,
sakshValidateEmailBatch, sakshCache, sakshAddCustomValidationRule
} = require('saksh-email-validator');
const {
sakshValidateEmail,
sakshValidateEmailBatch, sakshCache
} = require('./index');
// Custom validation rule 1: Disallow emails containing "test"
function customRule1(email) {
return {
valid: !email.includes('test'),
reason: 'Emails containing "test" are not allowed.',
priority: 1 // Higher priority
};
}
// Custom validation rule 2: Disallow emails from "example.com"
function customRule2(email) {
const domain = email.split('@')[1];
return {
valid: domain !== 'example.com',
reason: 'Emails from "example.com" are not allowed.',
priority: 2 // Lower priority
};
}
// Example email addresses to test
const emails = [
'valid.email@allowed.com',
'test.email@allowed.com',
'valid.email@example.com',
'test.email@example.com',
'susheelhbti@gmail.com',
'saksh@sakshamapp.com',
'susheel@aistore2030.com'
];
// Configuration object
const config = {
validateMx: true,
validateSmtp: true,
blacklistedDomains: [],
disposableDomains: [],
roleBasedEmails: [],
wellKnownDomains: ['sakshamapp.com'],
customValidationRules: [customRule1, customRule2]
};
console.log(await sakshValidateEmail('test@example.com', config));
// Test the email validation function
async function testEmailValidation() {
for (const email of emails) {
const result = await sakshValidateEmail(email, config);
console.log(`Validation result for ${email}:`, result);
}
}
testEmailValidation();
function clearCache() {
sakshCache.flushAll();
console.log('Cache cleared.');
}
// clearCache()
-
sakshValidateEmailSyntax(email)
Description: Validates the syntax of an email address using a regular expression.
Parameters:
email
(string) - The email address to validate.Returns:
boolean
- Returnstrue
if the email syntax is valid,false
otherwise. -
sakshEmailSyntaxSuggestions(email)
Description: Provides suggestions for correcting common email syntax errors.
Parameters:
email
(string) - The email address to suggest corrections for.Returns:
string[]
- Returns an array of suggested email formats. -
sakshCheckDomainMXRecords(email)
Description: Checks if the domain of an email address has MX records.
Parameters:
email
(string) - The email address to check.Returns:
Promise
- Returns a promise that resolves totrue
if the domain has MX records,false
otherwise. -
sakshIsDomainBlacklisted(email, customBlacklistedDomains = [])
Description: Checks if the domain of an email address is blacklisted.
Parameters:
email
(string) - The email address to check.
customBlacklistedDomains
(string[]) - Custom blacklisted domains.Returns:
boolean
- Returnstrue
if the domain is blacklisted,false
otherwise. -
sakshIsDisposableEmail(email, customDisposableDomains = [])
Description: Checks if the email address is disposable.
Parameters:
email
(string) - The email address to check.
customDisposableDomains
(string[]) - Custom disposable domains.Returns:
boolean
- Returnstrue
if the email is disposable,false
otherwise. -
sakshIsRoleBasedEmail(email, customRoleBasedEmails = [])
Description: Checks if the email address is role-based.
Parameters:
email
(string) - The email address to check.
customRoleBasedEmails
(string[]) - Custom role-based emails.Returns:
boolean
- Returnstrue
if the email is role-based,false
otherwise. -
sakshCheckSmtpServer(email, config = {})
Description: Checks if the SMTP server for the email domain is valid.
Parameters:
email
(string) - The email address to check.
config
(object) - Configuration object for SMTP validation.Returns:
Promise< object >
- Returns a promise that resolves to the validation result. -
sakshAddCustomValidationRule(rule)
Description: Adds a custom validation rule.
Parameters:
rule
(function) - The custom validation rule to add. -
sakshValidateEmail(email, config = {})
Description: Validates an email address with various checks and custom validation rules.
Parameters:
email
(string) - The email address to validate.
config
(object) - Configuration object for validation options.Returns:
Promise< object >
- Returns a promise that resolves to the validation result. -
sakshValidateEmailBatch(emails, config = {})
Description: Validates a batch of email addresses.
Parameters:
emails
(string[]) - The array of email addresses to validate.
config
(object) - Configuration object for validation options.Returns:
Promise< object[] >
- Returns a promise that resolves to an array of validation results. -
sakshNormalizeInternationalEmail(email)
Description: Normalizes an international email address to ASCII format.
Parameters:
email
(string) - The email address to normalize.Returns:
string
- Returns the normalized email address. -
sakshSuggestEmailCorrections(email)
Description: Provides suggestions for correcting common email syntax errors.
Parameters:
email
(string) - The email address to suggest corrections for.Returns:
string[]
- Returns an array of suggestions. -
sakshLevenshtein(a, b)
Description: Calculates the Levenshtein distance between two strings.
Parameters:
a
(string) - The first string.
b
(string) - The second string.Returns:
number
- The Levenshtein distance between the two strings. -
sakshSuggestDomain(email, customDomains = [])
Description: Suggests corrections for the domain part of an email address.
Parameters:
email
(string) - The email address to suggest corrections for.
customDomains
(string[]) - An array of custom domains to include in the suggestions.Returns:
object[]
- Returns an array of suggested domain corrections. -
sakshCorrectEmail(email, customDomains = [])
Description: Corrects the domain part of an email address.
Parameters:
email
(string) - The email address to correct.
customDomains
(string[]) - An array of custom domains to include in the suggestions.Returns:
string
- Returns the corrected email address. -
sakshCache
Description: A cache instance used to store validation results temporarily.
Type:
NodeCache
instance.
This email validation module offers a robust set of features to ensure the validity of email addresses. It is highly configurable and can be extended with custom validation rules to meet specific requirements. The module also provides helpful suggestions for correcting common email errors, making it a comprehensive solution for email validation needs.
susheel2339 at gmail.com