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 ability to skip zod's parsing #401

Merged
merged 2 commits into from May 15, 2022
Merged

Conversation

KATT
Copy link
Contributor

@KATT KATT commented May 12, 2022

When using tRPC, it's common to use the same validation schema on the server as the client. Therefore, it's good to be able to send the unparsed raw values from the client to the server.

Maybe this should be the default behavior, unsure if it's intentional to be as it is.

@@ -69,7 +69,7 @@ export const zodResolver: Resolver =

return {
errors: {} as FieldErrors,
values: data,
values: resolverOptions.rawValues ? values : data,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unsure if it was ever intentional to pass the parsed output here.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR, it's intentional to return what's provided by the resolvers. What's the issue that brings?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I made a reproduction

  1. Go to https://kitchen-sink-git-repro-trpc.vercel.app/react-hook-form
  2. Open network tab
  3. Fill in form
  • Expected: submits successfully
  • Actual doesn't work

Highlighted inline here trpc/examples-kitchen-sink#36 on why it doesn't work - it's because the zod resolver coerces the values before sending them to the server which will then try to validate them based on the input:

  1. dateStr gets validated as Date on client
  2. Date is sent to server
  3. Server receives Date, tries to validate as a string --> ❌

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks for the context. rawValues option I couldn't find in zod's doc do you have a reference?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With raw just means that we're not using zod's parsed output

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had a look around and there's inconsistency how the resolvers behave in terms of this

Here's a few that return the raw value as default:

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you want to avoid breaking changes, we can't have a default behavior for this. I think it's better to only add it to zod for now.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

great find 👍 let's keep them consistent. ❤️

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. Do you want this to be done ahead of merging this PR?
  2. What behavior do you want as default?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The default behavior should return by what returned by the resolver

rawValues: false

and users can opt in to return rawValues if they want the values to return by hook form. I am happy to merge this PR to get started just let @jorisre do a review on this as well.

@bluebill1049 bluebill1049 requested a review from jorisre May 13, 2022 08:23
"values": Object {
"accessToken": "accessToken",
"birthYear": 2000,
"dateStr": 2020-01-01T00:00:00.000Z,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You see here @bluebill1049, the default behavior is to apply the transforms which will give a Date here and not a string

@KATT KATT requested a review from bluebill1049 May 13, 2022 09:27
Copy link
Member

@bluebill1049 bluebill1049 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Copy link
Member

@jorisre jorisre left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM ! Thank you for the PR @KATT

@bluebill1049
Copy link
Member

bluebill1049 commented May 13, 2022

@jorisre merge when you feel comfortable ❤️ This should be a minor version and we can follow up to patch the other resolvers as well.

@jorisre jorisre merged commit 259b4dc into react-hook-form:master May 15, 2022
@github-actions
Copy link
Contributor

🎉 This PR is included in version 2.8.9 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants