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

Feature Request: Support for passing zod Schema instead of object #176

Closed
shkreios opened this issue Jan 29, 2024 · 1 comment
Closed

Feature Request: Support for passing zod Schema instead of object #176

shkreios opened this issue Jan 29, 2024 · 1 comment

Comments

@shkreios
Copy link

shkreios commented Jan 29, 2024

Summary

This issue proposes an enhancement to the current configuration system by allowing the passing of complete Zod schemas, rather than just object structures. This feature aims to provide greater flexibility and precision in validating environment variables, especially in scenarios requiring conditional validations and multiple configurations.

Detailed Explanation

In applications, which deploy to many different environments, with different set of environment variables having more power over the validation process in terms of which env variables are needed together or which should not nether be used together.

Proposed Solution

From what I can tell, this should be as easy as adapting the types to require a ZodSchema instead of Record<any,ZodType>. The tricky part could be to get the ErrorMessage type working.

Example:

Take, 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 GOOGLE_CLIENT_ID is provided, it's imperative that GOOGLE_CLIENT_SECRET is also present, and not just marked as optional.

Example schema

const 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 type

type ServerSchemaType = {
  NODE_ENV: "development" | "test" | "production";
  NEXTAUTH_SECRET?: string | undefined;
} & (
  | {
      GOOGLE_CLIENT_ID: string;
      GOOGLE_CLIENT_SECRET: string;
    }
  | {
      RESEND_API_KEY: string;
    }
);
@juliusmarminge
Copy link
Member

duplicate of #169

@juliusmarminge juliusmarminge closed this as not planned Won't fix, can't repro, duplicate, stale Feb 7, 2024
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

No branches or pull requests

2 participants