Skip to content

Validation Assertions

Validation assertions check common data formats like emails, URLs, UUIDs, and IP addresses.

Assert that a value is a valid email address.

use Cline\Assert\Assertions\Assertion;
Assertion::email('user@example.com');
Assertion::email($emailAddress, 'Invalid email address');

Assert that a value is a valid URL.

Assertion::url('https://example.com');
Assertion::url($websiteUrl, 'Invalid URL format');

Assert that a value is a valid UUID.

Assertion::uuid('550e8400-e29b-41d4-a716-446655440000');
Assertion::uuid($id, 'Invalid UUID format');

Assert that a value is a valid IPv4 or IPv6 address.

Assertion::ip('192.168.1.1');
Assertion::ip('2001:0db8:85a3:0000:0000:8a2e:0370:7334');

Assert that a value is a valid IPv4 address.

Assertion::ipv4('192.168.1.1');

Assert that a value is a valid IPv6 address.

Assertion::ipv6('2001:0db8:85a3:0000:0000:8a2e:0370:7334');

Assert that a value is a valid E.164 phone number format.

Assertion::e164('+14155552671');
Assertion::e164($phoneNumber, 'Invalid phone number format');

Assert that a value is valid base64 encoded data.

Assertion::base64('SGVsbG8gV29ybGQ=');

Assert that a value is a valid JSON string.

Assertion::isJsonString('{"key":"value"}');
Assertion::isJsonString($jsonData, 'Invalid JSON format');

Assert that a date string matches a specific format.

Assertion::date('2024-01-15', 'Y-m-d');
Assertion::date($dateString, 'Y-m-d H:i:s', 'Invalid date format');
use Cline\Assert\Assert;
Assert::that($email)
->string()
->notEmpty()
->email('Please provide a valid email address');
Assert::that($website)
->string()
->url('Invalid website URL')
->startsWith('https://', 'Website must use HTTPS');
Assert::lazy()
->that($data['email'], 'email')
->notEmpty('Email is required')
->email('Invalid email address')
->that($data['phone'] ?? null, 'phone')
->nullOr()->e164('Invalid phone number format')
->verifyNow();
Assert::that($redirectUrl)
->url('Invalid redirect URL')
->regex('/^https:\/\//', 'Only HTTPS URLs are allowed');
Assert::that($userId)
->notEmpty('User ID is required')
->uuid('Invalid user ID format');
Assert::that($startDate)
->date('Y-m-d', 'Invalid start date format');
Assert::that($endDate)
->date('Y-m-d', 'Invalid end date format');
Assert::that($email)
->email('Invalid email format')
->endsWith('@company.com', 'Must use company email');
Assert::thatAll($recipients)
->email('All recipients must have valid email addresses');
Assert::that($url)
->url('Invalid URL')
->startsWith('https://', 'Only HTTPS URLs allowed');
Assert::that($callbackUrl)
->url()
->contains('example.com', 'Callback must be on example.com');
Assert::that($internalIp)
->ipv4()
->satisfy(function($ip) {
return filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE) === false;
}, 'Must be private IP address');
Assert::that($email)->email();
$user = User::where('email', $email)->first();
// Use built-in validator
Assertion::email($email);
// Instead of generic regex
Assertion::regex($email, '/^[^@]+@[^@]+\.[^@]+$/');