colinhacks/zod

Using function arg values in return validator

Superpat opened this issue · 2 comments

I think what I'd like to do is not currently possible in zod, but I'd love to be proven wrong.

The basic idea is the ability to use the function arguments in the returns validator.

So let's say I have an identity function, I'd like to do the following pseudo code:

z.function().args(z.any()).returns((arg1, result) => { arg1 === result }).implements((arg1) => { arg1 })

Hi, what usecase would this have? Do you have an example of a schema with usage so we can understand this feature.

Hi, not sure I understand the question. The identity function is a use case in itself no ? But there are other cases, like forcing the output to contain the input.