Returning a 400 error for invalid input #787
-
If I define and API route such as
and then I call that API but only provide one of those fields as input, for example The API server responds with a 500, and I see
However, for input validation, I would expect the API server to respond with a 400 (bad request) because the input provided by the user didn't match the specified input schema. I'm wondering why this isn't the default behaviour (or if it is, what I might be doing wrong, because i did override the |
Beta Was this translation helpful? Give feedback.
Replies: 5 comments 5 replies
-
From a little more investigation, it seems like Right now, we have no way to differentiate errors that happened during backend code execution (which should throw a 500) from errors that are thrown during this parse step, which IMO should return a 400. I am aware that I can override the Edit: I went one step further in my investigation and discovered that the
however, this doesn't work in our case because we ALSO use |
Beta Was this translation helpful? Give feedback.
-
Hello, @TheWisestOne . Thank you for the clarification. It made it clear now.
The problem you state is that What you can do in this regard.
// this is your Endpoint's handler
const validation = schema.safeParse(valueFromDb);
if (!validation.success) {
throw new Error(validation.error.message); // this is for 500, but you can handle it according to the local needs
} // else {
const theStuffFromDb = validation.data;
// this is your custom error file
class DbError extends Error {
public override name = "DbError";
} // this is your Endpoint's handler, or it can be extracted into another function
try {
const theStuff = getTheStuff()
} catch (e) {
if (e instance of ZodError) {
throw new DbError(e.message);
}
// ... other possible cases
} // and this is your custom error-to-status code transformer, for using in your custom ResultHandler
function getStatusCodeFromError(error: Error): number {
if (error instanceof ZodError) {
return 400;
}
if (error instanceof DbError) {
return 500;
}
// ... other cases
return 500;
} Let me know what you think about it. |
Beta Was this translation helpful? Give feedback.
-
@TheWisestOne , sorry for delay. |
Beta Was this translation helpful? Give feedback.
-
@TheWisestOne , I believe it will allow you using zod for your database communication and at the same time to avoid mixing the possible validation issues in it with the ones that are IO-related. Please let me know what you think. |
Beta Was this translation helpful? Give feedback.
-
thank you so much for your patience and all the efforts you made regarding this problem. For the consistency I decided to create So, this should enable you using zod for database validations. Consider checking the new version. Please let me know how it works. |
Beta Was this translation helpful? Give feedback.
@TheWisestOne ,
thank you so much for your patience and all the efforts you made regarding this problem.
The solution is about to be released in version
9.0.0-beta2
along with another potentially breaking change.For the consistency I decided to create
InputValidationError
class, that you can utilize in custom ResultHandler for responding with400
. There is also a couple of error handling methods exposed. If you're using the default ResultHandler, no changes needed.So, this should enable you using zod for database validations.
Consider checking the new version. Please let me know how it works.