Represents a UUID (Universally Unique Identifier) with validation.

UUID is immutable and validates the UUID format (version 4). Provides convenient generation and validation methods.

const uuid = UUID.generate();
console.log(uuid.value); // "550e8400-e29b-41d4-a716-446655440000"

const result = UUID.create('550e8400-e29b-41d4-a716-446655440000');
if (result.isSuccess) {
console.log(result.value.value);
}

Hierarchy (View Summary)

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 a UUID instance with validation.

    Parameters

    • value: string

      The UUID string

    Returns Result<UUID, DomainError>

    Success with UUID or Failure with DomainError

    const result = UUID.create('550e8400-e29b-41d4-a716-446655440000');
    if (result.isSuccess) {
    console.log(result.value.value);
    }

    const invalid = UUID.create('not-a-uuid');
    if (invalid.isFailure) {
    console.log(invalid.error.code); // "INVALID_UUID_FORMAT"
    }
  • Generates a new random UUID (version 4).

    Returns UUID

    A new UUID instance

    const uuid = UUID.generate();
    console.log(uuid.value); // "550e8400-e29b-41d4-a716-446655440000"
  • Checks if a string is a valid UUID.

    Parameters

    • value: string

      The string to validate

    Returns boolean

    true if the string is a valid UUID

    console.log(UUID.isValid('550e8400-e29b-41d4-a716-446655440000')); // true
    console.log(UUID.isValid('not-a-uuid')); // false
  • Gets the UUID version.

    Returns number

    The version number (1-5)

    const uuid = UUID.generate();
    console.log(uuid.version()); // 4
  • Gets the UUID variant.

    Returns string

    The variant identifier

    const uuid = UUID.generate();
    console.log(uuid.variant());
  • Converts the UUID to uppercase format.

    Returns string

    The UUID in uppercase

    const uuid = UUID.create('550e8400-e29b-41d4-a716-446655440000').unwrap();
    console.log(uuid.toUpperCase()); // "550E8400-E29B-41D4-A716-446655440000"
  • Removes hyphens from the UUID.

    Returns string

    The UUID without hyphens

    const uuid = UUID.create('550e8400-e29b-41d4-a716-446655440000').unwrap();
    console.log(uuid.toCompact()); // "550e8400e29b41d4a716446655440000"

Properties

value: string