Skip to content

feat!(#113): change validation errors to 422 and add custom error messages#135

Merged
usernane merged 1 commit into
devfrom
feat/113-validation-status-code-messages
Jun 1, 2026
Merged

feat!(#113): change validation errors to 422 and add custom error messages#135
usernane merged 1 commit into
devfrom
feat/113-validation-status-code-messages

Conversation

@usernane

@usernane usernane commented Jun 1, 2026

Copy link
Copy Markdown
Member

Summary

Change validation error HTTP status from 404 to 422 (Unprocessable Entity) and add support for custom per-parameter error messages.

Motivation

404 means "resource not found" — using it for validation errors is semantically incorrect and confuses API consumers. Additionally, the error response only listed parameter names with no human-readable explanation. Fixes #113.

Changes

  • Status code: ErrorResponse::invalidParams() and missingParams() now return 422
  • Custom messages: Added ParamOption::MESSAGE option and RequestParameter::getMessage()/setMessage()
  • Attribute support: #[RequestParam(..., message: 'Custom error')]
  • Response format: {"message": "Validation failed", "type": "error", "http-code": 422, "more-info": {"errors": {"field": "message"}}}
  • Auto-generated messages: If no custom message set, uses "Invalid value for parameter 'X'." or "Required parameter 'X' is missing."
  • Backward compat for ErrorResponse: Accepts both string names and RequestParameter objects

UI Changes

N/A

How to Test / Verify

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

9 new tests, 19 assertions. Full suite: 583 tests pass.

Breaking Changes and Migration Steps

Status code changed from 404 to 422 for validation errors. Clients checking for 404 on invalid/missing parameters need to check for 422 instead.

Response format changed: more-info now contains {"errors": {"field": "message"}} instead of {"invalid": ["field"]} or {"missing": ["field"]}.

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 #113

…ages

BREAKING CHANGE: Validation error responses now return HTTP 422 instead of 404.

- Change ErrorResponse::invalidParams() and missingParams() from 404 to 422
- Add ParamOption::MESSAGE for custom per-parameter error messages
- Add RequestParameter::getMessage()/setMessage()
- Add 'message' param to #[RequestParam] attribute
- Response format: {message, type, http-code: 422, more-info: {errors: {field: msg}}}
- If no custom message: auto-generates 'Invalid value for parameter X' or 'Required parameter X is missing'
- WebServicesManager now passes RequestParameter objects to ErrorResponse
- Update existing tests for new 422 status code and response format
- 9 new tests covering all message scenarios
@sonarqubecloud

sonarqubecloud Bot commented Jun 1, 2026

Copy link
Copy Markdown

Quality Gate Failed Quality Gate failed

Failed conditions
14.9% Duplication on New Code (required ≤ 6%)

See analysis details on SonarQube Cloud

@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.77%. Comparing base (2793917) to head (26341e2).

Additional details and impacted files
@@             Coverage Diff              @@
##                dev     #135      +/-   ##
============================================
+ Coverage     93.75%   93.77%   +0.01%     
- Complexity     1317     1322       +5     
============================================
  Files            39       39              
  Lines          3218     3227       +9     
============================================
+ Hits           3017     3026       +9     
  Misses          201      201              
Flag Coverage Δ
php-8.3 93.77% <100.00%> (+0.01%) ⬆️

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 ea28ea9 into dev Jun 1, 2026
5 of 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