- How do I install webmozart/assert in a Laravel project?
- Install it with Composer using composer require webmozart/assert. It is a pure PHP package, so no Laravel service provider or configuration file is required. You can then import Webmozart\Assert\Assert and call assertions directly in your classes or middleware.
- Does webmozart/assert work with Laravel 10 and Laravel 11?
- Webmozart Assert is framework-agnostic and has no Laravel-specific dependencies, so it can be used with modern Laravel versions. Compatibility depends mainly on the PHP version required by your Laravel release and the package's Composer constraints. Check the installed package version against your project's PHP version before adopting it broadly.
- Where should I use Webmozart Assert in a Laravel application?
- Common places are constructors and factory methods for DTOs or domain entities, public method preconditions, and middleware that checks route or request values early. It also works as a lightweight pre-validation layer before Laravel FormRequest or Validator rules run. Avoid putting heavy assertion loops in hot paths unless you have benchmarked the overhead.
- How is webmozart/assert different from Laravel's built-in validator?
- Webmozart Assert is a low-level assertion library that throws Webmozart\Assert\InvalidArgumentException when a value fails a check. Laravel's Validator is designed for form and request validation and returns structured error messages or ValidationException responses. Many teams use Assert for developer-facing invariants and Laravel validation for user-facing request errors.
- What exception does webmozart/assert throw when validation fails?
- Failed assertions throw Webmozart\Assert\InvalidArgumentException. In Laravel, you can catch this exception in middleware or the exception handler and convert it into an HTTP response if needed. This lets you keep domain-level assertions while still returning API-friendly errors.
- Can I use webmozart/assert for request payload validation in Laravel middleware?
- Yes, you can use assertions such as Assert::uuid() or Assert::integer() in middleware to reject malformed route parameters or request data early. It is best suited to low-level checks and invariants, not full replacement of Laravel's validation rules for complex user input. Pair it with Laravel validation when you need field-specific messages and redirects.
- How do I customize error messages with webmozart/assert?
- Pass a custom message as the second argument to an assertion, such as Assert::string($path, 'The path is expected to be a string. Got: %s'). The %s placeholder is consistently replaced with the tested value, and additional placeholders like %2$s are assertion-specific. Keep messages clear and avoid overly nested placeholders in user-facing output.
- Is webmozart/assert safe to use in production compared with PHP assert()?
- Webmozart Assert is an explicit library that throws exceptions when checks fail, so it is not affected by PHP's zend.assertions setting. Native PHP assert() can be disabled in production, which makes it less predictable for enforcing method contracts. Use Webmozart Assert when you want validation behavior that remains active in production code.
- What are alternatives to webmozart/assert for Laravel validation?
- Alternatives include Laravel's built-in Validator and FormRequest classes, symfony/validator, and beberlei/assert. Webmozart Assert is heavily inspired by beberlei/assert but provides more consistent placeholder ordering and clearer default error messages. Choose based on whether you need framework-level form validation or lightweight method-level assertions.
- How do I test code that uses webmozart/assert in PHPUnit?
- Test the behavior of the class or method and use expectException(Webmozart\Assert\InvalidArgumentException::class) for failure paths. Because assertions are static calls, mocking them directly is usually not practical; focus on valid and invalid inputs instead. You can also unit-test your custom error messages by catching the exception and inspecting its message.