turkerdev / fastify-type-provider-zod

MIT License
411 stars 25 forks source link

Failed validation returning status 500 instead of 400 #113

Closed severireivinen closed 1 month ago

severireivinen commented 1 month 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 1 month ago

Thank you for the report! I will try to reproduce

kibertoad commented 1 month 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)

kibertoad commented 1 month ago

@severireivinen Having investigated further, I believe that the error that you are receiving is most likely due to your error handler expecting ZodError to be thrown, and the latest version of the library throwing FastifyError with validation field instead. Together with @Bram-dc we have released 4.0.0 which exposes type guards for working with the new types conveniently, you may want to upgrade to that. See "Customizing error responses" section of the updated readme.

kibertoad commented 1 month ago

@severireivinen You can see here an example of update needed when transitioning to a newer version of fastify-type-provider-zod: https://github.com/lokalise/node-service-template/pull/842

severireivinen commented 1 month ago

Thank you for the investigations @kibertoad ! I haven't had time to come back to this yet but will do this week

severireivinen commented 1 month ago

This solved the issue @kibertoad 🎉 Thank you for the help! I will close this.

kibertoad commented 1 month ago

glad to hear that, thank you for the update! please don't hesitate to reach out if anything else comes up