Persist default values when skipping validation#291
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.
oh i actualy like this, but don't think it'll be supported with this change: #299 ://
There was a problem hiding this comment.
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.
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.
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.
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()), "foo@bar.com"),
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.
@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.
Looks good! @EskiMojo14 could you open the PR for this? We can then review together with @juliusmarminge ;)
There was a problem hiding this comment.
#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.