coka/rest-request-validator-bundle
Installation:
composer require coka/rest-request-validator-bundle
Add the bundle to config/bundles.php:
CedrickOka\RestRequestValidatorBundle\CedrickOkaRestRequestValidatorBundle::class => ['all' => true],
First Use Case: Define a validator for a controller action using annotations:
use CedrickOka\RestRequestValidatorBundle\Annotation\ValidateRequest;
class UserController extends AbstractController
{
/**
* @ValidateRequest({
* "name": {"required": true, "type": "string"},
* "email": {"required": true, "type": "email"}
* })
*/
public function create(Request $request)
{
// Logic here
}
}
Key Files:
src/Annotation/ValidateRequest.php (for annotations)config/packages/cedrickoka_rest_request_validator.yaml (default config)Annotation-Based Validation:
Use @ValidateRequest on controller methods to enforce rules:
/**
* @ValidateRequest({
* "user": {
* "required": true,
* "type": "object",
* "properties": {
* "id": {"type": "integer"},
* "name": {"type": "string", "maxLength": 50}
* }
* }
* })
*/
public function update(Request $request, int $id)
{
// ...
}
Dynamic Rule Loading:
Load rules from YAML/JSON files (e.g., config/validation/rules.yml):
user_create:
name: {required: true, type: string}
email: {required: true, type: email}
Reference in annotation:
@ValidateRequest("user_create")
Event-Based Integration:
Subscribe to kernel.request event to validate globally:
public function onKernelRequest(GetResponseEvent $event)
{
$request = $event->getRequest();
if ($request->isMethod('POST') && $request->getPathInfo() === '/api/users') {
$validator = $this->container->get('cedrickoka_rest_request_validator.validator');
$validator->validate($request, ['name', 'email']);
}
}
Custom Validators: Extend the validator service to add business logic:
$validator->addCustomRule('is_active', function ($value) {
return $value === 'true' || $value === '1';
});
Validator for hybrid validation.Validation extension for OpenAPI schema generation.$validator = $this->createMock(ValidatorInterface::class);
$validator->expects($this->once())->method('validate');
$this->container->set('cedrickoka_rest_request_validator.validator', $validator);
Annotation Parsing:
doctrine/annotations is installed (composer require doctrine/annotations).php bin/console cache:clear
Rule Conflicts:
required: true + default: null) may cause unexpected behavior. Test edge cases.Performance:
Type System:
"type": "integer" accepts "123" but not "abc"). For strict validation, combine with Symfony’s Assert\Type.Enable Debug Mode:
# config/packages/cedrickoka_rest_request_validator.yaml
debug: true
Logs validation errors to Symfony’s profiler.
Error Handling:
Customize error responses in config/packages/cedrickoka_rest_request_validator.yaml:
error_handler:
http_status: 422
format: json
message: "Validation failed: %errors%"
Custom Rule Providers:
Implement CedrickOka\RestRequestValidatorBundle\Validator\RuleProviderInterface to add reusable rule sets.
Event Dispatching:
Extend the bundle’s events (e.g., ValidationFailedEvent) to trigger side effects:
$dispatcher->addListener(
'cedrickoka_rest_request_validator.validation_failed',
function (ValidationFailedEvent $event) {
// Log, notify, or retry logic
}
);
Configuration Overrides:
Override default rules in config/packages/cedrickoka_rest_request_validator.yaml:
default_rules:
email: {type: email, message: "Invalid email format"}
$validator->validate($request, ['name', 'email'], ['skip_missing' => true]);
# config/packages/cedrickoka_rest_request_validator.yaml
error_handler:
translation_domain: "validation"
How can I help you explore Laravel packages today?