Skip to content

parseEmail

Parses a value into a valid email address. This parser wraps the validateEmail semantic validator and returns the original string when the email is valid. No normalization is performed.

The parser never throws and never mutates input. It returns a structured ParseResult<string> describing success or failure.

Signature

function parseEmail(
    value: unknown,
    field?: string,
    strict?: boolean
): ParseResult<string>

Behavior

  • Accepted inputs:
  • Strings that pass validateEmail
  • In strict mode:
    • No whitespace.
    • No quoted local parts.
    • No consecutive dots.
    • Local part and domain must follow RFC‑style constraints.
  • In loose mode:
    • Quoted local parts allowed.
    • Escaped characters allowed.
  • Rejected inputs:
  • Non-string values.
  • Empty or whitespace-only strings.
  • Strings without an @.
  • Invalid local parts.
  • Invalid domain labels.
  • Invalid TLDs.
  • Any string that validateEmail rejects.
  • These produce: "Value must be a valid email address"

Returns

A ParseResult<string> object:

  • ok: true And the original email string when parsing succeeds.
  • ok: false, value: null And a list of issues when parsing fails.

Examples

parseEmail("user@example.com")
// { ok: true, value: "user@example.com", issues: [] }

parseEmail("user..dot@example.com")
// { ok: false, value: null, issues: ["Value must be a valid email address"] }

parseEmail("\"quoted\"@example.com", "email", false)
// { ok: true, value: "\"quoted\"@example.com", issues: [] }

Notes

  • This parser intentionally returns the original string, not the normalized one.
  • For trimming or canonicalization, apply a string normalizer before parsing.