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

Add ctx object to Zod resolver #724

Open
ericlifs opened this issue Nov 1, 2024 · 1 comment
Open

Add ctx object to Zod resolver #724

ericlifs opened this issue Nov 1, 2024 · 1 comment

Comments

@ericlifs
Copy link

ericlifs commented Nov 1, 2024

Zod's resolver does not support passing useForm's context as to superRefine zod method.

Proposed solution

After reading useForm's documentation I have noticed you can have an optional context object on your form so it then gets passed to the resolver as the second argument.

Unfortunately after debugging this for a while I have noticed that the context gets omitted and it is never sent to Zod's resolver:
image

The context works perfectly on the useForm resolver:
image

But the context is never set on the zod resolver:
image

Additional context

image image

I hope I expressed myself well, otherwise I can explain the problem further.

Thanks!

@immitsu
Copy link

immitsu commented Jan 15, 2025

At the moment there is no possibility to pass down сontext to zod schema. See discussion.

Probably it is worth mentioning in the docs:

"This context object is mutable and will be injected into the resolver's second argument if possible or Yup validation's context object."

You can store data externally as context and import it in validation.

Or – I chose this option – use something like meta fields (just keep, don't render: { _hasBillingStates, field1, field2 }) and validate conditionally if need it via discriminated-unions.

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

No branches or pull requests

2 participants