email-guard
v1.0.0
Published
Email validation with syntax check, MX lookup and typo suggestions. No external dependencies.
Maintainers
Readme
email-guard
Email validation with syntax check, MX lookup and typo suggestions. Zero external dependencies.
Features
- ✅ Syntax validation – checks for valid email format
- 🔍 MX record lookup – verifies the domain can receive emails
- 💡 Typo suggestions – detects common misspellings (
gmial.com→gmail.com) - 🌍 Extended domain list – includes CZ, SK, DE and other European domains
- 0️⃣ Zero dependencies – uses Node.js built-in
dnsmodule - 🔷 TypeScript first – full type definitions included
Install
npm install email-guardUsage
import { validate } from 'email-guard'
const result = await validate('[email protected]')
// {
// valid: false,
// reason: 'mx',
// suggestion: '[email protected]',
// validators: {
// regex: { valid: true },
// typo: { valid: true, suggestion: '[email protected]' },
// mx: { valid: false, reason: 'MX record not found.' }
// }
// }Options
const result = await validate('[email protected]', {
validateRegex: true, // default: true
validateTypo: true, // default: true
validateMx: true, // default: true
dnsServers: ['8.8.8.8', '1.1.1.1'], // default
})Result
import { SyntaxError, TypoError, MxError } from 'email-guard'
interface EmailGuardResult {
valid: boolean
reason?: 'regex' | 'typo' | 'mx' // which validator failed
suggestion?: string // suggested correction
validators: {
regex: { valid: boolean; reason?: SyntaxError }
typo: { valid: boolean; reason?: TypoError; suggestion?: string }
mx: { valid: boolean; reason?: MxError }
}
}Error codes
enum SyntaxError {
Empty = 'EMAIL_EMPTY',
MissingAt = 'EMAIL_MISSING_AT',
MultipleAt = 'EMAIL_MULTIPLE_AT',
MissingLocal = 'EMAIL_MISSING_LOCAL',
MissingDomain= 'EMAIL_MISSING_DOMAIN',
MissingTld = 'EMAIL_MISSING_TLD',
InvalidChars = 'EMAIL_INVALID_CHARS',
}
enum TypoError {
TypoDetected = 'TYPO_DETECTED',
}
enum MxError {
NotFound = 'MX_NOT_FOUND',
}Examples
// Valid email
await validate('[email protected]')
// { valid: true, validators: { regex: { valid: true }, typo: { valid: true }, mx: { valid: true } } }
// Typo in domain
await validate('[email protected]')
// { valid: false, reason: 'mx', suggestion: '[email protected]', ... }
// TLD typo
await validate('[email protected]')
// { valid: false, reason: 'mx', suggestion: '[email protected]', ... }
// Invalid syntax
await validate('notanemail')
// { valid: false, reason: 'regex', validators: { regex: { valid: false, reason: 'EMAIL_MISSING_AT' } } }
// Skip MX check
await validate('[email protected]', { validateMx: false })
// { valid: true, ... }
// Switch on error codes
import { validate, SyntaxError } from 'email-guard'
const result = await validate(email)
if (result.reason === 'regex') {
switch (result.validators.regex.reason) {
case SyntaxError.Empty: return 'Please enter an email.'
case SyntaxError.MissingAt: return 'Email must contain @.'
case SyntaxError.MissingTld: return 'Domain is missing.'
}
}Supported domains for typo detection
Global: gmail.com, yahoo.com, hotmail.com, outlook.com, icloud.com, protonmail.com, proton.me and more.
European: seznam.cz, email.cz, centrum.cz (CZ), zoznam.sk, azet.sk, atlas.sk (SK), web.de, gmx.de (DE/AT).
Want to add more domains? Open a PR.
Node.js requirement
Node.js >= 18.0.0
License
MIT
