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

.required() doesn't remove optional flag from the result of .nullish() #1046

Closed
Darkhogg opened this issue Mar 24, 2022 · 7 comments · Fixed by #1542
Closed

.required() doesn't remove optional flag from the result of .nullish() #1046

Darkhogg opened this issue Mar 24, 2022 · 7 comments · Fixed by #1542

Comments

@Darkhogg
Copy link

The (undocumented) function .required is not working properly for properties defined with .nullish():

const base = z.object({
  required: z.string(),
  nullable: z.string().nullable(),
  optional: z.string().optional(),
  nullableOptional: z.string().nullable().optional(),
  nullish: z.string().nullish(),
})

const req = base.required()

The resulting inferred type of req is:

{
    required: string;
    nullable: string | null;
    optional: string;
    nullableOptional: string | null;
    nullish?: string | null | undefined;
}

As you can see, all fields that were defined as .optional() have properly been made non-optional except for the one created using .nullish()

This seems to arise from the fact that .nullish() is defined to return a ZodNullable<ZodOptional<T>> and .required simply returns the outermost ZodOptional without doing any type recursion, and as such it cannot see the optional in that type.

@stale
Copy link

stale bot commented May 23, 2022

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added wontfix This will not be worked on and removed wontfix This will not be worked on labels May 23, 2022
@SrBrahma
Copy link
Contributor

Up

@3x071c
Copy link

3x071c commented Jun 26, 2022

Here's how I solved it: https://github.com/3x071c/lsg/blob/e2a9592ba3ec5103556f2cf307c32f08aeaee32d/app/lib/util/zod.ts
It's inspired from the .required() implementation in the zod source code. Since it has similar semantics, it can be integrated as-is. One major difference is that due to the circumstance of it being an external module, it accepts zod objects as first parameter, not as this (via chaining).

@stale
Copy link

stale bot commented Aug 30, 2022

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the wontfix This will not be worked on label Aug 30, 2022
@SrBrahma
Copy link
Contributor

Up

@stale stale bot removed the wontfix This will not be worked on label Aug 31, 2022
@zomars
Copy link

zomars commented Oct 7, 2022

Having the same issue. I've opened a question here.

igalklebanov added a commit to igalklebanov/zod that referenced this issue Nov 5, 2022
@igalklebanov
Copy link
Contributor

Having the same issue. I've opened a question here.

Not quite, you removed null values in your example.

Required is defined in typescript and in zod as being the opposite of Partial. So it should keep null, and only remove undefined and ?.

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.

5 participants