- How do I integrate this package into Laravel’s FormRequest validation?
- Override the `rules()` method in your `FormRequest` class to return a schema object. Use the `Validator` class to validate the JSON payload against your schema, then skip Laravel’s default validation by returning an empty array. This works well for complex nested JSON structures where Laravel’s built-in validation falls short.
- Does this package support remote JSON Schema references (e.g., HTTP/HTTPS `$ref`)?
- Yes, the package supports remote schemas via `$ref` (e.g., `https://example.com/schema.json`). Use the `SchemaStorage` class to cache and resolve references, but note that remote schemas may introduce latency. For production, consider implementing retry logic or caching mechanisms to handle failures gracefully.
- Which JSON Schema drafts are fully supported, and how do I check compliance?
- Draft-3, Draft-4, and Draft-6 are well-supported, while Draft-7 has partial compliance. Check the [Bowtie report](https://bowtie.report/#/implementations/php-justinrainbow-json-schema) for real-time compliance metrics. For strict Draft-6 compliance, use `Constraint::CHECK_MODE_STRICT` to enforce validation rules.
- Can I use this package to validate API requests in Laravel middleware?
- Yes, you can create middleware to validate incoming JSON payloads before they reach your controllers. Instantiate the `Validator` in middleware and validate the request data against your schema. If validation fails, return a `JsonResponse` with formatted errors, similar to Laravel’s default validation responses.
- How do I customize error messages to match Laravel’s validation format?
- Use the `Validator::setErrorFormatter()` method to customize error messages. Alternatively, map the package’s errors to Laravel’s `Validator` exceptions by catching validation failures and throwing `ValidationException` with formatted error bags. This ensures consistency with Laravel’s built-in validation language.
- Will this package work with Laravel’s testing tools (e.g., `createJsonTest()`)?
- Yes, you can integrate this package into Laravel’s testing suite. Use `createJsonTest()` to send JSON payloads and validate responses with the `Validator` class. For schema testing, leverage the package’s built-in test suite to verify compliance with your draft requirements.
- How do I handle type coercion in validation (e.g., converting strings to booleans)?
- Enable type coercion by passing `Constraint::CHECK_MODE_COERCE_TYPES` to the `validate()` method. However, be cautious—coercion modifies input data, which may conflict with Laravel’s immutable request handling. Clone the input data before validation or use coercion sparingly in non-critical paths.
- What’s the best way to store and manage JSON schemas in a Laravel project?
- Store schemas in a `resources/schemas/` directory for static schemas or fetch them dynamically from a config service/database. Use `SchemaStorage` to cache resolved schemas and handle `$ref` references efficiently. For remote schemas, implement caching (e.g., Redis) to avoid repeated HTTP requests.
- Does this package support async validation for high-throughput APIs?
- The package itself doesn’t support async validation natively, but you can queue validation jobs for heavy schemas using Laravel’s queue system. Dispatch a job to validate the payload against the schema and return a response immediately, then process the validation result later.
- Are there alternatives to this package for JSON Schema validation in Laravel?
- Yes, alternatives include `zendframework/zend-schema` (more feature-rich but heavier) and `webonyx/graphql-php` (if you’re using GraphQL). However, `justinrainbow/json-schema` is lightweight (~1MB) and integrates seamlessly with Laravel’s ecosystem, making it ideal for most use cases without unnecessary bloat.