Skip to content

Add validationRules() method to base Dto class#79

Merged
dereuromark merged 2 commits intomasterfrom
feature/validation-rules-helper
Feb 8, 2026
Merged

Add validationRules() method to base Dto class#79
dereuromark merged 2 commits intomasterfrom
feature/validation-rules-helper

Conversation

@dereuromark
Copy link
Contributor

Adds a validationRules() method that returns framework-agnostic validation rules extracted from field metadata. This enables framework plugins (CakePHP, Laravel, Symfony) to bridge DTO validation rules to their native validator systems.

Usage

$dto = new UserDto(['name' => 'Test']);
$rules = $dto->validationRules();
// Returns:
// [
//     'name' => ['required' => true, 'minLength' => 2, 'maxLength' => 50],
//     'email' => ['required' => true, 'pattern' => '/^[^@]+@[^@]+$/'],
//     'age' => ['min' => 0, 'max' => 150],
// ]

Includes tests for both populated and empty metadata.

Returns framework-agnostic validation rules extracted from field metadata.
Enables framework plugins to bridge DTO rules to native validators.

Example: $dto->validationRules() returns:
[
    'name' => ['required' => true, 'minLength' => 2, 'maxLength' => 50],
    'email' => ['pattern' => '/^[^@]+@[^@]+$/'],
]
@codecov
Copy link

codecov bot commented Feb 8, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 82.99%. Comparing base (1db1605) to head (2eabced).
⚠️ Report is 4 commits behind head on master.

Additional details and impacted files
@@             Coverage Diff              @@
##             master      #79      +/-   ##
============================================
+ Coverage     82.93%   82.99%   +0.05%     
- Complexity     1375     1381       +6     
============================================
  Files            40       40              
  Lines          3323     3334      +11     
============================================
+ Hits           2756     2767      +11     
  Misses          567      567              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

dereuromark added a commit to php-collective/symfony-dto that referenced this pull request Feb 8, 2026
- Add DtoConstraintBuilder::fromDto() to convert DTO validation rules to Symfony Validator constraints
- Maps required/minLength/maxLength/min/max/pattern to NotBlank/Length/Range/Regex
- Add symfony/validator to require-dev and suggest
- Add comprehensive tests (8 tests, 21 assertions)
- Update docs with Validation Bridge section
- Depends on php-collective/dto#79 (validationRules method)
@dereuromark dereuromark merged commit 3b3cb45 into master Feb 8, 2026
12 checks passed
@dereuromark dereuromark deleted the feature/validation-rules-helper branch February 8, 2026 14:21
dereuromark added a commit to php-collective/symfony-dto that referenced this pull request Feb 9, 2026
* Add Symfony validation bridge (DtoConstraintBuilder)

- Add DtoConstraintBuilder::fromDto() to convert DTO validation rules to Symfony Validator constraints
- Maps required/minLength/maxLength/min/max/pattern to NotBlank/Length/Range/Regex
- Add symfony/validator to require-dev and suggest
- Add comprehensive tests (8 tests, 21 assertions)
- Update docs with Validation Bridge section
- Depends on php-collective/dto#79 (validationRules method)

* Add Symfony validator constraint bridge for DTO validation rules

* Fix CI: widen validator to ^7.0|^8.0 for PHP 8.2, add PHPStan ignore

* Rename docs/Usage.md to docs/README.md

* Update php-collective/dto constraint

* Update symfony/validator version constraint
dereuromark added a commit to php-collective/symfony-dto that referenced this pull request Feb 9, 2026
* Add Symfony validation bridge (DtoConstraintBuilder)

- Add DtoConstraintBuilder::fromDto() to convert DTO validation rules to Symfony Validator constraints
- Maps required/minLength/maxLength/min/max/pattern to NotBlank/Length/Range/Regex
- Add symfony/validator to require-dev and suggest
- Add comprehensive tests (8 tests, 21 assertions)
- Update docs with Validation Bridge section
- Depends on php-collective/dto#79 (validationRules method)

* Add Symfony validator constraint bridge for DTO validation rules

* Fix CI: widen validator to ^7.0|^8.0 for PHP 8.2, add PHPStan ignore

* Add DTO mapper and JSON response helpers

* Update php-collective/dto constraint
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant