examples.19_strict_validated_value
Demonstrates how StrictValue differs from regular
ConstrainedValue by raising exceptions instead of returning
a non-OK status when validation fails.
This example shows:
- How
StrictValueautomatically validates in its constructor.- That a successful pipeline behaves like
ConstrainedValue.- That a failing pipeline raises
ValueErrorimmediately.
Run directly:
python examples/19_strict_validated_value.py
Expected output:
AlwaysOK: OK 7 AlwaysFail raised: Failed Constraints for value - '9': boom
1"""Demonstrates how :class:`StrictValue` differs from regular 2:class:`ConstrainedValue` by raising exceptions instead of returning 3a non-OK status when validation fails. 4 5This example shows: 6 * How `StrictValue` automatically validates in its constructor. 7 * That a successful pipeline behaves like `ConstrainedValue`. 8 * That a failing pipeline raises `ValueError` immediately. 9 10Run directly: 11 12 python examples/19_strict_validated_value.py 13 14Expected output: 15 16 AlwaysOK: OK 7 17 AlwaysFail raised: Failed Constraints for value - '9': boom 18""" 19 20import sys 21import pathlib 22from typing import List 23 24# --------------------------------------------------------------------------- 25# Make repo root importable when running this file directly 26# --------------------------------------------------------------------------- 27sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[1])) 28 29from constrained_values import StrictValue 30from constrained_values.strategies import FailValidationStrategy 31 32 33class AlwaysOK(StrictValue[int]): 34 """A `StrictValue[int]` that always succeeds. 35 36 Since it defines no failing strategies, all values pass validation. 37 """ 38 39 def get_strategies(self) -> List: 40 """Return an empty strategy list.""" 41 return [] 42 43 44class AlwaysFail(StrictValue[int]): 45 """A `StrictValue[int]` that always fails. 46 47 The strategy list contains a single :class:`FailValidationStrategy`, 48 which raises a `ValueError` when validation runs. 49 """ 50 51 def get_strategies(self) -> List: 52 """Return a single failing strategy.""" 53 return [FailValidationStrategy("boom")] 54 55 56def main() -> None: 57 """Run the StrictValue demonstration. 58 59 Creates two subclasses of :class:`StrictValue`: 60 61 1. **AlwaysOK** — accepts any input successfully. 62 2. **AlwaysFail** — raises a `ValueError` during initialization. 63 64 Prints: 65 * `"AlwaysOK: OK 7"` 66 * `"AlwaysFail raised: Failed Constraints for value - '9': boom"` 67 """ 68 x = AlwaysOK(7) 69 print("AlwaysOK:", x.status.name, x.value) 70 71 try: 72 AlwaysFail(9) 73 except ValueError as e: 74 print("AlwaysFail raised:", e) 75 76 77if __name__ == "__main__": 78 main()
class
AlwaysOK(constrained_values.constrained_value_types.StrictValue[int]):
34class AlwaysOK(StrictValue[int]): 35 """A `StrictValue[int]` that always succeeds. 36 37 Since it defines no failing strategies, all values pass validation. 38 """ 39 40 def get_strategies(self) -> List: 41 """Return an empty strategy list.""" 42 return []
A StrictValue[int] that always succeeds.
Since it defines no failing strategies, all values pass validation.
Inherited Members
- constrained_values.constrained_value_types.StrictValue
- StrictValue
- constrained_values.value.ConstrainedValue
- status
- details
- value
- unwrap
- ok
class
AlwaysFail(constrained_values.constrained_value_types.StrictValue[int]):
45class AlwaysFail(StrictValue[int]): 46 """A `StrictValue[int]` that always fails. 47 48 The strategy list contains a single :class:`FailValidationStrategy`, 49 which raises a `ValueError` when validation runs. 50 """ 51 52 def get_strategies(self) -> List: 53 """Return a single failing strategy.""" 54 return [FailValidationStrategy("boom")]
A StrictValue[int] that always fails.
The strategy list contains a single FailValidationStrategy,
which raises a ValueError when validation runs.
def
get_strategies(self) -> List:
52 def get_strategies(self) -> List: 53 """Return a single failing strategy.""" 54 return [FailValidationStrategy("boom")]
Return a single failing strategy.
Inherited Members
- constrained_values.constrained_value_types.StrictValue
- StrictValue
- constrained_values.value.ConstrainedValue
- status
- details
- value
- unwrap
- ok
def
main() -> None:
57def main() -> None: 58 """Run the StrictValue demonstration. 59 60 Creates two subclasses of :class:`StrictValue`: 61 62 1. **AlwaysOK** — accepts any input successfully. 63 2. **AlwaysFail** — raises a `ValueError` during initialization. 64 65 Prints: 66 * `"AlwaysOK: OK 7"` 67 * `"AlwaysFail raised: Failed Constraints for value - '9': boom"` 68 """ 69 x = AlwaysOK(7) 70 print("AlwaysOK:", x.status.name, x.value) 71 72 try: 73 AlwaysFail(9) 74 except ValueError as e: 75 print("AlwaysFail raised:", e)
Run the StrictValue demonstration.
Creates two subclasses of StrictValue:
- AlwaysOK — accepts any input successfully.
- AlwaysFail — raises a
ValueErrorduring initialization.
Prints:
"AlwaysOK: OK 7""AlwaysFail raised: Failed Constraints for value - '9': boom"