Julien-R44 / vite-plugin-validate-env

✅ Vite plugin for validating your environment variables
MIT License
164 stars 6 forks source link

fix(zod): transform/preprocess from string to array does not work #13

Closed nicobao closed 9 months ago

nicobao commented 1 year ago

Hi!

Thanks for the lib. I tried to transform my config param string "acme.com, example.com" to an array of string: ["acme.com", "example.com"] with zod and this lib. Typescript and my IDE say it works, but typeof import.meta.env.VITE_AUTHORIZED_FQDNS returns string and import.meta.env.VITE_AUTHORIZED_FQDNS is the original config string :/

Here is the latest config I tried from env.ts:

  VITE_AUTHORIZED_FQDNS: z.preprocess(
    (val) =>
      String(val)
        .trim()
        .split(",")
        .filter((v) => validator.isFQDN(v)),
    z.array(z.string().nonempty()).nonempty()
  ),

I tried also with transform + refine - no luck.

KholdStare commented 11 months ago

@Julien-R44 Yes, this kills the usefulness of this plugin, because the types reported by the schema are a lie.

env is still Record<string, string>

https://github.com/Julien-R44/vite-plugin-validate-env/blob/main/src/validators/zod/index.ts#L21

Any way to actually store the correct type after transformation/preprocessing?

Julien-R44 commented 9 months ago

Hey! Sorry for taking so long. This is now fixed with 1.0.0

https://github.com/Julien-R44/vite-plugin-validate-env/releases/tag/v1.0.0