basilicom/json-schema-request-validator-bundle
Pros:
EventDispatcher, RequestStack). Aligns with Symfony’s event-driven architecture (e.g., KERNEL_REQUEST event for pre-validation).400 Bad Request, enforcing API contracts early./users vs. /users/{id}).Cons:
Request object and event system.FilePathProvider, which may introduce complexity for large APIs.500 for missing schemas may not suit all use cases (e.g., graceful degradation or custom error responses).Laravel Compatibility:
Illuminate\Http\Request) differs significantly from Symfony’s. Key challenges:
App\Http\Kernel) instead of Symfony’s EventDispatcher. Requires custom middleware to replicate validation logic.Request extends HttpFoundation\Request, while Laravel’s Request is a separate class. Schema validation would need adaptation (e.g., via a facade or wrapper).route name → schema file) doesn’t directly translate to Laravel’s route naming conventions (e.g., Route::name()).justinrainbow/json-schema for validation logic.SchemaValidator) that both Laravel and Symfony could consume, reducing duplication.#[Schema(file: "user.schema.json")]) to map schemas, with a compiler pass to generate validation rules.Technical Risk:
HttpFoundation, Filesystem). Laravel alternatives (e.g., symfony/http-foundation via Composer) could introduce compatibility issues.Use Case Justification:
Validator, FormRequest)? Schema validation offers richer data modeling but may be overkill for simple APIs.spatie/laravel-json-api, [zircote/swagger-php)) that could fulfill similar needs with less integration effort?Architectural Trade-offs:
Request class) or aspect-oriented approach (e.g., Laravel’s around middleware) reduce coupling?json-schema-faker for testing be integrated?Performance:
Maintenance:
Alternatives:
zircote/swagger-php with Laravel middleware) be more sustainable than adapting this bundle?Laravel Ecosystem:
AppServiceProvider.#[ValidateSchema] attribute).HttpTests with JsonSchema assertions (e.g., via PHPUnit extensions).Compatibility Matrix:
| Laravel Feature | Bundle Equivalent | Integration Strategy |
|---|---|---|
| Middleware | Symfony Event Listeners | Create ValidateJsonSchema middleware |
| Route Annotations | JsonSchemaRequestValidationControllerInterface |
Custom #[Schema] attribute + compiler pass |
| Request Validation | Automatic 400 rejection |
Middleware + Validator facade |
| Error Handling | Custom 500 for missing schemas |
Override middleware error responses |
| Schema Storage | File-based (FilePathProvider) |
Laravel’s storage/ or config/ |
Phase 1: Proof of Concept (2-4 weeks)
justinrainbow/json-schema and symfony/http-foundation as dependencies.namespace App\Http\Middleware;
use Illuminate\Http\Request;
use Symfony\Component\HttpFoundation\JsonResponse;
use Zircote\Swagger\Validator;
class ValidateJsonSchema
{
public function handle(Request $request, Closure $next)
{
$schema = $this->resolveSchema($request);
$validator = new Validator();
$validator->validateJson($request->json()->all(), $schema);
if ($validator->errors()) {
return new JsonResponse($validator->errors(), 400);
}
return $next($request);
}
protected function resolveSchema(Request $request): string
{
// Logic to map route to schema (e.g., config, annotations)
return config("schemas.{$request->route()->getName()}");
}
}
app/Http/Kernel.php:
protected $middleware = [
\App\Http\Middleware\ValidateJsonSchema::class,
];
Phase 2: Route-Schema Mapping (2 weeks)
#[Schema(file: "user.schema.json")] on controllers/actions.
config/schemas.php:
return [
'users.store' => 'schemas/user_create.schema.json',
'users.update' => 'schemas/user_update.schema.json',
];
schemas/{resource}.schema.json).Phase 3: Error Handling & Edge Cases (1-2 weeks)
400 responses to include detailed schema errors (e.g., validator->errors()).404 instead of 500).Illuminate\Support\Facades\Cache).Phase 4: Testing & Optimization (1-2 weeks)
HttpTests for validation scenarios (valid/invalid payloads).How can I help you explore Laravel packages today?