@valentech/validate-email
v1.0.4
Published
Import the module and call `validateEmail` with an email string. The function returns a promise that resolves to a `ValidateEmailResult` object, providing both overall validity and detailed diagnostic information.
Downloads
1
Readme
Usage
Import the module and call validateEmail with an email string. The function returns a promise that resolves to a ValidateEmailResult object, providing both overall validity and detailed diagnostic information.
import validateEmail from "@valentech/validate-email";
async function runValidation() {
const email = "[email protected]";
const result = await validateEmail(email);
if (result.valid) {
console.log("Email is valid!", result.details);
} else {
console.error("Invalid email:", result.reason);
if (result.suggestion) {
console.info("Did you mean:", result.suggestion);
}
}
}
runValidation();API
validateEmail(email: string): Promise
Returns: A promise that resolves to an object with the following structure:
| Property | Type | Description |
| ---------- | -------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| valid | boolean | Indicates if the email passed all checks. |
| reason | string | null | Error identifier when validation fails. Possible values include:• invalid_syntax• disposable_domain• dns_timeout• invalid_domain• dns_error• unknown_error |
| suggestion | string | null | Provides a suggested correction if the domain appears mistyped. |
| details | object | Detailed result of various validation checks: |
details structure
| Property | Type | Description | | ------------- | --------------- | --------------------------------------------------------------- | | syntax | boolean | Result of the basic email syntax check. | | disposable | boolean | Whether the email belongs to a disposable domain. | | mxRecords | boolean | Whether MX DNS records were found. | | aRecords | boolean | Whether A/AAAA DNS records were found. | | smtpHandshake | boolean | null | Outcome of the SMTP handshake test (if performed). | | smtpPort | number | null | The port on which the SMTP handshake succeeded (if applicable). |
Features
Syntax Verification
Uses a regular expression along with the validator package to ensure the email is correctly formatted.
Disposable Domain Filtering
Compares the email's domain against a list of known disposable domains.
DNS Verification
Uses Node's DNS promises to check for MX and A/AAAA records. If no MX records are found but A/AAAA records are available, an SMTP handshake is attempted.
SMTP Handshake
Performs multi-port SMTP handshakes (plaintext on ports 25, 587, 2525 and TLS on port 465) to check if the domain can accept emails.
Typo Correction
Leverages a SymSpell-based lookup to suggest corrections for common email provider typos.
Caching
Utilizes LRU caches for email validations and DNS lookups to optimize performance.
