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)
Schema Documentation
Generate formatted help text documenting all fields, grouped by required and optional.
schema = (
Schema()
.url("DATABASE_URL", description="PostgreSQL connection string")
.string("API_KEY")
.boolean("DEBUG", default=False, required=False, description="Enable debug mode")
.integer("PORT", default=8000, required=False)
)
print(schema.generate_help())
# REQUIRED:
# DATABASE_URL (url): PostgreSQL connection string
# API_KEY (str): No description
#
# OPTIONAL:
# DEBUG (bool) [default: false]: Enable debug mode
# PORT (int) [default: 8000]: No description
Load from .env File
Read and validate a .env file directly against a schema.
schema = (
Schema()
.string("DATABASE_URL")
.integer("PORT", default=3000)
.boolean("DEBUG", default=False)
)
config = schema.load_from_env_file(".env")
print(config["DATABASE_URL"])
print(config["PORT"]) # coerced to int
The .env file uses standard KEY=VALUE format. Comments (#) and blank lines are skipped. Quoted values are unquoted automatically.
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 |
Schema.generate_help() |
Return formatted help text documenting all fields grouped by required/optional |
Schema.load_from_env_file(path) |
Load and validate a .env file against the schema |
FieldSpec |
Field specification with type, default, choices, pattern, validator, and description options |
ValidationError |
Raised when validation fails, contains list of error messages in errors |
Development
pip install -e .
python -m pytest tests/ -v
Support
If you find this project useful:
License
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file philiprehberger_env_validator-0.2.1.tar.gz.
File metadata
- Download URL: philiprehberger_env_validator-0.2.1.tar.gz
- Upload date:
- Size: 7.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f5ce04b41ed3840728b9057c5365f790227841458db1422927c93b8b371da9a5
|
|
| MD5 |
fced705524f560256fe1dd0112acf1da
|
|
| BLAKE2b-256 |
bfaf7ba2981198320dede50d989d5e6604fb1deb414671e0cd00e0d92b6bc16c
|
File details
Details for the file philiprehberger_env_validator-0.2.1-py3-none-any.whl.
File metadata
- Download URL: philiprehberger_env_validator-0.2.1-py3-none-any.whl
- Upload date:
- Size: 6.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
68ca154b9ced15f7442e5e5c94b2255b6922aa662ee5f7bae031bc0597b349d4
|
|
| MD5 |
b99078b266773dcdb74b43d447f69fc7
|
|
| BLAKE2b-256 |
750b24fd29e228ce776df77e02705614f155bce1efe4d010b4ab8fd63ffa91d4
|