Skip to main content

email

Accepts and returns strings that are syntactically valid email addresses. Note: This validates email syntax only. It does not verify that the email address actually exists.

Type Signature

const email: Decoder<string>

Validation Pattern

Uses a comprehensive email regex pattern that validates:
  • Local part: alphanumeric characters, dots, and special characters in quotes
  • Domain part: IP addresses in brackets or domain names with TLDs
Regex pattern:
/^(([^<>()[\]\\.,;:\s@"]+(\.[^<>()[\]\\.,;:\s@"]+)*)|".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/
Source: emailregex.com

Valid Inputs

import { email } from 'decoders';

email.verify('[email protected]');        // ✓ '[email protected]'
email.verify('[email protected]'); // ✓ '[email protected]'
email.verify('[email protected]');     // ✓ '[email protected]'
email.verify('"spaces ok"@example.com'); // ✓ '"spaces ok"@example.com'
email.verify('user@[192.168.1.1]');      // ✓ 'user@[192.168.1.1]'

Invalid Inputs

email.verify('notanemail');          // ✗ Must be email
email.verify('missing@domain');      // ✗ Must be email
email.verify('@example.com');        // ✗ Must be email
email.verify('user@');               // ✗ Must be email
email.verify('user @example.com');   // ✗ Must be email (space before @)
email.verify(123);                   // ✗ Must be string

Error Message

When validation fails: "Must be email"

Implementation

export const email: Decoder<string> = regex(
  /^(([^<>()[\]\\.,;:\s@"]+(\.[^<>()[\]\\.,;:\s@"]+)*)|".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/,
  'Must be email',
);
Source: src/strings.ts:63-67
  • regex - Create custom string pattern validators
  • url - Validate URL strings

Build docs developers (and LLMs) love