You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Our application is using zod in order to validate data and log any errors. However, because we want to error gracefully, we decide to use safeParse for validation.
We also use the intellisense provided by our IDE in order for better code documentation / clarity throughout the application, and for this we want to be able return the data field from our safe parse.
We want something like this:
constgetCustomer=async(id : string)=>{returnsafeParseAsync(awaitaxios.get(`api/v1/customer/${id}`)).then(result=>{if(!result.success){// log the error via some logging service}returnresult}).data
This example uses safeParseAsync but would also apply to safeParse.
Although, some parts of the data may fail validation, we may still want to be able to soften the blow later in the application and not disrupt the user experience.
The text was updated successfully, but these errors were encountered:
This looks incomplete.
You must have a schema that you call . safeParseAsync.
What you are requesting already is supported you only have to structure it a bit differently
For example:
const getCustomer = async (id: string) => {
const response = await Schema.safeParseAsync(
await axios.get(api/v1/customer/${id}),
);
if (!response.success) {
throw response.error;
}
return response.data;
};
Or you can use safeParse which is better imo.
const getCustomer = async (id: string) => {
const response = await axios.get(api/v1/customer/${id});
const parsedResponse = Schema.safeParse(response.data);
if (!parsedResponse.success) {
throw parsedResponse.error;
}
return parsedResponse.data;
};
Or the best way is to just use parse that throws errors by design, so there is no need to return anything as zod will do it for you. I suggest this way of doing server validation as zod errors are quite a user friendly. :)
Our application is using zod in order to validate data and log any errors. However, because we want to error gracefully, we decide to use safeParse for validation.
We also use the intellisense provided by our IDE in order for better code documentation / clarity throughout the application, and for this we want to be able return the data field from our safe parse.
We want something like this:
This example uses safeParseAsync but would also apply to safeParse.
Although, some parts of the data may fail validation, we may still want to be able to soften the blow later in the application and not disrupt the user experience.
The text was updated successfully, but these errors were encountered: