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

Fix: Optional object properties are not equivalent to required properties with undefined values #3817

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

MrPossumz
Copy link

@MrPossumz MrPossumz commented Oct 24, 2024

The object input and output logic currently used in z.object() converts all keys with a potential value of undefined to optional even if they are required to be present on the object.

Current Behavior:

const obj = z.object({
  a: z.string(),
  b: z.union([z.string(), z.undefined()]),
});

obj._output; // { a: string; b?: string | undefined; }

This is different than the type determined by passing the object's type directly into ZodType, which does respect the difference between an absent key versus a key present with an undefined value.

Expected Behavior:

type ObjectWithKey = ZodType<{ a: string; b: number | undefined }>;
// ObjectWithKey output and input = { a: string; b: number | undefined }

type ObjectWithOptionalKey = ZodType<{ a: string; b?: number | undefined }>;
// ObjectWithKey output and input = { a: string; b?: number | undefined }

This PR addresses that by correctly differentiating the optional keys from required keys that may have a value of undefined. It also accounts for the difference in input/out for the zod default type and behavior.

All tests related to the code changes are passing but I did notice that a test for the Ip string type was failing on the main branch.

@MrPossumz MrPossumz closed this Oct 24, 2024
Copy link

netlify bot commented Oct 24, 2024

Deploy Preview for guileless-rolypoly-866f8a ready!

Built without sensitive environment variables

Name Link
🔨 Latest commit 1d0258d
🔍 Latest deploy log https://app.netlify.com/sites/guileless-rolypoly-866f8a/deploys/671ad495beba5b000871345f
😎 Deploy Preview https://deploy-preview-3817--guileless-rolypoly-866f8a.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

@MrPossumz MrPossumz changed the title Fix: Fix: Optional object properties are not equivalent to required properties with undefined values Oct 24, 2024
@MrPossumz MrPossumz reopened this Oct 24, 2024
@andersk
Copy link
Contributor

andersk commented Dec 18, 2024

Your modified types don’t match the behavior of Zod at runtime (which you haven’t changed):

> obj.parse({ a: 'foo' })
{ a: 'foo' }

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 this pull request may close these issues.

3 participants