Typical Symfony API endpoint:
- Request DTO
- Symfony Validator constraints
- OpenAPI annotations
- Mapping logic
Same field described multiple times.
class CreateUserRequest
{
#[Assert\NotBlank]
#[Assert\Email]
public string $email;
}Plus OpenAPI annotations and mapping.
This logic is repeated in 3 different places in real projects.
One schema:
{
"type": "object",
"required": ["email"],
"properties": {
"email": {
"type": "string",
"format": "email"
}
}
}Used for:
- validation
- request mapping
- OpenAPI generation
One schema replaces all of this duplication.
composer require outcomer/symfony-json-schema-validationuse Outcomer\Bundle\SymfonyJsonSchemaValidation\Attribute\MapRequest;
class UserController
{
#[Route('/api/users', methods: ['POST'])]
public function create(
#[MapRequest('schemas/user-create.json')]
UserCreateDto $user
): JsonResponse {
// $user contains validated data from request body, query, path, and headers
return new JsonResponse(['id' => $userService->create($user)]);
}
}{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"type": "object",
"properties": {
"body": {
"type": "object",
"properties": {
"name": {"type": "string", "minLength": 1},
"email": {"type": "string", "format": "email"}
},
"required": ["name", "email"]
},
"query": {
"type": "object",
"properties": {
"locale": {"type": "string", "enum": ["en", "de", "fr"]}
}
},
"headers": {
"type": "object",
"properties": {
"x-api-version": {"type": "string", "pattern": "^v[1-9]$"}
}
}
}
}Read the story behind this bundle on Hashnode
Complete Documentation - Visit our comprehensive documentation website
- π How It Works
- π Installation Guide
- π Quick Start Tutorial
- π Schema Basics
- π Configuration Options
- π DTO Injection
- π OpenAPI Integration
- π Examples
- π API Reference
- Single source of truth for API validation
- No serializer groups
- Automatic OpenAPI generation
Use API Platform if you need:
- full CRUD automation
- admin panels
- heavy framework magic
You can use this bundle:
- on a single endpoint
- together with Symfony Validator
- together with API Platform
- without rewriting your application
No migration is required.
No. You can use both together.
Yes. The bundle can coexist with API Platform.
No. You can adopt it endpoint-by-endpoint.
To avoid duplication between validation, request mapping and OpenAPI.
No. Your schemas remain standard JSON Schema documents.
We welcome contributions! Please see our Contributing Guide for details.
This project is licensed under the MIT License - see the LICENSE file for details.
Need Help?
- π Check our documentation
- π Report issues
- π¬ Join discussions