envvalidator description
Project description
EnvValidator
EnvValidator is a lightweight Python library that helps validate environment variables against a predefined schema. It ensures that required variables exist, follow the correct type or format, and falls back to system environment variables when an .env file is missing.
Features
- Schema-Based Validation – Define expected environment variables and their types (string, int, bool, regex).
- Automatic Fallback – If no .env file is found, it retrieves values from system environment variables.
- Regex Support – Enforce custom formats (e.g., API keys, UUIDs).
- Error Handling – Provides clear error messages when validation fails.
- Lightweight & Fast – Minimal overhead with a simple API.
Requirements
- Python 3.x (recommended: 3.7 or higher)
Installation
Install the package using pip:
pip install envvalidator
Usage
Here is an example of how to use EnvValidator:
# Define the expected schema
schema = {
"DATABASE_URL": "string",
"DEBUG": "bool",
"PORT": "int",
"API_KEY": {"regex": r"^[A-Za-z0-9]{32}$"} # Ensure 32-character API key format
}
try:
ENV = envvalidator.validate_env(".env", schema)
print(ENV)
except ValueError as e:
print("Configuration error:", e)
Output Example
If .env contains:
DATABASE_URL=postgres://user:pass@localhost:5432/db
DEBUG=true
PORT=8080
API_KEY=1234567890abcdef1234567890abcdef
The output would be:
{
"DATABASE_URL": "postgres://user:pass@localhost:5432/db",
"DEBUG": "true",
"PORT": "8080",
"API_KEY": "1234567890abcdef1234567890abcdef"
}
If a required variable is missing or does not match the expected type, an error is raised.
How It Works
- Reads the .env file (if available).
- Parses key-value pairs and trims whitespace.
- Checks each variable against the schema:
- If defined in .env, it is validated.
- If missing, it looks in system environment variables.
- If validation fails, an error is raised.
- Returns a dictionary of validated environment variables.
Limitations
- Does not support deeply nested configurations (e.g., JSON-like structures).
- Assumes all values are stored as strings in environment variables.
Contributing
Contributions are welcome! Feel free to open an issue or submit a pull request if you have suggestions or improvements.
Local Development
To set up a local development environment, follow these steps:
- Create and activate a virtual environment:
python -m venv venv source venv/bin/activate # macOS/Linux venv\Scripts\activate # Windows pip install -r requirements.txt
- Build & install the project
python setup.py build python setup.py install
- Run an example script to test:
python examples/example.py
The module is now installed in the virtual environment. You can test it by running the example script:
python examples/example.py
License
This project is licensed under the Apache-2.0 License. See the LICENSE file for details.
Author
Developed by Qiyaya
Acknowledgements
Thanks to the Python (and ...) development communities for providing tools and resources to make this project possible.
This styling ensures clarity, proper sectioning, and good readability. Let me know if you`d like any further adjustments!
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file envvalidator-1.0.0.tar.gz.
File metadata
- Download URL: envvalidator-1.0.0.tar.gz
- Upload date:
- Size: 8.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b15fe2cfb0b545ace47f52f4804bf685c2aeb29044ffd76e5a794d04a89357c0
|
|
| MD5 |
ad47a5e663232736b8ab195603108f22
|
|
| BLAKE2b-256 |
9608abe378685f3f57781d266f3f28682794b7718e0f4c3a451f7e6a603e6dc4
|
File details
Details for the file envvalidator-1.0.0-cp313-cp313-win_amd64.whl.
File metadata
- Download URL: envvalidator-1.0.0-cp313-cp313-win_amd64.whl
- Upload date:
- Size: 45.7 kB
- Tags: CPython 3.13, Windows x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
858b791c489234898de3f971fb7b5dc5828cbde197e526b7e56242a4dce5d7fd
|
|
| MD5 |
b03b9c549af5c1380ad4add9c1220225
|
|
| BLAKE2b-256 |
697dd30fd2c30b41103c9c4daebb1950d17e7371e5d5c3e9c64f0c577cf2b78c
|