Validate your environment variables with Zod
Zod is the most famous validation library in the TypeScript ecosystem. With Zod, you create a schema and validate your data according to the schema. Observe the schema below:
This schema can be used to validate an object as follows:
Zod is capable of performing various types of validations on your data, so be sure to read the documentation for more details.
Validating Environment Variables
We can use Zod to validate the values present in process.env
and even process them before using the environment variables in our application. Usually, I like to create an environment.ts
file, as in the example below:
Then, just import the variable and use it throughout my application: