Skip to content

Fix validation error HTTP status code and add custom error messages #113

@usernane

Description

@usernane

Is your feature request related to a problem? Please describe.
Two issues with current validation error responses:

  1. Wrong status code: WebServicesManager::invParams() and missingParams() return HTTP 404. Validation errors should return 422 Unprocessable Entity (RFC 4918). 404 means "resource not found," which is misleading.

  2. No custom messages: The error response only lists parameter names ("The following parameters have invalid values: 'email', 'age'.") with no human-readable explanation of what went wrong. Frontend developers cannot display meaningful error messages to users.

Describe the solution you'd like

Status code fix: Change response code from 404 to 422 in invParams() and missingParams().

Custom messages: Add a message option to RequestParameter:

$this->addParameter([
    'name' => 'email',
    'type' => 'email',
    'message' => 'Please provide a valid email address.'
]);

$this->addParameter([
    'name' => 'age',
    'type' => 'integer',
    'min-val' => 18,
    'message' => 'You must be at least 18 years old.'
]);

New error response format:

{
  "message": "Validation failed",
  "type": "error",
  "errors": {
    "email": "Please provide a valid email address.",
    "age": "You must be at least 18 years old."
  }
}

If no custom message is set, auto-generate based on the rule that failed (e.g., "Must be at least 18", "Required field", "Invalid email format").

Describe alternatives you'd considered

  • Keep 404 (incorrect semantics, confuses API consumers)
  • Only fix status code without messages (still poor DX for frontend developers)

Additional context
The status code change is technically breaking (clients checking for 404 on validation errors would need updating). Should be released in a major or minor version with a note in changelog.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions