How to set envs as number in t3stack

I would like the envs to be parsed as numbers/booleans straight from env.mjs, how to approach that?
23 Replies
Diogo
Diogo•2y ago
instead of z.string() do this https://zod.dev/?id=numbers
GitHub
TypeScript-first schema validation with static type inference
TypeScript-first schema validation with static type inference
Diogo
Diogo•2y ago
GitHub
TypeScript-first schema validation with static type inference
TypeScript-first schema validation with static type inference
Neto
Neto•2y ago
{
some_number: z.coerce.number(proccess.env.SOME_NUMBER),
some_boolean: z.coerce.boolean(process.env.SOME_BOOLEAN)
}
{
some_number: z.coerce.number(proccess.env.SOME_NUMBER),
some_boolean: z.coerce.boolean(process.env.SOME_BOOLEAN)
}
Jaaneek
Jaaneek•2y ago
This will probably not work, I doubt it will parse itself thx, I was looking for that!
Jaaneek
Jaaneek•2y ago
I'm doing something wrong? Where should I put it?
Jaaneek
Jaaneek•2y ago
Those values are defined as numbers
Neto
Neto•2y ago
try this
Neto
Neto•2y ago
adding the number as possible type
Jaaneek
Jaaneek•2y ago
/**
* You can't destruct `process.env` as a regular object in the Next.js edge runtimes (e.g.
* middlewares) or client-side so we need to destruct manually.
*
* @type {Record<keyof z.infer<typeof server> | keyof z.infer<typeof client>, string | number | undefined>}
*/
/**
* You can't destruct `process.env` as a regular object in the Next.js edge runtimes (e.g.
* middlewares) or client-side so we need to destruct manually.
*
* @type {Record<keyof z.infer<typeof server> | keyof z.infer<typeof client>, string | number | undefined>}
*/
Does not work 😦
Type 'ZodNumber' is not assignable to type 'string | number | undefined'
Type 'ZodNumber' is not assignable to type 'string | number | undefined'
Neto
Neto•2y ago
i guess that env variables can't be numbers like really won't allow
nexxel
nexxel•2y ago
env variables can never be numbers they can only be strings even if the string contains numbers you can use a transform maybe
Neto
Neto•2y ago
at best you can transform at runtime
Jaaneek
Jaaneek•2y ago
I don't see a reason why we shouldnt be able to validate and transform them in the env.mjs file and then use them as booleans/numbers
nexxel
nexxel•2y ago
try transforming but validating can only ensure its a string cause thats what it is
Jaaneek
Jaaneek•2y ago
Imo this is a good feature to potentially support it out of box with t3stack & add that to docs
nexxel
nexxel•2y ago
@julius @cje thoughts?
julius
julius•2y ago
what's the feature request? env variables are always strings - if you want them as a number or boolean, use .transform:
const server = z.object({
NODE_ENV: z.enum(["development", "test", "production"]),
SOME_NUM: z.string().transform(Number).pipe(z.number()),
SOME_BOOL: z.string().transform((str) => str && str !== "false" && str !== "0"),
});
const server = z.object({
NODE_ENV: z.enum(["development", "test", "production"]),
SOME_NUM: z.string().transform(Number).pipe(z.number()),
SOME_BOOL: z.string().transform((str) => str && str !== "false" && str !== "0"),
});
JSON.stringify({ processEnv: process.env, env: env }, null, 4) then outputs this:
julius
julius•2y ago
getting some nasty error about the stypes not overlapping though... jsdoc is weird
julius
julius•2y ago
they are correctly inferred though
Jaaneek
Jaaneek•2y ago
Would be great to have it typed correctly without any errors out of box imo,
const server = z.object({
NODE_ENV: z.enum(["development", "test", "production"]),
SOME_NUM: z.string().transform(Number).pipe(z.number()),
SOME_BOOL: z.string().transform((str) => str && str !== "false" && str !== "0"),
});
const server = z.object({
NODE_ENV: z.enum(["development", "test", "production"]),
SOME_NUM: z.string().transform(Number).pipe(z.number()),
SOME_BOOL: z.string().transform((str) => str && str !== "false" && str !== "0"),
});
This example also could be potentially added into the docs imo
julius
julius•2y ago
can you do as unknown as SomeType in jsdoc? or maybe the underlying type is wrong
Jaaneek
Jaaneek•2y ago
Could you point where should I do it?
julius
julius•2y ago
On this typeerrror
Want results from more Discord servers?
Add your server