Represents an email address with validation.

Email is immutable and automatically normalized (trimmed and lowercased).

const result = Email.create('user@example.com');
if (result.isSuccess) {
const email = result.value;
console.log(email.value); // "user@example.com"
console.log(email.domain); // "example.com"
console.log(email.localPart); // "user"
}

Hierarchy (View Summary)

Accessors

  • get domain(): string

    Gets the domain part of the email address.

    Returns string

    const email = Email.create('user@example.com').unwrap();
    console.log(email.domain); // "example.com"
  • get localPart(): string

    Gets the local part (before @) of the email address.

    Returns string

    const email = Email.create('user@example.com').unwrap();
    console.log(email.localPart); // "user"

Methods

  • Compares this Value Object with another for equality. Value Objects are equal if they are of the same type and all equality components match.

    Parameters

    • other: ValueObject

      The other Value Object to compare with

    Returns boolean

    true if the Value Objects are equal, false otherwise

  • Creates an Email instance with validation.

    Parameters

    • value: string

      The email address string

    Returns Result<Email, DomainError>

    Success with Email or Failure with DomainError

    const result = Email.create('user@example.com');
    if (result.isSuccess) {
    console.log(result.value.value); // "user@example.com"
    }

    const invalid = Email.create('not-an-email');
    if (invalid.isFailure) {
    console.log(invalid.error.code); // "INVALID_EMAIL_FORMAT"
    }
  • Checks if this email belongs to a specific domain.

    Parameters

    • domain: string

      The domain to check

    Returns boolean

    true if the email belongs to the domain

    const email = Email.create('user@example.com').unwrap();
    console.log(email.belongsToDomain('example.com')); // true
    console.log(email.belongsToDomain('other.com')); // false

Properties

value: string