Skip to main content

EnvValidator is a lightweight Python library that helps validate environment variables against a predefined schema.

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

  1. Reads the .env file (if available).
  2. Parses key-value pairs and trims whitespace.
  3. 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.
  4. 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:

  1. 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
    
  2. Build & install the project
    python setup.py build
    python setup.py install
    
  3. 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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

envvalidator-1.0.2.tar.gz (8.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

envvalidator-1.0.2-cp313-cp313-win_amd64.whl (47.4 kB view details)

Uploaded CPython 3.13Windows x86-64

File details

Details for the file envvalidator-1.0.2.tar.gz.

File metadata

  • Download URL: envvalidator-1.0.2.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

Hashes for envvalidator-1.0.2.tar.gz
Algorithm Hash digest
SHA256 a36f1d82198b2ccd8d41b2e91026a9a728c8ff8acb82e3ad6893597b55a6b6af
MD5 3ce5e32e49346e0782853997faf5df2c
BLAKE2b-256 d231ad76ae096423a98a44bffe0cd1f79c27b741008c05a888198672bc1ee141

See more details on using hashes here.

File details

Details for the file envvalidator-1.0.2-cp313-cp313-win_amd64.whl.

File metadata

File hashes

Hashes for envvalidator-1.0.2-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 b896f83852718cd7b7cf6163fcfe69beb957f6ae006999e69f93d1030e8592b2
MD5 469937a9fd2befd00b235c168b80d22f
BLAKE2b-256 56cc28d795cb5d032d59a44b29266198c5faf86f8e35d99c45dbf3ffd2ca1cb8

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page