Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Assert Laravel Package

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.

View on GitHub
Deep Wiki
Context7

Assertions to validate method input/output with nice error messages.

Frequently asked questions about Assert
How does Webmozart Assert compare to Laravel’s built-in validation (e.g., Form Requests)?
Webmozart Assert is designed for domain-level validation (e.g., constructor checks in models/services) where Laravel’s Form Requests focus on HTTP input. Use Assert for business logic invariants (e.g., `Assert::positiveInteger($orderId)`) and Laravel validation for user-facing rules (e.g., `required|email`). They complement each other seamlessly.
Can I use Webmozart Assert in Laravel middleware for API request validation?
Yes. Install the package via Composer, then validate payloads in middleware by catching `InvalidArgumentException` and converting it to a `JsonResponse` with HTTP 422. Example: `try { Assert::json($request->input()); } catch (InvalidArgumentException $e) { return response()->json(['error' => $e->getMessage()], 422); }`
Will Webmozart Assert work with Laravel’s dependency injection (e.g., constructor validation in services)?
Absolutely. The package integrates natively with Laravel’s container. Use assertions in service constructors (e.g., `Assert::uuid($userId)`) or bind assertions to interfaces for reusable validation logic. No extra configuration is needed beyond requiring the package.
How do I customize error messages in Webmozart Assert for Laravel API responses?
Pass a custom message as the second argument to assertions: `Assert::string($value, 'Field must be a string. Got: %s')`. For API consistency, wrap exceptions in middleware to format messages as JSON (e.g., `{'errors': {'field': 'Invalid value'}}`). The `%s` placeholder always represents the invalid input value.
Is Webmozart Assert compatible with Laravel 10 and PHP 8.2+?
Yes, the package supports Laravel 10 and PHP 8.2+. While PHP’s strict typing reduces some use cases (e.g., `integer()` checks), assertions remain valuable for runtime validation of dynamic data (e.g., API inputs or database fields). No breaking changes have been introduced in 5+ years of active maintenance.
Can I replace Laravel’s Rule objects (e.g., `Rule::uuid()`) with Webmozart Assert in Form Requests?
Yes, but with caution. Use assertions for complex, reusable logic (e.g., nested array validation) by overriding `withValidator()` in Form Requests. For simple rules, Laravel’s built-in validation is more concise. Example: `Assert::uuid($request->input('id'))` in a custom validator method.
How does Webmozart Assert handle performance in high-traffic Laravel APIs?
The package is optimized for speed, with benchmarks showing ~0.1ms per assertion. For high-throughput APIs, profile assertions in production-like environments. Avoid overusing assertions in tight loops; reserve them for critical validation (e.g., constructor checks or payload parsing).
What’s the difference between Webmozart Assert and beberlei/assert?
Webmozart Assert fixes a key usability issue in beberlei/assert: consistent placeholder ordering in error messages. All assertions use `%s` for the invalid value, followed by assertion-specific details (e.g., `%2$s` for min/max values). This makes custom messages predictable and easier to maintain.
How do I test Webmozart Assert in PHPUnit for Laravel applications?
Use assertions directly in tests for method outputs: `Assert::same($user->getFullName(), 'John Doe')`. For input validation, mock constructors/services and verify `InvalidArgumentException` is thrown. Example: `$this->expectException(InvalidArgumentException::class); new User('invalid@email');`
Are there alternatives to Webmozart Assert for Laravel validation?
For Laravel-specific needs, consider `laravel/validation` (built-in) or `spatie/laravel-validation-extensions` for advanced rules. For standalone PHP, `symfony/validator` is robust but heavier. Webmozart Assert stands out for its lightweight, expressive syntax and consistency in error messages, ideal for domain-driven validation.
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
hexters/coinpayment
rjcodes/rjcms
act-training/laravel-permissions-manager
alimarchal/laravel-chart-of-accounts
babenkoivan/elastic-scout-driver
mkwebdesign/filament-watchdog-v5
renatomarinho/laravel-page-speed
zedmagdy/filament-business-hours
renatovdemoura/blade-elements-ui
devgeek/beacon-admin
benjamin-rqt/data-watcher-bundle
atriumphp/atrium
sandermuller/package-boost-laravel
sandermuller/boost-skills
redaxo/core
yusufgenc/filament-api-forge
l3aro/rating-star-for-filament
leek/filament-subtenant-scope
anil/file-picker
broqit/fields-ai