-
-
Notifications
You must be signed in to change notification settings - Fork 86
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 |
If there's a technical limitation to z.object, I wonder if there's an innovative workaround. I'm thinking, what if there were another parameter: E.g. given this createEnv call: export const env = createEnv({
runtimeEnv: process.env,
emptyStringAsUndefined: true,
server: {
NODE_ENV: z.enum(['development', 'test', 'production']),
url: z.string(),
otherStuff: z.string().optional(),
},
discriminatedUnions: {
NODE_ENV: {
development: {
url: z.literal('http://localhost'),
},
test: {
url: z.literal('https://test.example.com'),
},
production: {
url: z.literal('https://example.com'),
otherStuff: z.string(),
},
}
}
});
When parsed, the resulting environment object would have this type:
```typescript
type Env =
| {
NODE_ENV: 'production',
url: z.literal('https://example.com'),
otherStuff: string,
}
| (
& { otherStuff?: string | undefined }
& (
| {
NODE_ENV: 'development',
url: 'http://localhost',
}
| {
NODE_ENV: 'test',
url: 'https://test.example.com',
}
)
) (The convoluted way of representing that type is intentional, intended to represent the type I would expect to see given how this would likely need to be implemented, e.g. with stuff like In essence, this would effectively mean parsing the environment once with the top-level validation, then creating a discriminated union parser (non-strict) that a runs secondary validation and unions the resulting types. The need for secondary validation is somewhat mitigated by using the more performant ZodDiscriminatedUnion. Worth noting: We would need type magic to validate that the top-level keys of the "discriminatedUnions" object are valid, and we would need additional type magic to ensure that the keys of each "discriminatedUnion" object are assignable to the type asserted by the primary validation schema, and to ensure that the sub-schemas provided represent valid subsets of the primary validation schema. |
No it should be doable. I just recall it being a pain validating the shape's keys when the input was a ZodObject. Should be doable but I don't see it as enough a benefit to justify the time it would take to dig into it now. If anyone has time and need this feature feel free to submit a PR |
I think I may be able to make it work, though it will take some time (correct type inference is hard). Btw, this issue only talks about |
Yea everything. |
@feder240516 Any way I can help? I'm pretty nifty with type inference, and could certainly use this for work! |
@helmturner I think I've finished with the core package, however the library also includes a next package, and that's where I'm currently having problems. I'll polish and upload my partial PR tonight (it's 7AM here) so you can give it a look |
@helmturner I've just uploaded a draft PR, as you can see next package doesn't work, but core package works like a charm Edit: While uploading and polishing some details, I found the origin of the issue, so I think the PR is now complete |
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: