turkerdev / fastify-type-provider-zod

MIT License
382 stars 24 forks source link

Failed validation returning status 500 instead of 400 #113

Open severireivinen opened 2 days ago

severireivinen commented 2 days ago

Been updating our dependencies and noticed that latest version of this package doesn't return proper error when validation fails.

Example route (this is wrapped in TypeProvider):

{
        method: "POST",
        url: "REDACTED",
        handler: translateContent,
        schema: {
          body: TRANSLATE_CONTENT_BODY_SCHEMA,
          response: {
            200: TRANSLATE_CONTENT_RESPONSE_SCHEMA,
          },
        },
      },

Currently using v2.1.0 Passing invalid body to endpoint correctly returns status 400

{
    "message": "Invalid params",
    "errorCode": "VALIDATION_ERROR",
    "details": {
        "error": [
            {
                "code": "invalid_type",
                "expected": "array",
                "received": "undefined",
                "path": [
                    "targetLanguages"
                ],
                "message": "Required"
            },
            {
                "code": "invalid_type",
                "expected": "object",
                "received": "undefined",
                "path": [
                    "content"
                ],
                "message": "Required"
            }
        ]
    }
}

Updating to 3.0.0 Passing invalid body to endpoint now returns 500

{
    "message": "Internal server error",
    "errorCode": "INTERNAL_SERVER_ERROR"
}
kibertoad commented 2 days ago

Thank you for the report! I will try to reproduce

kibertoad commented 1 day ago

@severireivinen I cannot reproduce the error, see https://github.com/turkerdev/fastify-type-provider-zod/pull/114

Could you please send a PR with a modification of the test from https://github.com/turkerdev/fastify-type-provider-zod/pull/114, adjusted to be closer to your use-case, illustrating how it is failing? I suspect that the error might be on your side rather than within the library (not reacting correctly to some of the behaviour changes in the major release)