npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

saksh-email-validator

v2.1.0

Published

A comprehensive email validation module with syntax, domain, blacklist, and SMTP checks.

Downloads

124

Readme

Overview

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.

Features

Syntax Validation:

• Validates the syntax of an email address using a regular expression. • Provides suggestions for correcting common syntax errors.

Domain Checks:

• Checks if the domain of an email address has MX (Mail Exchange) records. • Skips SMTP validation for blacklisted domains.

Blacklist Checks:

• Checks if the domain of an email address is blacklisted. • Supports custom blacklisted domains.

Disposable Email Detection:

• Detects if an email address is from a disposable email provider. • Supports custom disposable domains.

Role-Based Email Detection:

• Detects if an email address is role-based (e.g., [email protected]). • Supports custom role-based emails.

SMTP Server Validation:

• Validates the SMTP server for the email domain. • Supports custom SMTP ports, connection timeout, and retry settings.

Custom Validation Rules:

Allows adding custom validation rules to extend the validation logic.

International Email Normalization:

Normalizes international email addresses to ASCII format using the punycode module.

Example Usage

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 = [
    '[email protected]',
    '[email protected]',
    '[email protected]',
    '[email protected]',
    '[email protected]',
    '[email protected]',
    '[email protected]'
];

// Configuration object
const config = {
    validateMx: true,
    validateSmtp: true,
    blacklistedDomains: [],
    disposableDomains: [],
    roleBasedEmails: [],
    wellKnownDomains: ['sakshamapp.com'],
    customValidationRules: [customRule1, customRule2]
};



console.log(await sakshValidateEmail('[email protected]', 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()

Detailed Function Descriptions

  • sakshValidateEmailSyntax(email)

    Description: Validates the syntax of an email address using a regular expression.

    Parameters: email (string) - The email address to validate.

    Returns: boolean - Returns true 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 to true 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 - Returns true 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 - Returns true 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 - Returns true 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.

Conclusion

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.

Support

susheel2339 at gmail.com