t3-oss / t3-env

https://env.t3.gg
MIT License
2.78k stars 86 forks source link

Accesing server-side env from client error thrown when importing Zod types generated from Drizzle schema #178

Closed zanzlender closed 9 months ago

zanzlender commented 9 months ago

I encountered an unexpected problem while I was playing with T3, drizzle and the drizzle-zod package. Maybe I am wrong and it's supposed to be like this, correct me if that is so. The example shows my setup - simplified for showcase.

Drizzle schema (schema.drizzle.ts)

import { createInsertSchema } from "drizzle-zod";

export const users = createTable("user", {
  id: varchar("id", { length: 255 })
    .notNull()
    .primaryKey()
    .$defaultFn(() => createId()),
  name: varchar("name", { length: 255 }),
  email: varchar("email", { length: 255 }).notNull(),
  vat: varchar("vat", { length: 255 }),
  iban: varchar("vat", { length: 255 }),
});

export const createUserSchema = createInsertSchema(users);

export const zFinanceSettingsSchema = createUserSchema.pick({
  iban: true,
  vat: true
})

export type FinanceSettingsSchema = z.infer<typeof zFinanceSettingsSchema >

Usage

"use client"
...
import {
  type FinanceSettingsSchema ,
  zFinanceSettingsSchema ,
} from "~/server/db/schema";

export default function FinanceSettingsForm() {
  const form = useForm<FinanceSettingsSchema >({
    resolver: zodResolver(zFinanceSettingsSchema ),
    defaultValues: {},
  });

  return <>...</>
}

Problem

Both createUserSchema and zFinanceSettingsSchema are normal Zod types. However, when I want to use it inside a Client component - just import that type to put inside resolver for React Hook Forms - I get this error:

image

Maybe I am missing something, but I don't see a reason to throw an error if I only import the type and do not use any variable or drizzle, which also won't be used at buildtime.

zanzlender commented 9 months ago

This was my bad... I defined the "projectName" variable in Drizzle config, then imported that in the schema, which t3-env then picked up as using on the client. Although I think my point was still kinda valid, this was a problem created by me and I don't believe it's worth the implementation time...