Skip to content

feat(#115): add cross-field validation with #[Validate] attribute#134

Merged
usernane merged 1 commit into
devfrom
feat/115-cross-field-validation
Jun 1, 2026
Merged

feat(#115): add cross-field validation with #[Validate] attribute#134
usernane merged 1 commit into
devfrom
feat/115-cross-field-validation

Conversation

@usernane

@usernane usernane commented Jun 1, 2026

Copy link
Copy Markdown
Member

Summary

Add cross-field validation support via an overridable validate() method and a #[Validate] attribute for method-specific validators.

Motivation

Individual parameter validation cannot express rules that depend on multiple fields together (e.g., "passwords must match", "end date after start date"). Currently developers must do these checks inside processRequest() and manually send error responses. Fixes #115.

Changes

  • Added WebService::validate(array $inputs): array — service-wide hook, returns empty array by default
  • Added #[Validate('methodName')] attribute for method-specific cross-field validation
  • Both run if defined: service-wide first, then method-specific. Errors are merged.
  • Returns 422 with {"message": "Validation failed", "type": "error", "more-info": {"errors": {...}}} if validation fails
  • Missing validator method throws InvalidArgumentException (caught as 500)
  • Uses reflection to invoke private validator methods
  • Updated README with documentation

UI Changes

N/A

How to Test / Verify

php vendor/bin/phpunit tests/WebFiori/Tests/Http/CrossFieldValidationTest.php

10 new tests, 36 assertions. Full suite: 574 tests pass.

Breaking Changes and Migration Steps

None. validate() returns empty array by default — existing services are unaffected.

Checklist

  • I reviewed my own diff before requesting review
  • My commits follow Conventional Commits
  • The title of the pull request follows Conventional Commits
  • I added/updated tests (or explained why not)
  • I updated docs (if needed)
  • I ran lint/cs-fixer (if applicable)
  • I considered backward compatibility
  • I considered security

Related issues

Closes #115

- Add overridable validate(array $inputs): array on WebService (service-wide)
- Add #[Validate('methodName')] attribute for method-specific validation
- Both run: service-wide first, then method-specific, errors merged
- Returns 422 with error details if validation fails
- Missing validator method throws InvalidArgumentException (caught as 500)
- Uses reflection to invoke private validator methods
- Update README with cross-field validation documentation
- 10 new tests covering all paths
@sonarqubecloud

sonarqubecloud Bot commented Jun 1, 2026

Copy link
Copy Markdown

@codecov

codecov Bot commented Jun 1, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 93.75%. Comparing base (01ce116) to head (851d6a0).

Additional details and impacted files
@@             Coverage Diff              @@
##                dev     #134      +/-   ##
============================================
+ Coverage     93.69%   93.75%   +0.05%     
- Complexity     1308     1317       +9     
============================================
  Files            39       39              
  Lines          3188     3218      +30     
============================================
+ Hits           2987     3017      +30     
  Misses          201      201              
Flag Coverage Δ
php-8.3 93.75% <100.00%> (+0.05%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ 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.

@usernane usernane merged commit 2793917 into dev Jun 1, 2026
6 checks passed
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