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

Proposal: ZodObject.omitIfExists() #2144

Closed
wants to merge 2 commits into from

Conversation

jussisaurio
Copy link
Contributor

I found myself wanting to predefine a set of keys that would always be omitted from certain schemas if present. Example scenario:

const OMIT_AUTOGENERATED = {
  id: true,
  uuid: true,
  createdAt: true,
  updatedAt: true
}

const someSchema = someSchemaRaw.omit(OMIT_AUTOGENERATED);
const someSchema2 = someSchema2Raw.omit(OMIT_AUTOGENERATED);

If there are certain "Raw schemas" (using the above nomenclature) that don't have all the properties defined in OMIT_AUTOGENERATED, then I can't use it on them.

With something like ZodType.omitIfExists() one could blanket-exclude a set of keys from schemas without necessarily requiring them to be present in all of them.

I found myself wanting to predefine a set of keys that would always
be omitted from certain schemas if present. Example scenario:

```
const OMIT_AUTOGENERATED = {
  id: true,
  uuid: true,
  createdAt: true,
  updatedAt: true
}

const someSchema = someSchemaRaw.omit(OMIT_AUTOGENERATED);
const someSchema2 = someSchema2Raw.omit(OMIT_AUTOGENERATED);
```

If there are certain "Raw schemas" (using the above nomenclature)
that don't have all the properties defined in OMIT_AUTOGENERATED,
then I can't use it on them.

With something like ZodType.omitIfExists() one could blanket-exclude
a set of keys from schemas without necessarily requiring them to
be present in all of them.
@netlify
Copy link

netlify bot commented Mar 5, 2023

Deploy Preview for guileless-rolypoly-866f8a ready!

Built without sensitive environment variables

Name Link
🔨 Latest commit 6d03867
🔍 Latest deploy log https://app.netlify.com/sites/guileless-rolypoly-866f8a/deploys/6404787790f95500087bb758
😎 Deploy Preview https://deploy-preview-2144--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 settings.

@colinhacks
Copy link
Owner

colinhacks commented Mar 6, 2023

Hm this is a decent argument for reverting the behavior introduced in #1564. I think I'm in favor of reverting, esp. since TypeScript itself doesn't care:

type asdf = Omit<{ a: string }, "b">;
// no error

I'm opposed to adding a new method with subtly different behavior, so it comes down to deciding how we want .omit() to behave. Thoughts @igalklebanov?

@igalklebanov
Copy link
Contributor

Let's revert it on all mask methods. It hurts compiler performance.

@jussisaurio
Copy link
Contributor Author

I let this stew overnight in my head and I also think it's not great to have two subtly different method names. This is a small convenience thing for me so I don't feel strongly about reverting to .omit() original behavior or leaving it as is based on that alone, but if the current implementation indeed carries a compile-time perf penalty, then I'd also lean towards reverting tbh.

@colinhacks
Copy link
Owner

colinhacks commented Mar 7, 2023

I've reverted #1564 in the latest patch release, so this should now work @jussisaurio

@colinhacks colinhacks closed this Mar 7, 2023
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.

None yet

3 participants