Skip to content

issue: Conditional/dynamic schema resolution no longer working #782

@djshubs

Description

@djshubs

Version Number

5.1.1

Codesandbox/Expo snack

https://codesandbox.io/p/sandbox/angry-butterfly-ljt6z2

Steps to reproduce

Please see code sandbox. I get the error when when I conditionally set the formResolver

Expected behaviour

Previously, I was able to conditionally pass the schema to denote if it was update or create based on others props (please see code below and in code sandbox). However, since upgrading to 5.1.1 from 5.0.1, zodResolver gives me type errors.

export function AgencyForm({ agency }: { agency?: UpdateAgencyType }) {
  const defaultValues = agency
    ? { ...agency, legalName: agency.legalName ?? "" }
    : {
        slug: "",
        name: "",
        legalName: "",
      };
  const formResolver = agency ? updateAgencySchema : createAgencySchema;
  const form = useForm<z.infer<typeof formResolver>>({
    mode: "onBlur",
    reValidateMode: "onBlur",
    resolver: zodResolver(
      formResolver as z.ZodType<z.infer<typeof formResolver>>
    ),
    defaultValues,
  });

Here's the error I"m getting:

No overload matches this call.
  Overload 1 of 4, '(schema: Zod3Type<{ slug: string; name: string; acronym: string; legalName: string | null; } | { cuid: string; slug?: string | undefined; name?: string | undefined; acronym?: string | undefined; legalName?: string | ... 1 more ... | undefined; }, { ...; } | { ...; }>, schemaOptions?: ParseParams | undefined, resolverOptions?: NonRawResolverOptions | undefined): Resolver<...>', gave the following error.
    Argument of type 'ZodType<{ slug: string; name: string; acronym: string; legalName: string | null; } | { cuid: string; slug?: string | undefined; name?: string | undefined; acronym?: string | undefined; legalName?: string | ... 1 more ... | undefined; }, ZodTypeDef, { ...; } | { ...; }>' is not assignable to parameter of type 'Zod3Type<{ slug: string; name: string; acronym: string; legalName: string | null; } | { cuid: string; slug?: string | undefined; name?: string | undefined; acronym?: string | undefined; legalName?: string | ... 1 more ... | undefined; }, { ...; } | { ...; }>'.
      Types of property '_def' are incompatible.
        Property 'typeName' is missing in type 'ZodTypeDef' but required in type '{ typeName: string; }'.
  Overload 2 of 4, '(schema: $ZodType<unknown, FieldValues, $ZodTypeInternals<unknown, FieldValues>>, schemaOptions?: ParseContext<$ZodIssue> | undefined, resolverOptions?: NonRawResolverOptions | undefined): Resolver<...>', gave the following error.
    Argument of type 'ZodType<{ slug: string; name: string; acronym: string; legalName: string | null; } | { cuid: string; slug?: string | undefined; name?: string | undefined; acronym?: string | undefined; legalName?: string | ... 1 more ... | undefined; }, ZodTypeDef, { ...; } | { ...; }>' is not assignable to parameter of type '$ZodType<unknown, FieldValues, $ZodTypeInternals<unknown, FieldValues>>'.
      Property '_zod' is missing in type 'ZodType<{ slug: string; name: string; acronym: string; legalName: string | null; } | { cuid: string; slug?: string | undefined; name?: string | undefined; acronym?: string | undefined; legalName?: string | ... 1 more ... | undefined; }, ZodTypeDef, { ...; } | { ...; }>' but required in type '$ZodType<unknown, FieldValues, $ZodTypeInternals<unknown, FieldValues>>'.

What browsers are you seeing the problem on?

No response

Relevant log output

Code of Conduct

  • I agree to follow this project's Code of Conduct

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions