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

ZodObject second generic arg is required after upgrading 3.20.2 -> 3.20.3 #2012

Closed
urugator opened this issue Feb 8, 2023 · 6 comments
Closed

Comments

@urugator
Copy link

urugator commented Feb 8, 2023

Hi, after upgrading from 3.20.2 to 3.20.3, my code is broken, because ZodObject now requires at least 2 generic params, eg

ERROR in ./apps/api/src/app/utils/misc.ts:81:5
TS2707: Generic type 'ZodObject<T, UnknownKeys, Catchall, Output, Input>' requires between 2 and 5 type arguments.
    79 |   prefix?: P,
    80 | ): P extends ''
  > 81 |   ? z.ZodObject<T> | z.ZodObject<{ [key in keyof T]: z.ZodNull }>
       |     ^^^^^^^^^^^^^^
    82 |   :
    83 |       | z.ZodObject<CapitalizeAndPrefixProps<T, P>>
    84 |       | z.ZodObject<{

If this change was intended could someone help me fixing the code?

export function outerJoinSchema<T extends z.ZodRawShape, P extends string = ''>(
  shape: T,
  prefix?: P,
): P extends ''
  ? z.ZodObject<T> | z.ZodObject<{ [key in keyof T]: z.ZodNull }>
  :
      | z.ZodObject<CapitalizeAndPrefixProps<T, P>>
      | z.ZodObject<{
          [key in keyof CapitalizeAndPrefixProps<T, P>]: z.ZodNull;
        }> {
  const prefixedSchema = prefix
    ? prefixPropsSchema(capitalizePropsSchema(shape).shape, prefix)
    : z.object(shape);
  const nullSchema = Object.keys(prefixedSchema.shape).reduce(
    (result, key) => {
      result[key] = z.null();
      return result;
    },
    // eslint-disable-next-line @typescript-eslint/no-explicit-any
    {} as any,
  ) as z.ZodObject<{ [key in keyof typeof prefixedSchema.shape]: z.ZodNull }>;
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
  return z.union([prefixedSchema, nullSchema]) as any;
}

The function takes zod shape and generates a union of the original shape and same shape where all props are null. Optionally the props are prefixed. Eg:
outerJoinSchema({ foo: z.string() }, 'prefix')
=>

z.union([
  z.object({ prefixFoo: z.string() }),
  z.object({ prefixFoo: z.null() }),
])

Originally it was supposed to take schema instead of shape, but I couldn't make it work type-wise.

@urugator
Copy link
Author

urugator commented Feb 8, 2023

Oh perhaps am I supposed to use ZodObjectDef instead of ZodObject?

EDIT: well after changing it to ZodObjectDef it doesn't work anymome...

EDIT: Added 'strip' everywhere as second arg, seems to work, but dunno if correct solution.

@SkyaTura
Copy link

SkyaTura commented Feb 8, 2023

+1 at this issue, this shouldn't be in a patch upgrade, broke many things. I recommend rolling back to 3.20.2 while it's unstable...

@colinhacks
Copy link
Owner

Apologies, fixed in 3.20.4.

@colinhacks
Copy link
Owner

colinhacks commented Feb 8, 2023

@SkyaTura What other breaking changes have you experienced? Please file issues.

@SkyaTura
Copy link

SkyaTura commented Feb 8, 2023

@colinhacks it was something related to merging objects defs. Unfortunately I was on a rush and had to quickly undo the update in order to delivery some things.

I may test it again with the bugged version for you when things get calm over here ;)

@colinhacks
Copy link
Owner

@SkyaTura Ah good call: #2016
Also fixed

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

No branches or pull requests

3 participants