Skip to main content

A Python library for creating type-safe, self-validating value objects using a powerful transformation and validation pipeline.

Project description

Docs Build Status PyPI Version Python Versions License: MIT

Constrained Values

A lightweight Python library for creating type-safe, self-validating value objects — transforming primitive data into meaningful, domain-aware objects with rich validation and transformation pipelines.


🧭 Philosophy: Beyond Primitive Types

In most codebases, we pass around raw values without context:

  • Is temperature = 25 Celsius or Fahrenheit?
  • Is spi_mode = 2 valid for this device?
  • What does -32768 mean again?

Primitive values lack meaning, constraints, and domain intent.
This is Primitive Obsession — a subtle but pervasive design smell.

Constrained Values replaces primitives with expressive, validated objects that make validity explicit.
Each object carries its status (OK or EXCEPTION) and associated errors.

By default, invalid values can exist safely and report their state — but if you want to enforce strict invariants, you can enable exception mode to raise immediately on invalid input.

📖 Full Documentation →


✨ Features

  • 🧩 Rich Value Objects – Replace primitives with expressive, validated domain objects.
  • 🔗 Composable Pipelines – Chain multiple validation and transformation strategies.
  • 🧠 Built-in Validators – Range checks, enums, type coercion, and more.
  • ⚙️ Custom Logic – Easily extend with your own domain-specific rules.
  • 🚦 Clear Error Handling – Track validation status and descriptive messages.
  • 🧯 Strict/Exception Mode (optional) – By default, invalid values are reported non-destructively; enable strict mode to raise exceptions and enforce invariants at creation.
  • 🧾 Type-Safety – Each value enforces its canonical type at runtime.

🚀 Installation

pip install constrained-values

💡 Quick Example

from constrained_values import ConstrainedValue, Range, CoerceType

class Temperature(ConstrainedValue[int]):
    __validators__ = [
        CoerceType(int),
        Range(min=0, max=100, message="Temperature must be between 0°C and 100°C")
    ]

t = Temperature(42)
print(t.value)   # ✅ 42
print(t.status)  # OK

t_invalid = Temperature(120)
print(t_invalid.status)  # EXCEPTION
print(t_invalid.errors)  # ['Temperature must be between 0°C and 100°C']

# Enable strict mode
t_strict = Temperature(120, throw=True)  # Raises ValueError

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

constrained_values-0.1.5.tar.gz (25.5 kB view details)

Uploaded Source

Built Distribution

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

constrained_values-0.1.5-py3-none-any.whl (15.2 kB view details)

Uploaded Python 3

File details

Details for the file constrained_values-0.1.5.tar.gz.

File metadata

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

File hashes

Hashes for constrained_values-0.1.5.tar.gz
Algorithm Hash digest
SHA256 a719d84f3264f7500722228846a7d476c6e55f2b4244c4ce5cd44106b2553c34
MD5 ab76c3d3fbb09cec4b0c1004461dba88
BLAKE2b-256 7d80eae15a9df0ea71429c0ad9b6ebf38c5dfa5d4ca6ede2dad122ce138087f5

See more details on using hashes here.

Provenance

The following attestation bundles were made for constrained_values-0.1.5.tar.gz:

Publisher: publish-pypi.yml on OODesigns/constrained-values

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

File details

Details for the file constrained_values-0.1.5-py3-none-any.whl.

File metadata

File hashes

Hashes for constrained_values-0.1.5-py3-none-any.whl
Algorithm Hash digest
SHA256 cfd558a0bb3a035d0f63ee109c951a9617c0e9abd7288bc32c9089749b37efe1
MD5 68b677c731918f170a6550ed008de4eb
BLAKE2b-256 fd687f6574bc9fae4aae05cd2a2170eaf5e2dbd0b2a70fbd61a465ee5e2e6b7e

See more details on using hashes here.

Provenance

The following attestation bundles were made for constrained_values-0.1.5-py3-none-any.whl:

Publisher: publish-pypi.yml on OODesigns/constrained-values

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