Product Decisions This Supports
- Schema Validation & Data Integrity: Enables strict input/output validation for APIs, forms, and database interactions, reducing runtime errors and improving data consistency.
- API Contracts & Documentation: Simplifies API design by defining clear object schemas (e.g., for GraphQL, REST, or internal services), accelerating developer onboarding and reducing miscommunication.
- Build vs. Buy: Avoids reinventing wheel for complex object casting/validation logic (e.g., replacing manual
array_map or Validator chains with a declarative approach).
- Use Cases:
- Admin Panels: Validate and cast user-submitted data (e.g., CMS content, user profiles) before saving to the database.
- Microservices: Enforce request/response contracts between services (e.g., validate payloads from a frontend or third-party API).
- Legacy System Modernization: Gradually introduce structured data handling in monolithic PHP apps without full refactoring.
- Rapid Prototyping: Quickly scaffold object models for MVP features (e.g., e-commerce products, event registrations).
When to Consider This Package
Adopt if:
- Your Laravel app requires consistent, reusable object validation across multiple layers (e.g., API, CLI, queues).
- You’re tired of boilerplate validation logic (e.g., nested
Validator::make() calls or manual type checks).
- Your team needs self-documenting data structures (schemas act as living docs for developers and QA).
- You’re integrating with external systems (e.g., payment gateways, SaaS APIs) where input/output schemas are critical.
Look elsewhere if:
- You’re using Laravel’s built-in validation (e.g.,
Validator facade) for simple forms and don’t need schema-driven casting.
- Your project is small-scale (e.g., a CRUD app with minimal data complexity).
- You prefer type systems like PHP 8.2+ attributes (
#[ArrayShape], #[Assert]) or libraries like spatie/laravel-data.
- You need advanced features like polymorphic relationships or complex nested mutations (consider
spatie/laravel-activitylog or custom solutions).
- Your team lacks PHP/OOP familiarity—this package assumes intermediate comfort with classes and traits.
How to Pitch It (Stakeholders)
For Executives:
"This package lets us treat JSON/data like structured objects—think of it as ‘TypeScript for PHP.’ It cuts debugging time by 30%+ by validating data upfront (e.g., catching malformed API payloads before they hit the database). For example, if our checkout API receives invalid user_address data, this package will reject it immediately with clear error messages, reducing support costs. It’s a low-risk investment (MIT license, minimal setup) that pays off in scalability and developer velocity."
For Engineers:
*"Object-Models replaces repetitive validation code with declarative schemas. For instance, instead of writing:
$validator = Validator::make($request->all(), [
'name' => 'required|string|max:255',
'email' => 'required|email',
'address.city' => 'required|string',
]);
You define a schema once:
class UserSchema extends Schema {
public function rules(): array {
return [
'name' => 'required|string',
'email' => 'required|email',
'address' => new AddressSchema(),
];
}
}
Then cast input/output seamlessly:
$user = UserSchema::cast($request->all()); // Validates + converts to object
Key benefits:
- DRY validation: Reuse schemas across controllers, jobs, and CLI commands.
- Type safety: Cast JSON to PHP objects/arrays with default values or transformations.
- Debugging: Clear error messages tied to schema fields (e.g.,
'address.city' must be a string).
- Future-proof: Works with Laravel’s ecosystem (e.g., integrate with
spatie/laravel-medialibrary for file uploads).
Migration Path:
Start with critical paths (e.g., API endpoints, admin forms). Pair with Laravel’s FormRequest for hybrid validation. Example roadmap:
- Week 1: Schema for
User and Order models.
- Week 2: Replace manual validation in 2–3 high-traffic endpoints.
- Week 3: Extend to CLI commands or queue jobs.
Tooling: Add a
phpstan rule to enforce schema usage where needed."*