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: ability to specify unique values #191

Closed
hichemfantar opened this issue Feb 10, 2024 · 2 comments
Closed

feat: ability to specify unique values #191

hichemfantar opened this issue Feb 10, 2024 · 2 comments

Comments

@hichemfantar
Copy link

hichemfantar commented Feb 10, 2024

What's the recommended approach for validating unique env vars?

my use case is application ports where i want to make sure multiple apps can't have the same port.

My proposal is a way to specify a prefix for unique values.

e.g. PORT_ADMIN_DASHBOARD (all env vars that start with PORT_ must be unique)

I'm thinking a special env variable UNIQUE_VAR_PREFIXES that contains comma seperated prefixes that should be unique
e.g. UNIQUE_VAR_PREFIXES=PORT,OTHER_UNIQUE_PREFIX
when t3-env runs, it searches for vars with these prefixes and makes sure they're all unique

the vars below will throw an error

PORT_ADMIN_DASHBOARD=3000
PORT_CLIENT_DASHBOARD=3000
@juliusmarminge
Copy link
Member

Should be solved with a .refine with #169

@juliusmarminge
Copy link
Member

juliusmarminge commented Feb 10, 2024

Can also do it today with just a check after the createEnv call, no?

const env = createEnv({
  server: {
    PORT_1: z.coerce.number(),
    PORT_2: z.coerce.number(),
  },
  runtimeEnv: process.env,
});

const allPorts = [env.PORT_1, env.PORT_2];
if (new Set(allPorts).size !== allPorts.length) {
  throw new Error("All ports must be unique, got " + JSON.stringify(allPorts));
}

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