Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

zx.parseFormSafe type inference does not handle ZodEffects #17

Closed
mdoury opened this issue Nov 13, 2022 · 1 comment · Fixed by #18
Closed

zx.parseFormSafe type inference does not handle ZodEffects #17

mdoury opened this issue Nov 13, 2022 · 1 comment · Fixed by #18

Comments

@mdoury
Copy link
Contributor

mdoury commented Nov 13, 2022

I noticed the zx.parseFormSafe type inference does not handle well ZodEffects schemas resulting from the use of refine, superRefine or transform. I will create a PR with a failing test and go from there. Do you have any idea how to fix this?

@mdoury
Copy link
Contributor Author

mdoury commented Nov 13, 2022

Just for context, I'm trying to parse a ZodEffects schema using zx.parseFormSafe with a FormData object because the form contains an image.

Here is a simplified reproduction of the error:

const schema = z
  .object({
    password: z.string().min(8),
    confirmPassword: z.string().min(8),
  })
  .refine(({ password, confirmPassword }) => password === confirmPassword);
const formData = new FormData();
formData.set('password', 'foo');
formData.set('confirmPassword', 'bar');
const result = zx.parseFormSafe(formData, schema);
if (result.success) {
  return json(result);
} 
const { formErrors, fieldErrors } = result.error.formErrors;
return json({ success: false, formErrors, fieldErrors });

The type of result.data seems correctly inferred, however, the type of result.error is not.
As a consequence, when I try to access fieldErrors.password or fieldErrors.confirmPassword in my template TypeScript is angry:

Property 'password' does not exist on type 'SerializeObject<UndefinedToOptional<{ _output?: string[] | undefined; _input?: string[] | undefined; refinement?: string[] | undefined; innerType?: string[] | undefined; _parse?: string[] | undefined; _type?: string[] | undefined; ... 27 more ...; isNullable?: string[] | undefined; }>>'. ts(2339)

I tried parsing directly with safeParse and safeParseAsync from zod and it gets it right.

It seems to be coming from the definition of SafeParsedData:

type SafeParsedData<T extends ZodRawShape | ZodTypeAny> = T extends ZodTypeAny
  ? SafeParseReturnType<T, ParsedData<T>>
  : T extends ZodRawShape
  ? SafeParseReturnType<ZodObject<T>, ParsedData<T>>
  : never;

The ZodEffects case is not handled, and SafeParseReturnType<Input, Output> gets the right Output which explain the correct inference of result.data, but the Input is visibly not appropriate.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant