Skip to main content

Schema-based environment variable validation with type coercion and helpful error messages

Project description

philiprehberger-env-validator

Schema-based environment variable validation with type coercion and helpful error messages.

Installation

pip install philiprehberger-env-validator

Usage

Basic Validation

from philiprehberger_env_validator import Schema, validate

schema = (
    Schema()
    .string("DATABASE_URL", description="PostgreSQL connection string")
    .integer("PORT", default=3000)
    .boolean("DEBUG", default=False)
    .string("NODE_ENV", choices=["development", "staging", "production"])
)

config = validate(schema)
print(config["PORT"])  # 3000 (int, not string)

Field Types

schema = (
    Schema()
    .string("API_KEY")
    .integer("MAX_CONNECTIONS")
    .float_field("RATE_LIMIT")
    .boolean("VERBOSE")
    .url("WEBHOOK_URL")
    .email("ADMIN_EMAIL")
)

Custom Validation

schema = Schema().string(
    "API_KEY",
    pattern=r"^sk-[a-zA-Z0-9]{32}$",
    validator=lambda v: len(v) > 10,
)

Optional Fields

schema = (
    Schema()
    .string("REQUIRED_VAR")
    .string("OPTIONAL_VAR", required=False, default="fallback")
)

Custom Source

config = validate(schema, source={"PORT": "8080", "DEBUG": "true"})

Error Handling

from philiprehberger_env_validator import ValidationError

try:
    config = validate(schema)
except ValidationError as e:
    for error in e.errors:
        print(error)

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

philiprehberger_env_validator-0.1.1.tar.gz (3.7 kB view details)

Uploaded Source

Built Distribution

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

philiprehberger_env_validator-0.1.1-py3-none-any.whl (4.6 kB view details)

Uploaded Python 3

File details

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

File metadata

File hashes

Hashes for philiprehberger_env_validator-0.1.1.tar.gz
Algorithm Hash digest
SHA256 16122bb690e275737b0ce842739f80d6a5a465dd1f715d507d8bcd262206b1ca
MD5 836d42b24cf23b291da8ce95e0193e27
BLAKE2b-256 8d2c8e827c2acc1aa9928544b7a9582b8b8032a4d57bccfad0fd97c6e9dab543

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for philiprehberger_env_validator-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 cf0f320bb147ae6b339ba684245abdcd468dcf5788d4f89cb3ab390459ebed63
MD5 75ff13f57c3a7b8a61b9dba51afd5fc0
BLAKE2b-256 12627e93e9381f1a3c094a87b43091e6cdbf4292cb564be674d51b1bc7d1bf63

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