Metadata-Version: 2.4
Name: constrained-values
Version: 0.1.5
Summary: A Python library for creating type-safe, self-validating value objects using a powerful transformation and validation pipeline.
Author-email: Michael Lindre <cvl@oodesigns.com>
License: MIT License
        
        Copyright (c) 2024 Michael Lindre
        
        Permission is hereby granted, free of charge, to any person obtaining a copy
        of this software and associated documentation files (the "Software"), to deal
        in the Software without restriction, including without limitation the rights
        to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
        copies of the Software, and to permit persons to whom the Software is
        furnished to do so, subject to the following conditions:
        
        The above copyright notice and this permission notice shall be included in all
        copies or substantial portions of the Software.
        
        THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
        IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
        FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
        AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
        LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
        OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
        SOFTWARE.
Project-URL: Homepage, https://github.com/OODesigns/constrained-values
Project-URL: Documentation, https://oodesigns.github.io/constrained-values/constrained_values.html
Project-URL: Repository, https://github.com/OODesigns/constrained-values
Project-URL: Issues, https://github.com/OODesigns/constrained-values/issues
Keywords: constraint-programming,constrained-enum,constrained-range,constrained-value,constraints,enum,range,types,validation,validation-library,value-object,validated-enum,validated-range,validated-value,validator
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3 :: Only
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: License :: OSI Approved :: MIT License
Classifier: Operating System :: OS Independent
Requires-Python: >=3.11
Description-Content-Type: text/markdown
License-File: LICENSE
Dynamic: license-file

[![Docs](https://img.shields.io/badge/docs-latest-brightgreen.svg?logo=readthedocs&logoColor=white)](https://OODesigns.github.io/constrained-values/constrained_values.html)
[![Build Status](https://github.com/oodesigns/constrained-values/actions/workflows/website.yml/badge.svg)](https://github.com/OODesigns/constrained-values/actions)
[![PyPI Version](https://img.shields.io/pypi/v/constrained-values.svg?logo=pypi&logoColor=white)](https://pypi.org/project/constrained-values/)
![Python Versions](https://img.shields.io/pypi/pyversions/constrained-values.svg)
[![License: MIT](https://img.shields.io/badge/license-MIT-blue.svg)](https://opensource.org/licenses/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 →**](https://oodesigns.github.io/constrained-values/constrained_values.html#the-philosophy-beyond-primitive-types)

---

## ✨ 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

```bash
pip install constrained-values
```
## 💡 Quick Example

```python
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
