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 RangeValue

class Temperature(RangeValue):
    """Temperature constrained between 0 and 100°C."""
    def __init__(self, value):
        super().__init__(value, low_value=0, high_value=100)

def main() -> None:
    # ✅ Valid temperature
    t = Temperature(42)
    print("Value:", t.value)        # 42
    print("Status:", t.status.name) # OK
    print("Details:", t.details)    # validation successful

    # 🚫 Invalid temperature
    t_invalid = Temperature(120)
    print("Value:", t_invalid.value)   # None
    print("Status:", t_invalid.status) # Status.EXCEPTION
    print("Details:", t_invalid.details)  # Value must be less than or equal to 100, got 120

if __name__ == "__main__":
    main()

🔥 Strict version

If you want the behavior where invalid input throws immediately, use the StrictValue mixin:

class StrictTemperature(RangeValue, StrictValue):
    """Same range constraint but raises if invalid."""
    def __init__(self, value):
        RangeValue.__init__(self, value, low_value=0, high_value=100)

def main() -> None:
    t = StrictTemperature(42)
    print("Value:", t.value) # 42

    try:
        StrictTemperature(120)
    except ValueError as e:
        print(e) # Failed Constraints for value - '120': Value must be less than or equal to 100, got 120

if __name__ == "__main__":
    main()

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

Uploaded Python 3

File details

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

File metadata

  • Download URL: constrained_values-0.1.7.tar.gz
  • Upload date:
  • Size: 25.8 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.7.tar.gz
Algorithm Hash digest
SHA256 dee6be3bc64f2d993319116273d3f0ee4cb35bc529baa888e6c14c7eb3aecedd
MD5 0ad64de7f8e57b87a327a147ac709606
BLAKE2b-256 e89f43fb1f93048532082a5387a48d720de6eeee9af128502e4a3a9fece5dc11

See more details on using hashes here.

Provenance

The following attestation bundles were made for constrained_values-0.1.7.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.7-py3-none-any.whl.

File metadata

File hashes

Hashes for constrained_values-0.1.7-py3-none-any.whl
Algorithm Hash digest
SHA256 cf93cbbb92997e7540300207c89e5fa5bf258b2ad7c8b06ead9dff28398f9910
MD5 fa331eb1a737c973f8bce864385fca6e
BLAKE2b-256 8a4c9f76e9a1c5a249678449e997def2cc2fb47b65a72b7f858f6b452eeef516

See more details on using hashes here.

Provenance

The following attestation bundles were made for constrained_values-0.1.7-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