Installation
composer require domtomproject/easy-rest-bundle "dev-master"
composer update
Register the bundle in AppKernel.php:
new DomTomProject\EasyRestBundle\DomtomEasyRestBundle(),
Add minimal config in config.yml:
domtom_easy_rest: ~
Define Validation Rules
Create a YAML file (e.g., app/Resources/validation/User.yml) with validation rules:
default:
name:
- notEmpty
- stringType
email:
- notEmpty
- email
First Use Case Use the bundle in a controller to validate incoming data:
use DomTomProject\EasyRestBundle\Validator\Validator;
public function createAction(Request $request)
{
$validator = new Validator('User'); // 'User' refers to User.yml
$data = $request->request->all();
$errors = $validator->validate($data);
if (count($errors) > 0) {
return new JsonResponse(['errors' => $errors], 400);
}
// Proceed with data processing
return new JsonResponse(['success' => true]);
}
Validation in Controllers
Reuse the Validator class for all REST endpoints:
$validator = new Validator('User');
$errors = $validator->validate($request->request->all());
Dynamic Rule Selection Pass different rule sets dynamically:
$validator = new Validator('User', 'default'); // 'default' is the key in YAML
Integration with Forms Use alongside Symfony Forms for hybrid validation:
$form = $this->createForm(UserType::class, $user);
$form->submit($request->request->all());
if (!$form->isValid()) {
$validator = new Validator('User');
$errors = $validator->validate($request->request->all());
// Merge form and validator errors
}
API Resource Handling
Validate nested data (e.g., for POST /users/{id}/addresses):
# app/Resources/validation/Address.yml
default:
street:
- notEmpty
city:
- notEmpty
$validator = new Validator('Address');
$addressData = $request->request->get('address');
$errors = $validator->validate($addressData);
Custom Validation Logic Extend the validator for business rules:
$validator = new Validator('User');
$validator->addCustomRule('age', function ($age) {
return $age >= 18;
}, 'Age must be 18+');
kernel.request or kernel.controller events.Validator in PHPUnit tests:
$validator = $this->createMock(Validator::class);
$validator->method('validate')->willReturn([]);
YAML Syntax Quirks
$ symbol is required for dynamic keys in keySet:
# Correct
keySet:
- $key: pl
- in: [intermediate, basic]
keySet:
- key: pl
- in: [intermediate, basic]
Rule File Caching
Deprecated Dependencies
Respect/Validation (v0.x), which is abandoned. Expect compatibility issues with newer PHP/Symfony versions.Error Handling
$errors = [
'name' => ['The name is required.'],
'address.street' => ['Street is required.']
];
Case Sensitivity
notEmpty) are case-sensitive. Use exact matches from Respect/Validation docs.Validate YAML Syntax Use a YAML linter (e.g., yamllint) to catch syntax errors early.
Log Validation Rules Dump the parsed rules for debugging:
$validator = new Validator('User');
$rules = $validator->getRules(); // Hypothetical method; inspect internals if needed
var_dump($rules);
Test Edge Cases Validate with:
null or undefined values.float vs. int).Custom Validators
Extend DomTomProject\EasyRestBundle\Validator\Validator to add reusable logic:
class CustomValidator extends Validator
{
public function validateEmailFormat($email)
{
return v::email()->validate($email);
}
}
Override Rule Files Use Symfony’s parameter system to override the default validation path:
# config.yml
domtom_easy_rest:
validation_path: "%kernel.project_dir%/config/validation"
Event Dispatching Trigger events before/after validation (e.g., for logging or metrics):
$validator->addListener('preValidate', function ($data) {
// Pre-validation logic
});
Alternative Validation Libraries
Replace Respect/Validation with Symfony Validator by:
$cache = new FilesystemCache();
$rules = $cache->get('validation_rules', function () {
return $this->parseYamlRules();
});
How can I help you explore Laravel packages today?