Coverage for src / toolbox_python / validators.py: 100%
24 statements
« prev ^ index » next coverage.py v7.13.1, created at 2026-01-02 22:56 +0000
« prev ^ index » next coverage.py v7.13.1, created at 2026-01-02 22:56 +0000
1# ============================================================================ #
2# #
3# Title: Validators Utility Module #
4# Purpose: Provides validation functions and classes for numeric ranges #
5# #
6# ============================================================================ #
9# ---------------------------------------------------------------------------- #
10# #
11# Setup ####
12# #
13# ---------------------------------------------------------------------------- #
16## --------------------------------------------------------------------------- #
17## Imports ####
18## --------------------------------------------------------------------------- #
21# ## Future Python Library Imports ----
22from __future__ import annotations
24# ## Python StdLib Imports ----
25from collections.abc import Sequence
26from numbers import Real
28# ## Local First Party Imports ----
29from toolbox_python.checkers import is_valid
32## --------------------------------------------------------------------------- #
33## Exports ####
34## --------------------------------------------------------------------------- #
37__all__: list[str] = ["Validators"]
40# ---------------------------------------------------------------------------- #
41# #
42# Validators ####
43# #
44# ---------------------------------------------------------------------------- #
47class Validators:
48 """
49 !!! note "Summary"
50 A class containing various validation methods.
52 Methods:
53 value_is_between(): Check if a value is between two other values.
54 assert_value_is_between(): Assert that a value is between two other values.
55 all_values_are_between(): Check if all values in an array are between two other values.
56 assert_all_values_are_between(): Assert that all values in an array are between two other values
57 """
59 @staticmethod
60 def value_is_between(value: Real, min_value: Real, max_value: Real) -> bool:
61 """
62 !!! note "Summary"
63 Check if a value is between two other values.
65 Params:
66 value (Real):
67 The value to check.
68 min_value (Real):
69 The minimum value.
70 max_value (Real):
71 The maximum value.
73 Returns:
74 (bool):
75 True if the value is between the minimum and maximum values, False otherwise.
76 """
77 if not is_valid(min_value, "<=", max_value):
78 raise ValueError(
79 f"Invalid range: min_value `{min_value}` must be less than or equal to max_value `{max_value}`"
80 )
81 result: bool = is_valid(value, ">=", min_value) and is_valid(value, "<=", max_value)
82 return result
84 @staticmethod
85 def assert_value_is_between(
86 value: Real,
87 min_value: Real,
88 max_value: Real,
89 ) -> None:
90 """
91 !!! note "Summary"
92 Assert that a value is between two other values.
94 Params:
95 value (Real):
96 The value to check.
97 min_value (Real):
98 The minimum value.
99 max_value (Real):
100 The maximum value.
102 Raises:
103 (AssertionError):
104 If the value is not between the minimum and maximum values.
105 """
106 if not Validators.value_is_between(value, min_value, max_value):
107 raise AssertionError(f"Invalid Value: `{value}`. Must be between `{min_value}` and `{max_value}`")
109 @staticmethod
110 def all_values_are_between(
111 values: Sequence[Real],
112 min_value: Real,
113 max_value: Real,
114 ) -> bool:
115 """
116 !!! note "Summary"
117 Check if all values in an array are between two other values.
119 Params:
120 values (Sequence[Real]):
121 The array of values to check.
122 min_value (Real):
123 The minimum value.
124 max_value (Real):
125 The maximum value.
127 Returns:
128 (bool):
129 True if all values are between the minimum and maximum values, False otherwise.
130 """
131 return all(Validators.value_is_between(value, min_value, max_value) for value in values)
133 @staticmethod
134 def assert_all_values_are_between(
135 values: Sequence[Real],
136 min_value: Real,
137 max_value: Real,
138 ) -> None:
139 """
140 !!! note "Summary"
141 Assert that all values in an array are between two other values.
143 Params:
144 values (Sequence[Real]):
145 The array of values to check.
146 min_value (Real):
147 The minimum value.
148 max_value (Real):
149 The maximum value.
151 Raises:
152 (AssertionError):
153 If any value is not between the minimum and maximum values.
154 """
155 values_not_between: list[Real] = [
156 value for value in values if not Validators.value_is_between(value, min_value, max_value)
157 ]
158 if not len(values_not_between) == 0:
159 raise AssertionError(f"Values not between `{min_value}` and `{max_value}`: {values_not_between}")