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
Allow for ZodObject to be passed directly to client
/server
keys instead of an object of ZodTypes
#169
Comments
Closing as you can just use /* External file*/
export const ServerSchema = z.object({
FOO: z.string().required()
});
export const ClientSchema = z.object({
BAR: z.string().required()
});
/* env.mjs */
const env = create schema({
server: ServerSchema.shape,
client: ClientSchema.shape
}); |
I believe there's significant value in considering the capability to pass a complete Zod schema, as opposed to merely an object structure, in our configuration. This feature would offer greater flexibility, particularly in complex scenarios where multiple conditional validations are necessary. ExampleTake, for instance, an app that allows a different set of authentication providers. In such cases, it's crucial to ensure that correlated environment variables are collectively defined. For example, if Example schemaconst GoogleAuthSchema = z.object({
GOOGLE_CLIENT_ID: z.string(),
GOOGLE_CLIENT_SECRET: z.string(),
});
const ResendSchema = z.object({
RESEND_API_KEY: z.string(),
});
const ServerSchema = z
.object({
NODE_ENV: z.enum(["development", "test", "production"]).default("development"),
NEXTAUTH_SECRET: process.env.NODE_ENV === "production" ? z.string() : z.string().optional(),
})
.and(z.union([GoogleAuthSchema, ResendSchema])); Resulting env typetype ServerSchemaType = {
NODE_ENV: "development" | "test" | "production";
NEXTAUTH_SECRET?: string | undefined;
} & (
| {
GOOGLE_CLIENT_ID: string;
GOOGLE_CLIENT_SECRET: string;
}
| {
RESEND_API_KEY: string;
}
); |
I opened a new issue #176 |
@shkreios I could simply reopen this one, if you'd like |
If thats possible, yes feel free to, maybe you can copy the new issue text into an updated request section or so |
Any updates here? I would appreciate this feature, as I want to utilize super refine to validate some variables to be required only if another one is a certain value. |
feel free to open a PR - iirc the reason why i did an object was cause inferring the shape didn't work properly with z.object() . Don't try to make it backwards compat, lib is still on |
Currently in
@t3-oss/env-nextjs
you are meant to define your env as follows:However, there are times where you might want to define these ZodObjects outside of your
env.mjs
file and simply pass them:This however does not work and the resulting
env
has typeRecord<string, {}>
because it wasn't expecting a ZodObject. It would be nice if we could pass pre-defined ZodObjects to these keys and retain type safety.The text was updated successfully, but these errors were encountered: