- Can I use php-schema to validate Laravel events like UserRegistered or OrderCreated?
- Yes, php-schema lets you define strict type definitions for Laravel events. You’d extend the event class to include schema validation in its `handle()` method or use middleware to validate payloads before dispatching. It works best for events with complex or critical data structures.
- How do I install php-schema in a Laravel project?
- Run `composer require event-engine/php-schema` to install. The package has minimal dependencies, but ensure your project uses PHP 8.0+ and Laravel 9+ for compatibility. Check the GitHub repo for any polyfills if issues arise with newer versions.
- Does php-schema work with Laravel’s built-in event system (Event::dispatch)?
- Yes, but you’ll need to manually integrate schema validation. For example, validate event payloads in listeners using `php-schema` before processing. It doesn’t replace Laravel’s event system but adds schema enforcement on top of it.
- Will php-schema break if I update Laravel from 9 to 10?
- There’s a risk since the package’s last release was in 2021. Test thoroughly with Laravel 10+ and PHP 8.2+, as type hints or dependency changes might require adjustments. Consider forking or patching if compatibility issues arise.
- Can I use php-schema for API request/response validation instead of Laravel’s FormRequest?
- Yes, php-schema can validate API payloads by defining schemas for requests/responses. You’d integrate it in `FormRequest::rules()` or middleware, but Laravel’s built-in validation might be simpler for basic cases. It shines for complex nested structures.
- How do I handle schema changes (e.g., adding a new field to an event)?
- php-schema doesn’t include built-in migration tools, so you’ll need to manually update schemas and handle backward/forward compatibility. Include a `schema_version` field in event payloads and implement logic to parse old/new formats gracefully.
- Is php-schema suitable for high-frequency events (e.g., real-time systems)?
- Schema validation adds overhead, so benchmark performance in your environment. For high-throughput systems, consider caching validated schemas or using lighter alternatives like `spatie/array-to-object` for simple cases.
- Can I integrate php-schema with non-Laravel event buses like RabbitMQ or Kafka?
- Yes, but you’ll need to wrap the package in a custom validator. For example, use it alongside a JSON Schema validator (like `justinrainbow/json-schema`) to validate messages before/after processing in your event bus.
- Are there alternatives to php-schema for Laravel event validation?
- Yes, consider `spatie/array-to-object` for simple validation or `php-event-sourcing/php-event` for event-sourcing patterns. Laravel’s built-in validation (e.g., `Validator::make()`) may suffice for basic cases, but php-schema offers reusable type definitions for complex schemas.
- How do I test schema validation in Laravel unit tests?
- Mock event payloads and assert validation failures/successes using PHPUnit. For example, test that invalid payloads throw exceptions or that valid payloads pass schema checks. Use `php-schema`’s validation methods directly in your test cases.