Skip to content

Response validation errors should return a 500 rather than a 422 #1480

@marcalexiei

Description

@marcalexiei

What is the problem this feature would solve?

Currently, when the response does not conform to the provided schema,
a 422 HTTP status code (Unprocessable Entity) is returned.

Reproduction

import Elysia from "elysia";
import { z } from "zod";

export const app = new Elysia()
  .get(
    "/api-keys",
    (ctx) => {
      return {
        keys2: [],
      };
    },
    {
      response: {
        200: z.strictObject({
          keys: z.array(z.unknown()),
        }),
      },
    }
  )
  .listen(3000);
  1. Start the server

  2. Perform a request

    curl -i http://localhost:3000/api-keys
  3. Check response status code:

    HTTP/1.1 422 Unprocessable Entity
    Content-Type: application/json
    Date: Tue, 14 Oct 2025 09:49:55 GMT
    Content-Length: 519
    
    # omitted for brevity
    

What is the feature you are proposing to solve the problem?

Since an invalid response typically requires server-side intervention,
I believe a 500 HTTP status code (Internal Server Error) would be more appropriate in this scenario.

What do you think?

If you agree, I can try submitting a PR with this change.

What alternatives have you considered?

N/A

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions