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) during construction to ensure invariants are met (e.g., Assert::positiveInteger($age)).if (!is_int($id)) throw new Exception()) by adopting a battle-tested, opinionated library."The email must be a valid address. Got: invalid@example"), improving debugging.Assert::isInstanceOf($result, User::class)).beberlei/assert: Standardize error messages and placeholder consistency across the codebase if using the older package.Assert::directory($uploadPath)) that could lead to runtime failures or security vulnerabilities.Validator for HTTP requests) and doesn’t need granular, method-level assertions."This package lets us fail fast and fail loudly by validating inputs/outputs at the method level—catching bugs early and reducing production errors. For example, instead of a cryptic TypeError in an API, we’ll get clear messages like 'The user ID must be an integer. Got: "abc123"'. This improves developer productivity and user experience. It’s a lightweight, MIT-licensed solution with 7.6K stars, used by teams building robust PHP applications."
*"webmozart/assert gives us:
beberlei/assert, placeholders are standardized).Assert::email($email)).if (!is_int($id)) throw new Exception() with Assert::integer($id).Assert::integer($age)->greaterThan($age, 0)).Use cases:
Assert::range($discount, 0, 100)).if/throw spaghetti with readable assertions.Migration path: Start with critical paths (e.g., user creation) and expand. Works alongside Laravel’s Validator for HTTP requests."*
How can I help you explore Laravel packages today?