You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This Vite plugin allows you to validate your environment variables at build or dev time. This allows your build/dev-server to [fail-fast](https://en.wikipedia.org/wiki/Fail-fast) if your setup is misconfigured.
5
+
# vite-plugin-validate-env
6
6
7
-
No more CI to restart because you are missing an environment variable, or to realize after 10 minutes of debugging that you forgot a variable 🥲
7
+
A Vite plugin that validates your environment variables at **build** or **dev time**. This helps you catch misconfigurations early by [failing fast](https://en.wikipedia.org/wiki/Fail-fast). No more broken builds or 10 minutes of debugging just to realize you forgot a variable 🥲
8
8
9
-
## Features
10
-
- Validate your environment variables at **build time only**. No runtime overhead
11
-
- Totally type-safe
12
-
- Support [standard-schema](https://github.com/standard-schema/standard-schema), meaning you can use every libraries compatible with it ( Zod, Valibot, ArkType... )
13
-
- Parsing, validation and transformation of your variables
14
-
- Custom rules and error messages
9
+
---
10
+
11
+
## ✅ Features
12
+
13
+
* Validate environment variables **at build time only**, zero runtime overhead
14
+
* Fully **type-safe**
15
+
* Supports [standard-schema](https://github.com/standard-schema/standard-schema) — works with Zod, Valibot, ArkType, and more
16
+
* Built-in parsing, validation, and transformation
17
+
* Custom rules and error messages
18
+
19
+
---
15
20
16
21
## Installation
17
22
18
23
```sh
19
24
pnpm add -D @julr/vite-plugin-validate-env
20
25
```
21
26
22
-
## Usage
23
-
`vite-plugin-validate-env` plugin allows you to validate your env, either with a very simplified builtin validation lib, or with Zod in the most complex cases when you want a very strict validation.
27
+
## Basic Usage
28
+
29
+
You can use the plugin with the [built-in validator](https://github.com/poppinss/validator-lite) for simple use cases, or with libraries like Zod for more advanced schemas.
30
+
31
+
> [!TIP]
32
+
> I would recommend using a dedicated `env.ts` file to keep your Vite config clean and separate from your environment variable definitions. See the [Using a Dedicated `env.ts` Config File](#using-a-dedicated-envts-config-file) section for more details.
33
+
34
+
### Built-in Validator
24
35
25
-
### Plugin options
26
-
The easiest way to define the options is to directly define the scheme as follows:
In case you want to change some plugin options, in particular change the validator (for Zod), you have to set your options as follows:
50
+
### Standard Schema Validators
51
+
52
+
If you want to use Zod or another validator compatible with [standard-schema](https://github.com/standard-schema/standard-schema), pass the `validator` and `schema` manually:
VITE_APP_PORT: Schema.number({ message: 'You must set a port !' }),
99
-
100
-
// Custom validator
101
-
VITE_CUSTOM_VARIABLE: (key, value) => {
102
-
if (!value) {
103
-
thrownewError(`Missing ${key} env variable`)
104
-
}
105
-
106
-
if (value.endsWith('foo')) {
107
-
thrownewError('Value cannot end with "foo"')
108
-
}
109
-
110
-
returnvalue
111
-
},
112
-
}),
113
-
],
89
+
// Custom validator
90
+
VITE_CUSTOM: (key, value) => {
91
+
if (!value) thrownewError(`Missing ${key}`)
92
+
if (value.endsWith('foo')) thrownewError('Cannot end with "foo"')
93
+
returnvalue
94
+
},
114
95
})
115
96
```
116
97
117
-
## Standard Schema
98
+
## Using Standard Schema
118
99
119
-
[standard-schema](https://github.com/standard-schema/standard-schema) is basically an attempt to standardize the way we can use validation libraries. It means that you can use any library that is compatible with it. Zod, Valibot, ArkType are popular libraries that are compatible with it.
100
+
`standard-schema` provides a common interface for multiple validation libraries.
120
101
121
-
Here is an example of how to use it with the plugin:
102
+
Here’s how to use it with Zod, Valibot, or ArkType, or any other library that supports it.
By default, the plugin is looking for a file named `env.ts` at the root of your project. If you want to use a different file, you can specify the path to your file in the plugin options.
This will look for a file named `env.ts` in the `config` folder at the root of your project. Make sure to not include the file extension in the path as the plugin will automatically search for `.js`, `.ts` and other valid file extensions.
182
-
183
-
## Transforming variables
184
-
In addition to the validation of your variables, there is also a parsing that is done. This means that you can modify the value of an environment variable before it is injected.
185
-
186
-
Let's imagine the following case: you want to expose a variable `VITE_AUTH_API_URL` in order to use it to call an API. However, you absolutely need a trailing slash at the end of this environment variable. Here's how it can be done :
Now, in your client front-end code, when you call `import.meta.env.VITE_AUTH_API_URL`, you can be sure that it will always end with a slash.
223
-
224
146
## Typing `import.meta.env`
147
+
225
148
In order to have a type-safe `import.meta.env`, the ideal is to use the dedicated configuration file `env.ts`.
226
149
Once this is done, you would only need to add an `env.d.ts` in `src/` folder to augment `ImportMetaEnv` (as [suggested here](https://vitejs.dev/guide/env-and-mode.html#env-files) ) with the following content:
By using `env` instead of `import.meta.env` in your code, TypeScript will now throw an error if you try to access an unknown variable.
255
178
256
-
## Sponsors
257
179
258
-
If you like this project, [please consider supporting it by sponsoring it](https://github.com/sponsors/Julien-R44/). It will help a lot to maintain and improve it. Thanks a lot !
180
+
## Transforming Variables
181
+
182
+
You can also **transform** values during parsing :
183
+
184
+
### Built-in:
185
+
186
+
```ts
187
+
VITE_API_URL: (key, value) => {
188
+
if (!value) thrownewError(`Missing ${key}`)
189
+
returnvalue.endsWith('/') ?value:`${value}/`
190
+
}
191
+
```
192
+
193
+
### With Zod:
194
+
195
+
```ts
196
+
VITE_API_URL: z.string().transform((v) =>
197
+
v.endsWith('/') ?v:`${v}/`
198
+
)
199
+
```
200
+
201
+
See the documentation of your validator for more details.
202
+
203
+
## 💖 Sponsors
204
+
205
+
If you find this useful, consider [sponsoring me](https://github.com/sponsors/Julien-R44)! It helps support and maintain the project 🙏
0 commit comments