Skip to content

fix: skip non-numeric status codes in response schema validation #1165

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed

Conversation

ajaykarthikr
Copy link

@ajaykarthikr ajaykarthikr commented Apr 16, 2025

When defining response schemas with status codes, only numeric HTTP status codes (like 200, 404, etc.) should be processed. Previously, the validator would crash if non-numeric keys were present in the response schema definition.

Error

undefined is not an object (evaluating 'maybeNameOrSchema.type')

This change adds a validation check to safely ignore any non-numeric status codes, making the schema validator more robust against invalid inputs.

And one of the use case is that we can set a default status in response schema as allowed by OpenAPI 3.0.4 specification.

const app = new Elysia().get(
  '/health',
  () => {
    return {
      status: 200,
      response: 'Hello world',
    };
  },
  {
    response: {
      200: t.String(),
      default: t.String(),
    },
  },
);

This will be generate in Swagger UI like this:

Screenshot 2025-04-16 at 3 27 44 PM

Also, fixes #1164

When defining response schemas with status codes, only numeric HTTP status codes
(like 200, 404, etc.) should be processed. Previously, the validator would crash
if non-numeric keys were present in the response schema definition.

This change adds a validation check to safely ignore any non-numeric status
codes, making the schema validator more robust against invalid inputs.
SaltyAom added a commit that referenced this pull request May 5, 2025
@SaltyAom
Copy link
Member

SaltyAom commented May 5, 2025

Merged to #1141 via 85c28b3

@SaltyAom SaltyAom closed this May 5, 2025
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.

Setting reference model as default response throws an internal server error
3 participants