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•15mo 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•15mo ago
GitHub
TypeScript-first schema validation with static type inference
TypeScript-first schema validation with static type inference
Neto
Neto•15mo 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•15mo ago
This will probably not work, I doubt it will parse itself thx, I was looking for that!
Jaaneek
Jaaneek•15mo ago
I'm doing something wrong? Where should I put it?
Jaaneek
Jaaneek•15mo ago
Those values are defined as numbers
Neto
Neto•15mo ago
try this
Neto
Neto•15mo ago
adding the number as possible type
Jaaneek
Jaaneek•15mo 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•15mo ago
i guess that env variables can't be numbers like really won't allow
nexxel
nexxel•15mo 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•15mo ago
at best you can transform at runtime
Jaaneek
Jaaneek•15mo 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•15mo ago
try transforming but validating can only ensure its a string cause thats what it is
Jaaneek
Jaaneek•15mo ago
Imo this is a good feature to potentially support it out of box with t3stack & add that to docs
nexxel
nexxel•15mo ago
@julius @cje thoughts?
julius
julius•15mo 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•15mo ago
getting some nasty error about the stypes not overlapping though... jsdoc is weird
julius
julius•15mo ago
they are correctly inferred though
Jaaneek
Jaaneek•15mo 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•15mo ago
can you do as unknown as SomeType in jsdoc? or maybe the underlying type is wrong
Jaaneek
Jaaneek•15mo ago
Could you point where should I do it?
julius
julius•15mo ago
On this typeerrror
Want results from more Discord servers?
Add your server
More Posts