beberlei/assert
Lightweight assertion library for validating method arguments and input data in PHP. Provides a fluent, readable API with many built-in rules (string, numeric, email, UUID, collection, etc.), clear exceptions, and easy extensibility for custom constraints.
Validator is suited for HTTP request validation (with rich features like localization and rule chaining), beberlei/assert fills gaps for:
Assertion::e164(), Assertion::uuid()) not natively supported in Laravel’s validator.Assert::that()->integer()->between()).Order::total() is a positive number).Validator facade extension).AssertionFailedException to HttpResponse in API controllers).Assert\Assertion as a singleton if needed).creating, updating) to validate attributes before persistence.if checks in Eloquent accessors/mutators (e.g., setTotalAttribute()).Validator (e.g., for non-standard formats like UUIDs or E.164 numbers).Validator::fails() but with richer error messages).Assertion for custom exceptions).AssertionFailedException is SPL-compatible but may require custom mapping to Laravel’s ValidationException for consistency.verifyNow() calls, which could be forgotten in async contexts (e.g., queues). Mitigate with:
Assert::lazyVerify()).verifyNow() is called in all code paths.Validator entirely, or supplement it? (Recommend: hybrid approach—use Laravel’s validator for HTTP, beberlei/assert for business logic.)AssertionFailedException messages map to Laravel’s ValidationException format? (Solution: Create a custom exception formatter.)composer require beberlei/assert).$this->app->singleton(Assertion::class, fn() => new Assertion());
if checks in services/repositories with assertions.// Before
if (!is_numeric($value)) {
throw new \InvalidArgumentException("Value must be numeric");
}
// After
Assertion::numeric($value, "Value must be numeric");
Validator.public function store(Request $request) {
$validator = Assert::lazy();
$validator->that($request->input('email'))->email();
$validator->that($request->input('phone'))->e164();
$validator->verifyNow(); // Throws LazyAssertionException on failure
}
protected function setTotalAttribute($value) {
Assertion::greaterThan($value, 0, "Total must be positive");
$this->attributes['total'] = $value;
}
public function handle() {
Assertion::uuid($this->job->data['user_id']);
// ...
}
AssertionFailedException to extend Laravel’s HttpResponseException for APIs:
use Assert\AssertionFailedException as BaseException;
class AssertionFailedException extends BaseException implements \Symfony\Component\HttpKernel\Exception\HttpExceptionInterface {
public function toArray() {
return ['error' => $this->getMessage()];
}
}
FormRequest validation:
public function rules() {
return ['email' => 'required|email'];
}
public function withValidator($validator) {
$validator->after(function ($validator) {
Assertion::e164($this->input('phone'));
});
}
Validator (they serve different purposes).composer.json:
"require": {
"beberlei/assert": "^3.0"
}
AssertionFailedException maps to a user-friendly response.if checks.minLength in one place).How can I help you explore Laravel packages today?