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: ZodObject pick/omit/required/partial strict args keys #2716

Closed
wants to merge 1 commit into from

Conversation

oljimenez
Copy link

@oljimenez oljimenez commented Sep 7, 2023

This PR add the ability to ZodObject pick/omit/required/partial functions to only allow valid object keys, instead of allowing any unkown keys (current behavior).
This fix the following Issues: #2607

New behavior:

import { z } from "./src";

const schema = z.object({
  name: z.string(),
  age: z.number(),
  isAdmin: z.boolean(),
});

schema.pick({
  name: true,
  asd: true, // <-- Now errors because `asd` is not in `schema `
  zxc: "", // <-- Now errors because `zxc` is not in `schema `
  isAdmin: true,
});


schema.omit({
  name: true,
  asd: true, // <-- Now errors because `asd` is not in `schema `
  zxc: "", // <-- Now errors because `zxc` is not in `schema `
  isAdmin: true,
});

schema.partial({
  name: true,
  asd: true, // <-- Now errors because `asd` is not in `schema `
  zxc: "", // <-- Now errors because `zxc` is not in `schema `
  isAdmin: true,
});

schema.required({
  name: true,
  asd: true, // <-- Errors because `asd` is not in `schema `
  zxc: "", // <-- Errors because `zxc` is not in `schema `
  isAdmin: true,
});

@netlify
Copy link

netlify bot commented Sep 7, 2023

Deploy Preview for guileless-rolypoly-866f8a ready!

Built without sensitive environment variables

Name Link
🔨 Latest commit 93d1395
🔍 Latest deploy log https://app.netlify.com/sites/guileless-rolypoly-866f8a/deploys/64f9317873bec200083b8f4d
😎 Deploy Preview https://deploy-preview-2716--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.

@ShravanSunder
Copy link

This would be great to have! @colinhacks would it be possible to merge this? 🙇🏾

as a work around i've been using satisfies to make it typesafe for now. Its not very ideal

const rawSelectSchema = createSelectSchema(chatMessageTable);
const rawInsertSchema = createInsertSchema(chatMessageTable);
export const chatMessageSchemas = {
	client: rawSelectSchema.omit({
		tenantId: true,
	} satisfies ZodKeyMask<typeof rawSelectSchema>),

@thelinuxlich
Copy link

Oh this would be very useful!

@Handfish
Copy link

+1 to this - Would be super useful

@Handfish
Copy link

@ShravanSunder Would you be willing to share your implementation of ZodKeyMask<>?

@ShravanSunder
Copy link

ShravanSunder commented Nov 24, 2023

@ShravanSunder Would you be willing to share your implementation of ZodKeyMask<>?

I think it was
export type ZodObjectMask<T extends AnyZodObject> = Partial<Record<keyof z.infer<T>, true>>;

@Handfish
Copy link

Thanks a lot @ShravanSunder!

export type ZodObjectMask<T extends ZodTypeAny> = Partial<Record<keyof z.infer<T>, true>>;

did the job.

@colinhacks
Copy link
Owner

Thanks this is great! This same functionality was added in #3255 and 81d2e89

It'll land in v3.23.

@colinhacks colinhacks closed this Apr 16, 2024
@oljimenez
Copy link
Author

oljimenez commented Apr 18, 2024

Thanks this is great! This same functionality was added in #3255 and 81d2e89

It'll land in v3.23.

🥳

But checking that PR i see that he only implemented on pick/omit, so is missing the functions partial/required. Should i open another PR for partial/required methods?

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.

5 participants