question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Schemas generated by refine do not have the omit method (or that is what typescript says)

See original GitHub issue

Hello, first of all, thanks for Zod, it’s fantastic. According to typescript, the objects that have been refined do not have the omit method. To reproduce you can try something as simple as this

const schema = z.object({ name: z.string(), age: z.number() }).refine(() => true);
const newSchema = schema.omit({ name: true }) // <- omit does not exist 

Issue Analytics

  • State:closed
  • Created a year ago
  • Reactions:2
  • Comments:5

github_iconTop GitHub Comments

1reaction
danielo515commented, Jul 27, 2022

Is this a bug or expected behavior? If it is the latter, can it we changed?

0reactions
scotttrinhcommented, Aug 19, 2022

Yeah, I know this seems not optimal, but it keeps everything explicit in a way that arbitrarily adding effects on defined schemas did not. There is a whole long history and context here: https://github.com/colinhacks/zod/issues/264 but here’s a bit of a snippet of the issue we’re trying to avoid:

Consider using this method on a transformer:

stringToNumber.transform(/* transformation_func */);

What type signature do you expect for transformation_func?

Most would probably expect (arg: number)=>number. Some would expect (arg: string)=>string. Neither of those are right; it’s (arg: number)=>string. The transformation function expects an input of number (the Output of stringToNumber) and a return type of number (the Input of stringToNumber). This type signature is a natural consequence of a series of logical design decisions, but the end result is dumb. Intuitively, you should be able to append .transform(val => val) to any schema. Unfortunately due to how transformers are implemented, that’s not always possible.

and

The fact that I incorrectly implemented both .transform and .default isn’t even the problem. The problem is that transformers make it difficult to write any generic functions on top of Zod (of which .transform and .default are two examples). Others have encountered similar issues. https://github.com/colinhacks/zod/issues/199 and https://github.com/colinhacks/zod/issues/213. are more complicated examples of how the current design of transformers makes it difficult to write any generic functions on top of Zod. Nested transformers in particular are a minefield.

Also consider this:

z.object({ type: z.literal("make me a string") }).transform(() => "i'm a string");

Obviously the object methods wouldn’t make sense here, but if you had a chain of different transform, you suddenly have to keep track of when they become object-like or array-like or promise-like or primitive-like, etc.


My personal recommendation is to separate out the input validation logic from any “effects” as a matter of principle when writing schemas. You can still just export the logical schemas most of the time, but in cases where you need to extend and apply effects to those schemas, either write a new schema in that same module, or export the base schema and effects to reassemble however you need.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Untitled
What is Zod Zod is a TypeScript-first schema declaration and validation library. ... Currently there is no support for Date or bigint literals...
Read more >
Why does Zod make all my schema fields optional?
I am using Zod inside my Express & TypeScript ...
Read more >
TypeScript schema validation with Zod - Iskander Samatov
Documentation: A good schema validation library will provide accurate type definitions for the data structures you use. Type definitions can be ...
Read more >
Documentation - Narrowing - TypeScript
Argument of type 'string | number' is not assignable to parameter of type ... of the function in a truthy check, but this...
Read more >
The Seven Sources of Unsoundness in TypeScript
The static types may or may not have anything to do with real ... Another is to generate JSON Schema from your TypeScript...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found