-
Zod has a method inferFlattenedErrors that gives a fully specified type for the errors. Is it possible to get a similarly fully typed error for a validator built with with-zod? Currently the type of errors for a
where the Compare with the following Zod code
Here,
which specifies all the fields explicitly. This is quite useful for getting typescript based completion for ActionData in Remix. My current work-around to avoid specifying the
This works well enough, but I will need to keep it up to date when/if the error type in with-zod changes. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
We don't currently have a way to infer the type of the error result -- the best we have is Example of why const s = z.object({
names: z.array(z.string())
}); Then you get this type using type ErrorType = z.inferFlattenedErrors<typeof s>['fieldErrors'];
// { names?: string[] | undefined } But the correct type for the errors returned by the validator are actually type ActualErrorType = {
names?: string | undefined;
'names[0]'?: string | undefined;
'names[1]'?: string | undefined;
// ...etc
} I will probably change the structure of the field errors at some point in the future to be more like the flattened errors zod returns, but this is the situation right now. Also, I'm curious: What's your use-case for accessing the error response directly? For the most part your errors will be provided automatically by |
Beta Was this translation helpful? Give feedback.
We don't currently have a way to infer the type of the error result -- the best we have is
ValidationErrorResponseData
which usesRecord<string, string>
for the field errors. I'm also not totally sure if we can provide this inference.z.inferFlattenedErrors
doesn't produce an accurate type for our uses so we'd potentially have to find some way to implement this type from scratch for both zod and yup.Example of why
z.inferFlattenedErrors
isn't accurate. If you have this schemaThen you get this type using
z.inferFlattenedErrors
But the…