examples.10_type_validation_strategy

Demonstrates how to enforce runtime type validation using TypeValidationStrategy.

This example shows:
  • How to create a TypeValidationStrategy that accepts multiple types.
  • How .validate() returns an OK or EXCEPTION Response based on type match.
  • How to inspect the returned status and details fields.
Run directly:

python examples/10_type_validation_strategy.py

Expected output:

validate 3: OK validate 3.0: OK validate 'x': EXCEPTION - Value must be one of 'int', 'float', got 'str'

 1"""Demonstrates how to enforce runtime type validation using
 2:class:`TypeValidationStrategy`.
 3
 4This example shows:
 5  * How to create a `TypeValidationStrategy` that accepts multiple types.
 6  * How `.validate()` returns an OK or EXCEPTION `Response` based on type match.
 7  * How to inspect the returned status and details fields.
 8
 9Run directly:
10
11    python examples/10_type_validation_strategy.py
12
13Expected output:
14
15    validate 3: OK
16    validate 3.0: OK
17    validate 'x': EXCEPTION - Value must be one of 'int', 'float', got 'str'
18"""
19
20import sys
21import pathlib
22
23# ---------------------------------------------------------------------------
24# Make repo root importable when running this file directly
25# ---------------------------------------------------------------------------
26sys.path.insert(0, str(pathlib.Path(__file__).resolve().parents[1]))
27
28from constrained_values import TypeValidationStrategy
29
30
31def main() -> None:
32    """Run the type validation demonstration.
33
34    Creates a :class:`TypeValidationStrategy` that allows `int` and `float`
35    values, then validates several inputs to show how the strategy enforces
36    runtime type checks.
37
38    Steps:
39        1. Validate `3` (int) → passes.
40        2. Validate `3.0` (float) → passes.
41        3. Validate `'x'` (str) → fails with EXCEPTION.
42
43    Prints:
44        * `"validate 3: OK"`
45        * `"validate 3.0: OK"`
46        * `"validate 'x': EXCEPTION - Value must be one of 'int', 'float', got 'str'"`
47    """
48    s = TypeValidationStrategy([int, float])
49    print("validate 3:", s.validate(3).status.name)
50    print("validate 3.0:", s.validate(3.0).status.name)
51    r = s.validate("x")
52    print("validate 'x':", r.status.name, "-", r.details)
53
54
55if __name__ == "__main__":
56    main()
def main() -> None:
32def main() -> None:
33    """Run the type validation demonstration.
34
35    Creates a :class:`TypeValidationStrategy` that allows `int` and `float`
36    values, then validates several inputs to show how the strategy enforces
37    runtime type checks.
38
39    Steps:
40        1. Validate `3` (int) → passes.
41        2. Validate `3.0` (float) → passes.
42        3. Validate `'x'` (str) → fails with EXCEPTION.
43
44    Prints:
45        * `"validate 3: OK"`
46        * `"validate 3.0: OK"`
47        * `"validate 'x': EXCEPTION - Value must be one of 'int', 'float', got 'str'"`
48    """
49    s = TypeValidationStrategy([int, float])
50    print("validate 3:", s.validate(3).status.name)
51    print("validate 3.0:", s.validate(3.0).status.name)
52    r = s.validate("x")
53    print("validate 'x':", r.status.name, "-", r.details)

Run the type validation demonstration.

Creates a TypeValidationStrategy that allows int and float values, then validates several inputs to show how the strategy enforces runtime type checks.

Steps:
  1. Validate 3 (int) → passes.
  2. Validate 3.0 (float) → passes.
  3. Validate 'x' (str) → fails with EXCEPTION.
Prints:
  • "validate 3: OK"
  • "validate 3.0: OK"
  • "validate 'x': EXCEPTION - Value must be one of 'int', 'float', got 'str'"