Envario is a command-line tool for validating environment variables against a schema. It helps ensure that your .env files are well-structured and contain all the necessary values in the right format.
- Validate .env files against a customizable schema.
- Support for required fields, default values, and custom validation functions.
- Strict and tolerant validation modes.
- Clear error reporting and user-friendly CLI.
- Can be imported directly into your Node.js projects for validation of environment variables.
To install Envario globally, use the following npm command:
npm install -g envario
To install it locally as a development dependency:
npm install --save-dev envario
Envario provides a CLI that you can use to validate your .env
file base on
JSON schema.
To validate your .env
file, run:
envario --env ./path/to/.env --schema ./path/to/schema.json
--env
: (optional) The path to your .env file. Defaults to .env.--schema
: The path to the JSON schema file.--mode
: (optional) Validation mode. Use strict (default) for failing immediately on errors, or tolerant to collect all errors and show them at once.
To view the CLI help
envario --help
To check the version of Envario:
envario --version
A schema defines the environment variables you expect to see in your .env
file
and their requirements
Example schema (schema.json
)
{
"PORT": { "required": true },
"NODE_ENV": { "required": true, "default": "development" },
"DEBUG": { "required": false },
"API_KEY": { "required": true, "validate": "startsWithSk" }
}
required
: Marks the variable as required.default
: Provides a default value if the variable is missing.validate
: A custom validation function (e.g., ensure API_KEY starts with "sk-").
Envario supports custom validation functions to add more flexibility in how environment variables are validated. For example, you can create a custom function to check if a value starts with a specific prefix.
Example of a custom validator function (startsWithSk
):
function startsWithSk(value) {
if (!value.startsWith("sk-")) {
throw new Error("API_KEY must start with 'sk-'");
}
return value;
}
You can add these custom functions in your schema by referencing the function name as a string.
You can also use Envario’s validation directly in your Node.js applications by
importing the validateEnv
function.
const {validateEnv} from "envario"
You can define a schema just like in the CLI example, which includes
required fields
, default values
, and custom validation
const schema = {
PORT: { required: true },
NODE_ENV: { required: true, default: "development" },
DEBUG: { required: false },
API_KEY: { required: true, validate: startsWithSk },
};
Call validateEnv
with your schema
to validate environment variables. This
will throw an error if any required environment variables are missing or if the
values don't meet the validation criteria.
try {
const envVars = validateEnv(schema);
console.log("Environment variables are valid:", envVars);
} catch (error) {
console.error("Environment variable validation failed:", error.message);
}
if your .env
file is located at ./.env
, and you want to validate it with the
default schema:
envario --schema ./schema.json
if you want to validate .env
file located elsewhere:
envario --env ./config/.env --schema ./config/schema.json
if you want to gather all validation errors instead of failing on the first error:
envario --env ./config/.env --schema ./config/schema.json --mode tolerant
Please update the contributing section in your README.md file to the following:
Thank you for considering contributing to Envario! We welcome contributions from the community and are excited to work with you.
Please see CONTRIBUTING.md for details on how to contribute.
Please note that this project is released with a Contributor Code of Conduct. By participating in this project, you agree to abide by its terms.
If you have any questions or need further assistance, feel free to open an issue or contact the maintainers.
Thank you for your contributions!