Skip to Content
DocumentationAPIEmail Validator

Email Validator

The Email Validator module provides a robust set of tools for handling email addresses, ensuring they follow standard formats while providing features for privacy and identification.

Overview

Working with email addresses requires balancing strict validation with the flexibility of real-world usage. This module follows common RFC standards while providing specific Indonesian-centric utilities.

Key Features

  • Standard Compliance: Validates emails against standard RFC patterns, including special characters and long TLDs.
  • Privacy First: Built-in masking utilities to hide sensitive information in logs or UI.
  • Security: Optional blocking for known disposable/temporary email providers to prevent spam.
  • Identification: Easily detect if an email belongs to a common provider (Gmail, Yahoo, etc.) or is a potential temporary address.

Installation

npm install @indodev/toolkit

Quick Start

import { validateEmail, maskEmail, getEmailInfo } from '@indodev/toolkit/email-validator'; // Standard validation validateEmail('halo@indodev.id'); // true // Security: Block disposable emails validateEmail('spam@mailinator.com', { blockDisposable: true }); // false // Privacy: Masking for logs/UI maskEmail('choirul@adamm.cloud'); // 'c*****l@adamm.cloud' // Identification: Extract info const info = getEmailInfo('adam@gmail.com'); // info.isCommonProvider => true

API Reference

validateEmail()

Validates if a string is a properly formatted email address.

Type Signature:

function validateEmail(email: string, options?: EmailValidationOptions): boolean;

Parameters:

NameTypeDescription
emailstringThe email address to validate.
optionsEmailValidationOptionsOptional configuration settings.

Options:

PropertyTypeDefaultDescription
blockDisposablebooleanfalseIf true, blocks common disposable email domains.

The validation follows a standard regex that supports most common email patterns including subdomains and special characters allowed by RFC 5322.


maskEmail()

Masks the username portion of an email for privacy protection.

Type Signature:

function maskEmail(email: string, options?: EmailMaskOptions): string;

Parameters:

NameTypeDescription
emailstringThe email address to mask.
optionsEmailMaskOptionsCustom masking configuration.

Options:

PropertyTypeDefaultDescription
visibleStartnumber1Characters to show at the start of the username.
visibleEndnumber1Characters to show at the end of the username.
maskCharstring'*'Character used for masking.

Example:

maskEmail('user@example.com', { visibleStart: 2, maskChar: '#' }); // 'us##r@example.com'

getEmailInfo()

Parses an email address to extract useful metadata.

Type Signature:

function getEmailInfo(email: string): EmailInfo | null;

Returns:

EmailInfo | null - An object containing email details, or null if the email is invalid.

Object Properties:

PropertyTypeDescription
usernamestringThe part before the @ symbol.
domainstringThe domain part after the @ symbol.
isCommonProviderbooleanTrue if it’s a major provider (Gmail, Yahoo, etc).
isDisposablebooleanTrue if it’s a known temporary email domain.

normalizeEmail()

Standardizes an email string for database storage or comparison.

Type Signature:

function normalizeEmail(email: string): string;

Behavior:

  • Trims leading and trailing whitespace.
  • Converts the entire string to lowercase.

Constants

EMAIL_REGEX

The regular expression used internally for validation.

DISPOSABLE_DOMAINS

A list of common disposable email domains that are blocked when blockDisposable is enabled.

Last updated on