webmozart/assert
Lightweight PHP assertion library for validating method input/output. Provides fast, readable checks via Webmozart\Assert\Assert with consistent error-message placeholders, throwing InvalidArgumentException on failure. Ideal for safer, less repetitive validation code.
User, Order) where strict type/value constraints are required. Example: Validating User::create() inputs before persistence.ValidateRequest) for low-level checks (e.g., Assert::uuid($request->id) in route middleware).assert(): More expressive than PHP’s native assert() with custom error messages and consistent placeholder syntax (%s, %2$s).composer require webmozart/assert). No configuration or service provider required.Validator or Laravel’s Validator as a pre-validation layer (e.g., reject malformed data early with Assert::email() before Symfony’s rules run).if (!Assert::integer($id)) return false;).Assert::classExists($class) in a singleton).%s placeholders).ValidationException format.Assert::uuid() rejecting valid UUIDs due to strict regex (test thoroughly).Assert::isCallable() failing on closure strings (e.g., 'function() { return 1; }').symfony/polyfill or other assertion libraries (e.g., beberlei/assert).User::fromArray() validate inputs, or delegate to a UserValidator?InvalidArgumentException (current behavior) vs. log and return null (for graceful degradation).HttpResponse in middleware)?Assert::email() with edge cases) or treated as infrastructure?Assert::inArray() in a loop) to ensure <1ms overhead.Assert::validPaymentMethod()) be needed? Extend the Assert class or create a wrapper.if (!is_int($id)) throw new \InvalidArgumentException(...) with Assert::integer($id, 'ID must be an integer.').Assert::uuid($request->resourceId) in ApiResourceMiddleware).FormRequest::validate() for pre-validation (e.g., reject null values before Symfony’s rules).app()->bind('validator', fn() => new CustomAssertions())).Assert::notEmpty($event->payload) in Kernel::handle()).Assert into services (e.g., public function __construct(private Assert $assert)).Assert::isList($data) in PHPUnit data sets).Assert::expectsException() pattern).User, Order).class User {
public function __construct(private string $email) {
Assert::email($this->email);
}
}
Assert (e.g., Assert::uuid($request->id)).FormRequest::rules() (e.g., reject null before Symfony’s rules).Assertion::validate($data, $rules)) to standardize usage.webmozart/assert:^1.0 (but expect deprecation warnings).Assert for low-level checks (e.g., Assert::notNull($value)) before Symfony’s constraints.Assert for non-standard rules (e.g., Assert::uuid() vs. Rule::uuid()).Assert before API Platform’s hydration.Assert::fileExists().| Step | Action | Dependencies |
|---|---|---|
| 1. Installation | composer require webmozart/assert |
None |
| 2. Pilot | Add assertions to 1–2 entities/middleware | None |
| 3. Testing | Write unit tests for assertions (edge cases, custom messages) | PHPUnit |
| 4. Documentation | Update API docs with assertion examples | Swagger/OpenAPI (if used) |
| 5. Middleware | Replace manual checks in middleware with Assert |
Laravel middleware pipeline |
| 6. Form Requests | Integrate with FormRequest::validate() |
Laravel validation pipeline |
| 7. Custom Wrapper | Create a facade/service for reusable assertions | Laravel service container |
| 8. Performance | Benchmark critical paths (e.g., loop assertions) | Blackfire/New Relic |
if (!is_int($x)) throw new ... code.Assert::validCouponCode()).VALIDATION_RULES.md for custom assertions.Assert (e.g., via static analysis).Assert::email() fails with Got: "user@example").How can I help you explore Laravel packages today?