-
-
Notifications
You must be signed in to change notification settings - Fork 94
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
Persist default values when skipping validation #291
base: main
Are you sure you want to change the base?
Conversation
The latest updates on your projects. Learn more about Vercel for Git ↗︎
|
@Bartek532 is attempting to deploy a commit to the t3-oss Team on Vercel. A member of the Team first needs to authorize it. |
const getDefaults = <Schema extends AnyZodObject>(schema: Schema) => { | ||
return Object.fromEntries( | ||
Object.entries(schema.shape).map(([key, value]) => { | ||
if (value instanceof ZodDefault) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
oh i actualy like this, but don't think it'll be supported with this change: #299 ://
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
unfortunately no - you could possibly add a getDefault
option that receives a given schema and returns a default if it can get one?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
an alternative would be something like
createEnv({
server: {
FOO: v.optional(v.string)
},
runtimeEnv: {
FOO: process.env.FOO ?? "DEFAUTL"
}
})
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd bet for @EskiMojo14 solution, as the goal should be to avoid declaring the same thing twice ;)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i've had a think and a play around, and ended up with this:
import { createEnv } from "@t3-oss/env-core";
import { v } from "valibot";
export const env = createEnv({
server: {
SKIP_AUTH: v.optional(v.boolean(), false),
EMAIL: v.optional(v.pipe(v.string(), v.email()), "[email protected]"),
PASSWORD: v.optional(v.pipe(v.string(), v.minLength(1)), "password"),
SECRET_NUMBER: v.pipe(
v.string(),
v.transform((s) => parseInt(s, 10)),
v.number(),
)
},
// ...
skipValidation: true,
getUnvalidatedEnv: (env, schema) => ({
...v.getDefaults(schema),
...env,
SECRET_NUMBER: typeof env.SECRET_NUMBER === "string" ? parseInt(env.SECRET_NUMBER, 10) : v.getDefault(schema.entries.SECRET_NUMBER),
}),
});
Requiring a getUnvalidatedEnv
callback when using skipValidation allows for the user to decide how to get the env shape they're expected to have, and we can check it against what the schema normally returns. The user can of course still bury their head in the sand, it's just a little more inconvenient to do so:
export const env = createEnv({
// ...
skipValidation: true,
getUnvalidatedEnv: env => env as any
});
I can't open the PR yet since it's based on the branch for #313, but once that's finalised I'll get that open so we can have a place to discuss it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@EskiMojo14 really liked your solution, but just wondering if we should introduce new property that's required, I think that it could be optional and by default, we'll return all the default values, wdyt?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Bartek532 done - had the same thought
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good! @EskiMojo14 could you open the PR for this? We can then review together with @juliusmarminge ;)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
#313 isn't merged, so if I opened the PR right now it would be on my fork not this one.
Closing: #266
When validation is skipped using
skipValidation: true
, the default values from zod schemas would still be taken into account.