Validation Assertions
Validation assertions check common data formats like emails, URLs, UUIDs, and IP addresses.
Available Assertions
Section titled “Available Assertions”email()
Section titled “email()”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');uuid()
Section titled “uuid()”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');ipv4()
Section titled “ipv4()”Assert that a value is a valid IPv4 address.
Assertion::ipv4('192.168.1.1');ipv6()
Section titled “ipv6()”Assert that a value is a valid IPv6 address.
Assertion::ipv6('2001:0db8:85a3:0000:0000:8a2e:0370:7334');e164()
Section titled “e164()”Assert that a value is a valid E.164 phone number format.
Assertion::e164('+14155552671');Assertion::e164($phoneNumber, 'Invalid phone number format');base64()
Section titled “base64()”Assert that a value is valid base64 encoded data.
Assertion::base64('SGVsbG8gV29ybGQ=');isJsonString()
Section titled “isJsonString()”Assert that a value is a valid JSON string.
Assertion::isJsonString('{"key":"value"}');Assertion::isJsonString($jsonData, 'Invalid JSON format');date()
Section titled “date()”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');Chaining Validation Assertions
Section titled “Chaining Validation Assertions”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');Common Patterns
Section titled “Common Patterns”User Registration Validation
Section titled “User Registration Validation”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();URL Validation with Protocol
Section titled “URL Validation with Protocol”Assert::that($redirectUrl) ->url('Invalid redirect URL') ->regex('/^https:\/\//', 'Only HTTPS URLs are allowed');UUID Primary Key Validation
Section titled “UUID Primary Key Validation”Assert::that($userId) ->notEmpty('User ID is required') ->uuid('Invalid user ID format');Date Range Validation
Section titled “Date Range Validation”Assert::that($startDate) ->date('Y-m-d', 'Invalid start date format');
Assert::that($endDate) ->date('Y-m-d', 'Invalid end date format');Email Validation
Section titled “Email Validation”Email with Domain Check
Section titled “Email with Domain Check”Assert::that($email) ->email('Invalid email format') ->endsWith('@company.com', 'Must use company email');Multiple Email Validation
Section titled “Multiple Email Validation”Assert::thatAll($recipients) ->email('All recipients must have valid email addresses');URL Validation
Section titled “URL Validation”HTTPS Only
Section titled “HTTPS Only”Assert::that($url) ->url('Invalid URL') ->startsWith('https://', 'Only HTTPS URLs allowed');Domain Restriction
Section titled “Domain Restriction”Assert::that($callbackUrl) ->url() ->contains('example.com', 'Callback must be on example.com');IP Address Validation
Section titled “IP Address Validation”Private IP Range
Section titled “Private IP Range”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');Best Practices
Section titled “Best Practices”Validate Format Before Processing
Section titled “Validate Format Before Processing”Assert::that($email)->email();$user = User::where('email', $email)->first();Use Specific Validators
Section titled “Use Specific Validators”// Use built-in validatorAssertion::email($email);
// Instead of generic regexAssertion::regex($email, '/^[^@]+@[^@]+\.[^@]+$/');Next Steps
Section titled “Next Steps”- String Assertions - String format validation
- Custom Assertions - Create custom validators
- Lazy Assertions - Validate multiple fields