Skip to main content

Validate your environment variables before your app crashes

Project description

envguard

Validate your environment variables before your app crashes

PyPI Python Zero deps License


The Problem

Your .env file has a typo. You won't know until your app crashes at 3 AM in production.

DATABSE_URL=...   # typo → app starts → crashes 2 hours later on first DB call
SECRET_KEY=       # empty → auth silently broken
DEBUG=maybe       # not a boolean → who knows what happens

The Solution

pip install envguard-check

Check everything before your app starts:

from envguard import guard

# App won't start until all vars are valid
guard(
    DATABASE_URL="url",
    REDIS_URL="url",
    SECRET_KEY="str",
    DEBUG="bool",
    PORT="int",
)

If anything is wrong, you get a clear error and the app refuses to start:

Environment validation failed:

  ✗ MISSING: DATABASE_URL is required but not set
  ✗ TYPE: PORT='abc' is not a valid integer
  ✗ TYPE: DEBUG='maybe' is not a valid boolean

  3 error(s) found. Fix your .env file and try again.

Features

Type Validation

from envguard import check_env, EnvVar

check_env([
    EnvVar(name="PORT", type="int"),           # must be integer
    EnvVar(name="RATE", type="float"),          # must be float
    EnvVar(name="DEBUG", type="bool"),          # true/false/1/0/yes/no
    EnvVar(name="API_URL", type="url"),         # must start with http(s)://
    EnvVar(name="ADMIN_EMAIL", type="email"),   # must contain @ and domain
])

Choices

check_env([
    EnvVar(name="ENV", choices=["development", "staging", "production"]),
    EnvVar(name="LOG_LEVEL", choices=["DEBUG", "INFO", "WARNING", "ERROR"]),
])

Pattern Matching

check_env([
    EnvVar(name="STRIPE_KEY", pattern=r"^sk_(live|test)_"),
    EnvVar(name="AWS_REGION", pattern=r"^[a-z]{2}-[a-z]+-\d$"),
])

Defaults

check_env([
    EnvVar(name="PORT", type="int", default="8000"),
    EnvVar(name="DEBUG", type="bool", default="false"),
])

Optional Variables

check_env([
    EnvVar(name="SENTRY_DSN", required=False),  # won't fail if missing
])

Minimum Length

check_env([
    EnvVar(name="SECRET_KEY", min_length=32),  # must be at least 32 chars
])

Quick Start with guard()

The guard() function is a one-liner shorthand:

from envguard import guard

guard(
    DATABASE_URL="url",
    REDIS_URL="url",
    SECRET_KEY="str",
    DEBUG="bool",
)

Zero Dependencies

envguard uses only Python standard library. No bloat.

License

MIT

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

envguard_check-0.1.1.tar.gz (5.1 kB view details)

Uploaded Source

Built Distribution

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

envguard_check-0.1.1-py3-none-any.whl (4.9 kB view details)

Uploaded Python 3

File details

Details for the file envguard_check-0.1.1.tar.gz.

File metadata

  • Download URL: envguard_check-0.1.1.tar.gz
  • Upload date:
  • Size: 5.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.5

File hashes

Hashes for envguard_check-0.1.1.tar.gz
Algorithm Hash digest
SHA256 e9b50f332547bbcf373ea52a997de018856be3fef1afb7104d0f06cda62e6d74
MD5 ac9d364a4ee1ef81bf35c4b2cdf03e60
BLAKE2b-256 f5ae75711bf6be0eea866d1a81c3b00ce281ce1f6aeec5a7f7ebb77615aaf191

See more details on using hashes here.

File details

Details for the file envguard_check-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: envguard_check-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 4.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.5

File hashes

Hashes for envguard_check-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 2a63bc418f835832c333f721fc1fbc37f1c5db53b7f28b7265ac7d39ff777655
MD5 15034af71802e387a7bdfb707042ac9a
BLAKE2b-256 532ffc6bc575fcd0bb1cd262b7da6c441ee161c2316f6dcc68ff7ee4f76ae80c

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