Skip to main content

Beautiful validation errors for pydantic-settings

Project description

pyenvalid

Beautiful validation errors for pydantic-settings.

The Problem

When using pydantic-settings with required environment variables, missing or invalid values produce cryptic errors:

from pydantic_settings import BaseSettings

class Settings(BaseSettings):
    database_url: str
    api_key: str
    port: int = 8080

settings = Settings()  # Raises ValidationError if DATABASE_URL or API_KEY not set
pydantic_core._pydantic_core.ValidationError: 2 validation errors for Settings
database_url
  Field required [type=missing, input_value={}, input_type=dict]
    For further information visit https://errors.pydantic.dev/2.12/v/missing
api_key
  Field required [type=missing, input_value={}, input_type=dict]
    For further information visit https://errors.pydantic.dev/2.12/v/missing

This error is confusing because:

  • It doesn't mention environment variables in a friendly way
  • The input_value={} is misleading (it's not a dict, it's your environment)
  • Hard to quickly see which variables need to be set

The Solution

pyenvalid wraps validation and provides clear, actionable error messages:

┌──────────────────────────────────────────────────────────────────────────┐
│ CONFIGURATION ERROR                                                      │
├──────────────────────────────────────────────────────────────────────────┤
│                                                                          │
│ The following environment variables have issues:                         │
│                                                                          │
│   ✗ DATABASE_URL (missing)                                               │
│   ✗ API_KEY (missing)                                                    │
│                                                                          │
├──────────────────────────────────────────────────────────────────────────┤
│ Set these in your .env file or environment                               │
└──────────────────────────────────────────────────────────────────────────┘

Installation

pip install pyenvalid

Or with uv:

uv add pyenvalid

Usage

from pydantic_settings import BaseSettings
from pyenvalid import validate_settings

class Settings(BaseSettings):
    database_url: str
    api_key: str
    port: int = 8080

settings = validate_settings(Settings)

That's it. If DATABASE_URL or API_KEY are missing, you get the nice error box instead of pydantic's raw error.

With .env file

from pydantic_settings import BaseSettings, SettingsConfigDict
from pyenvalid import validate_settings

class Settings(BaseSettings):
    model_config = SettingsConfigDict(env_file=".env")

    secret_key: str
    database_url: str

settings = validate_settings(Settings)

Custom error messages

from pyenvalid import ConfigurationError, validate_settings

try:
    settings = validate_settings(Settings)
except ConfigurationError as e:
    raise ConfigurationError(
        e.errors,
        title="DATABASE ERROR",
        hint="Check your .env.local file",
    ) from None

Handling errors programmatically

from pyenvalid import ConfigurationError, validate_settings

try:
    settings = validate_settings(Settings)
except ConfigurationError as e:
    print(e.errors)  # [('database_url', 'missing'), ('api_key', 'missing')]
    print(e.missing_fields)  # ['database_url', 'api_key']

Error Types

The error type shown is the raw pydantic error type:

Error Meaning
missing Required field not set
int_parsing Value can't be parsed as integer
bool_parsing Value can't be parsed as boolean
literal_error Value not in allowed options
url_parsing Invalid URL format

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

pyenvalid-0.1.0.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.

pyenvalid-0.1.0-py3-none-any.whl (3.1 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: pyenvalid-0.1.0.tar.gz
  • Upload date:
  • Size: 5.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for pyenvalid-0.1.0.tar.gz
Algorithm Hash digest
SHA256 fbd774a949ed418592f895b5e9e9c45f1ee265dd20fbecb9660e07564897fd5b
MD5 614e3eb683fca8e3c582baa08427c6e6
BLAKE2b-256 3a0b48e8c1248e336fd2405553bcd9327dbe8a93c86e2c37faf3057f9beb1e25

See more details on using hashes here.

Provenance

The following attestation bundles were made for pyenvalid-0.1.0.tar.gz:

Publisher: publish.yml on truehazker/pyenvalid

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

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

File metadata

  • Download URL: pyenvalid-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 3.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for pyenvalid-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 3ed57e2a76a5bf45ded48f2d6b3ae5d60e16fac16928bbda6bdbb26ff0e03c66
MD5 170ed1363a0b07b48240488d40b1c198
BLAKE2b-256 418c5e808ebcc912f6a5fd37b05fcaa1756601a0bc7ef30d6980602006f799fc

See more details on using hashes here.

Provenance

The following attestation bundles were made for pyenvalid-0.1.0-py3-none-any.whl:

Publisher: publish.yml on truehazker/pyenvalid

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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