Coverage for structured_tutorials / models / validators.py: 100%
13 statements
« prev ^ index » next coverage.py v7.13.0, created at 2025-12-26 12:41 +0100
« prev ^ index » next coverage.py v7.13.0, created at 2025-12-26 12:41 +0100
1# Copyright (c) 2025 Mathias Ertl
2# Licensed under the MIT License. See LICENSE file for details.
4"""Validators for various models."""
6import re
7from typing import Any
9from pydantic import NonNegativeInt
12def validate_regex(value: Any) -> Any:
13 """Validate and compile a regular expression."""
14 if isinstance(value, str):
15 return re.compile(value.encode())
16 return value # pragma: no cover
19def validate_count_tuple(
20 value: tuple[NonNegativeInt | None, NonNegativeInt | None],
21) -> tuple[NonNegativeInt | None, NonNegativeInt | None]:
22 """Validate that min is larger than max in a count tuple."""
23 count_min, count_max = value
24 if count_min is not None and count_max is not None and count_min > count_max:
25 raise ValueError(f"Minimum ({count_min}) is greater than maximum ({count_max}).")
26 if count_min is None and count_max is None:
27 raise ValueError("At least one of minimum or maximum must be specified.")
28 return value