Skip to main content

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

Project description

philiprehberger-env-validator

Tests PyPI version License

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)

API

Function / Class Description
validate(schema, source) Validate environment variables against a schema, returning typed dict
Schema Fluent schema builder with string(), integer(), float_field(), boolean(), url(), email() methods
FieldSpec Field specification with type, default, choices, pattern, and validator options
ValidationError Raised when validation fails, contains list of error messages in errors

Development

pip install -e .
python -m pytest tests/ -v

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.6.tar.gz (5.0 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.6-py3-none-any.whl (4.9 kB view details)

Uploaded Python 3

File details

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

File metadata

File hashes

Hashes for philiprehberger_env_validator-0.1.6.tar.gz
Algorithm Hash digest
SHA256 59c7fbdbf5e7a9ed08b614d8321d465de1d3b790ad480204f77fd7b54c61ce6a
MD5 5451c2e707d05e106ed076aff053e05e
BLAKE2b-256 e8b3f65ca965affa73d8a2b4e4a43bc9c75c78affdd7e957e9011de8cc158894

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for philiprehberger_env_validator-0.1.6-py3-none-any.whl
Algorithm Hash digest
SHA256 4d27ae20b0d92c9fbd2bc5c3bb7f57be823385a202a5c9ddc1d4ff9d02812e16
MD5 830a2e5236595b745d4fd82269be436b
BLAKE2b-256 ec1ef0ebc9dad4254865f5b08346638cc1e4a0f355291e5227620a1e6746b31d

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