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 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.0.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.0-py3-none-any.whl (4.9 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: envguard_check-0.1.0.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.0.tar.gz
Algorithm Hash digest
SHA256 7d6143c58c4ca9f8929094844bc3f71090ba2da54d826521194ca4c526ae9e64
MD5 47f560b28454ac29db8b943ddaff7a51
BLAKE2b-256 7b1357fff8aa3f00d788c3014c503de4dba2ca783718bc61ebd4b6ceda16b9f5

See more details on using hashes here.

File details

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

File metadata

  • Download URL: envguard_check-0.1.0-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.0-py3-none-any.whl
Algorithm Hash digest
SHA256 6fcefa1cce0fe9c897e85257249d24199d7f747a682bb0f428e6818cce9923aa
MD5 15c42fe494638119c0ab88131e68ef5e
BLAKE2b-256 8ddd185758968a6a330e2cbaaa9e96880bd7fa1749db47a19302d260d0b36e08

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