1.1.0 • Published 7 months ago

email-validate-domain v1.1.0

Weekly downloads
-
License
MIT
Repository
-
Last release
7 months ago

Email Validator with Temporary Domain Check

A robust Node.js package to validate email addresses and identify if they belong to a list of temporary (disposable) domains fetched dynamically from a reliable source.

Features

  • ✔️ Validate email format.
  • ✔️ Check if the email belongs to a temporary domain.
  • ✔️ Fetch domain list dynamically from GitHub.
  • ✔️ Handles edge cases (e.g., null, empty input).
  • ✔️ Designed for performance and scalability.

Installation

Install the package using npm:

npm install email-validate-domain

Usage

Basic Example

const { validateEmail } = require('email-validate-domain');

(async () => {
  const result = await validateEmail("user@example.com");
  console.log(result);
})();

Output

For a valid email:

{
  "isValid": true,
  "problems": []
}

For an email from a temporary domain:

{
  "isValid": false,
  "problems": ["Email domain is a temporary domain."]
}

Advanced Usage

Validate Multiple Emails

const { validateEmail } = require('email-validate-domain');

const emails = [
  "user@example.com",
  "user@mailinator.com",
  "invalid-email"
];

(async () => {
  for (const email of emails) {
    const result = await validateEmail(email);
    console.log(`Validation result for ${email}:`, result);
  }
})();

Output Example

Validation result for user@example.com: { isValid: true, problems: [] }
Validation result for user@mailinator.com: { isValid: false, problems: [ 'Email domain is a temporary domain.' ] }
Validation result for invalid-email: { isValid: false, problems: [ 'Invalid email format.' ] }

API Reference

validateEmail(email)

Validates an email address and checks if it belongs to a temporary domain.

Parameters:

  • email (string): The email address to validate.

Returns:

  • Promise<Object>: An object containing:
    • isValid (boolean): Indicates if the email is valid.
    • problems (array): A list of validation issues (e.g., invalid format, temporary domain).

Examples:

const result = await validateEmail("user@mailinator.com");
/*
{
  isValid: false,
  problems: ["Email domain is a temporary domain."]
}
*/

Error Handling

If the domain list API is unavailable or the request fails, the package gracefully falls back to allowing all domains. You can log errors for debugging.

How It Works

Email Validation

Uses the validator package to validate the email format.

Fetch Domains Dynamically

Retrieves a list of disposable domains from the GitHub API: https://raw.githubusercontent.com/disposable/disposable-email-domains/refs/heads/master/domains.json.

Check Domain

Extracts the domain from the email and compares it against the fetched list.

Contributing

We welcome contributions! Please follow these steps:

  1. Fork the repository.
  2. Create a new branch for your feature or fix.
  3. Commit your changes.
  4. Submit a pull request.

Tests

The package includes a comprehensive test suite using Jest. To run the tests:

Install development dependencies:

npm install

Run the tests:

npm test

Changelog

v1.0.0

  • Initial release.
  • Dynamic domain list fetching.
  • Comprehensive email validation.

License

This project is licensed under the MIT License. See the LICENSE file for details.

Acknowledgments

Thanks to the maintainers of the disposable-email-domains repository for the domain list. Built with ❤️ by oalaoui.