-
-
Notifications
You must be signed in to change notification settings - Fork 168
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
Conversation
@@ -69,7 +69,7 @@ export const zodResolver: Resolver = | |||
|
|||
return { | |||
errors: {} as FieldErrors, | |||
values: data, | |||
values: resolverOptions.rawValues ? values : data, |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I made a reproduction
- Go to https://kitchen-sink-git-repro-trpc.vercel.app/react-hook-form
- Open network tab
- 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:
dateStr
gets validated asDate
on clientDate
is sent to server- Server receives
Date
, tries to validate as a string --> ❌
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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:
Line 50 in 350d255
return { values, errors: {} }; resolvers/typanion/src/typanion.ts
Line 37 in 350d255
return { values, errors: {} }; Line 45 in 350d255
return { values, errors: {} }; return { values, errors: {} };
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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. ❤️
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- Do you want this to be done ahead of merging this PR?
- What behavior do you want as default?
There was a problem hiding this comment.
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.
"values": Object { | ||
"accessToken": "accessToken", | ||
"birthYear": 2000, | ||
"dateStr": 2020-01-01T00:00:00.000Z, |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
There was a problem hiding this 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
@jorisre merge when you feel comfortable ❤️ This should be a minor version and we can follow up to patch the other resolvers as well. |
🎉 This PR is included in version 2.8.9 🎉 The release is available on: Your semantic-release bot 📦🚀 |
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.