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

feat: directly pass zod objects to server and client #272

Conversation

feder240516
Copy link

@feder240516 feder240516 commented Oct 8, 2024

fixes #169

Improvements:

  • Core package supports z.object
  • Next package supports z.object
  • This PR only extends support for plain z.object types, meaning that ZodObjects with transforms / refines / catchs are not supported. The same goes for other specialized types (z.union, z.discriminatedUnion, z.record).

Discussion:

  • I know that the main reason to use zod objects instead of the current implementation is to enable the power of refinements, unions and what not.
  • The main issue is that applying these transformations to a zod object will convert its type from ZodObject to a ZodEffects, and ZodEffects don't have a merge method which is needed by the library to combine the validations required by client, server and shared schemas.
  • I'm already researching what can be done, but for now this PR handles a case where only plain ZodObjects are accepted

Copy link

vercel bot commented Oct 8, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
t3-env ✅ Ready (Inspect) Visit Preview 💬 Add feedback Oct 8, 2024 1:44pm

Copy link

vercel bot commented Oct 8, 2024

@feder240516 is attempting to deploy a commit to the t3-oss Team on Vercel.

A member of the Team first needs to authorize it.

Copy link
Member

@juliusmarminge juliusmarminge left a comment

Choose a reason for hiding this comment

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

Any way we can get the errors on the properties instead of the entire object?

FOO_BAR: z.string(),
// @ts-expect-error - no FOO_ prefix
Copy link
Member

Choose a reason for hiding this comment

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

hmm having the error on the key is a bit nicer ux

@@ -16,12 +16,12 @@ function ignoreErrors(cb: () => void) {
test("server vars should not be prefixed", () => {
ignoreErrors(() => {
createEnv({
server: {
// @ts-expect-error - server should not have NEXT_PUBLIC_ prefix
// @ts-expect-error - server should not have NEXT_PUBLIC_ prefix
Copy link
Member

Choose a reason for hiding this comment

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

same here

server: {},
client: {
server: z.object({}),
// @ts-expect-error - no NEXT_PUBLIC_ prefix
Copy link
Member

Choose a reason for hiding this comment

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

and here

Copy link
Member

@juliusmarminge juliusmarminge left a comment

Choose a reason for hiding this comment

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

Any way we can get the errors on the properties instead of the entire object?

@feder240516
Copy link
Author

Any way we can get the errors on the properties instead of the entire object?

@juliusmarminge it's something that I've explored, but I don't think it's possible, due to the properties being now part of the zod schema constructor

@juliusmarminge
Copy link
Member

hey we added standard schema support #299 so i think this will need to be restarted / abandoned. i'll close it given the amount of conflicts but the issue is still up

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

Successfully merging this pull request may close these issues.

Allow for ZodObject to be passed directly to client/server keys instead of an object of ZodTypes
2 participants