Sigra.Email (Sigra v0.2.3)

Copy Markdown View Source

Email normalization and format validation.

Provides consistent email handling for authentication operations. All emails are normalized before storage or comparison to prevent duplicate accounts and ensure reliable matching.

Normalization Steps

  1. Trim leading/trailing whitespace
  2. Downcase the entire email
  3. Apply Unicode NFKC normalization

NFKC is applied to emails per Unicode best practices. It is intentionally NOT applied to passwords (per NIST SP 800-63B).

Format Validation

Uses a permissive regex (~r/^[^\s]+@[^\s]+$/) to avoid rejecting valid addresses. Maximum length is 160 characters.

Gmail dot-stripping and plus-stripping are NOT applied per design decision D-41 -- users should be able to use plus-addressing.

Summary

Functions

Normalizes an email address by trimming, downcasing, and applying NFKC.

Validates the format of an email address.

Functions

normalize(email)

(since 0.2.0)
@spec normalize(String.t()) :: String.t()

Normalizes an email address by trimming, downcasing, and applying NFKC.

Examples

iex> Sigra.Email.normalize("  FOO@Bar.COM  ")
"foo@bar.com"

iex> Sigra.Email.normalize("user+tag@example.com")
"user+tag@example.com"

validate_format(email)

(since 0.2.0)
@spec validate_format(String.t()) :: :ok | {:error, String.t()}

Validates the format of an email address.

Returns :ok for valid emails or {:error, reason} for invalid ones.

Validation rules:

  • Must contain exactly one @ with non-empty local and domain parts
  • Must not contain whitespace
  • Must not exceed 160 characters

Examples

iex> Sigra.Email.validate_format("user@example.com")
:ok

iex> Sigra.Email.validate_format("no-at-sign")
{:error, "must contain exactly one @ sign"}