martijnvdb/type-vault

Validated types for PHP: Email, UUID, URL, and more

Maintainers

Package info

github.com/martijnvdb87/type-vault-php

pkg:composer/martijnvdb/type-vault

Transparency log

Statistics

Installs: 3

Dependents: 0

Suggesters: 0

Stars: 1

Open Issues: 0

0.2.0 2026-07-04 12:06 UTC

This package is auto-updated.

Last update: 2026-07-04 12:32:45 UTC


README

Type Vault is a PHP utility package that provides robust and validated types for common structured values like UUIDs, URLs, ISO date-times, hex colors, and more. It helps enforce correctness at runtime while maintaining strong typing.

โœจ Features

  • โœ… Runtime and compile time validation
  • ๐Ÿงช Built-in helpers for immutability
  • ๐Ÿ” Types are \Stringable and \JsonSerializable

๐Ÿง‘โ€๐Ÿ’ป How to use

๐Ÿ“ฅ Basic Usage

Create a validated Email instance and access or update its value:

use Martijnvdb\TypeVault\Types\Email;

$email = new Email("user@example.com");
echo $email->value; // 'user@example.com'

// Update the value with another valid email
$email->value = "foo@bar.com";
echo $email->value; // 'foo@bar.com'

๐Ÿ”’ Immutable Variant

Use ::immutable() or pass new TypeOptionsDTO(immutable: true) to prevent value changes after initialization:

use Martijnvdb\TypeVault\DTOs\TypeOptionsDTO;
use Martijnvdb\TypeVault\Errors\ImmutableValueError;
use Martijnvdb\TypeVault\Errors\TypeVaultValidationError;
use Martijnvdb\TypeVault\Types\Email;

$immutable = Email::immutable("user@example.com");
// Or:
$immutable = new Email("user@example.com", new TypeOptionsDTO(immutable: true));

echo $immutable->value; // 'user@example.com'

try {
	$immutable->value = "another@example.com"; // โŒ
} catch (ImmutableValueError $error) {
	// Specific immutable mutation error
} catch (TypeVaultValidationError $error) {
	// Fallback for any Type Vault validation error
}

๐Ÿšจ Error Types

All validation-related errors extend TypeVaultValidationError.

  • InvalidValueError - Thrown when a value fails validation.
  • ImmutableValueError - Thrown when mutating an immutable type value.
  • InvalidPropertyError - Thrown when updating a non-existing DTO property (e.g. in copyWith).
  • InvalidTypeError - Thrown for mismatched types in utilities like Collection.

๐Ÿงฐ Supported Types

Type Vault offers a rich set of validated types. Each type enforces strict formatting and value constraints at runtime, and all values are stored in a normalized form to ensure consistency and predictability across your application.

๐ŸŽจ Color Types

For working with color values in various formats:

  • ColorHex โ€“ Hexadecimal color code (e.g. '#ffcc00ff', '#ffcc00' or '#fc0')
  • ColorRgb โ€“ RGB color object (e.g. 'rgb(255 128 0 / 100%)', 'rgb(255, 128, 0)' or 'rgb(255 128 0 / 1)')
  • ColorHsl โ€“ HSL color object (e.g. 'hsl(360 0 0 / 1)' or 'hsl(360deg 0% 0% / 100%)')
  • ColorOklch โ€“ OKLCH color object (e.g. 'oklch(70 0.4 120deg / 25%)', 'oklch(70% 100% 120deg / 0.25)' or 'oklch(0.7 0.4 120 / 25%)')

๐Ÿ•’ Temporal Types

For representing and validating time-related values:

  • DateOnly โ€“ ISO date string without time (e.g. '2023-01-02' or '2023-1-2')
  • DateTime โ€“ ISO UTC date-time string (e.g. '2023-01-02T01:23:45.123Z' or '2023-01-02T01:23:45Z')
  • TimeOnly โ€“ ISO time string without date (e.g. '01:23:45.123' or '01:23:45')
  • Duration โ€“ ISO 8601 duration string (e.g. 'PT1H30M')
  • Month โ€“ Valid month name or number (e.g. 'january' or december)
  • Weekday โ€“ Valid weekday name (e.g. 'monday')
  • Year โ€“ Valid four-digit year (e.g. '2025')

๐ŸŒ Communication Types

For validating contact and identity formats:

  • Email โ€“ RFC-compliant email address (e.g. 'user@example.com')
  • PhoneNumber โ€“ E.164 formatted phone number (e.g. '+31612345678')

โœ๏ธ Text & Numeric Types

For structured text, numbers, and identifiers:

  • Text โ€“ A valid text string (e.g. 'foo', 'Lorem ipsum dolor sit amet' or '')
  • Integer โ€“ Whole number (e.g. 42)
  • FloatingPoint โ€“ Decimal number (e.g. 3.14)
  • Percentage โ€“ Decimal number between 0 and 1 (e.g. 0, 0.5 or 1)
  • Url โ€“ Valid absolute URL (e.g. 'https://example.com')
  • Uuid โ€“ RFC 4122 UUID string (e.g. '550e8400-e29b-41d4-a716-446655440000')