Coverage for structured_tutorials / models / validators.py: 100%
18 statements
« prev ^ index » next coverage.py v7.13.5, created at 2026-04-04 13:17 +0200
« prev ^ index » next coverage.py v7.13.5, created at 2026-04-04 13:17 +0200
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 pathlib import Path
8from typing import Any
10from pydantic import NonNegativeInt
13def validate_regex(value: Any) -> Any:
14 """Validate and compile a regular expression."""
15 if isinstance(value, str):
16 return re.compile(value.encode())
17 return value # pragma: no cover
20def validate_relative_path(value: Path) -> Path:
21 """Validate that a path is relative."""
22 if value.is_absolute():
23 raise ValueError(f"Path must be relative, got: {value!r}")
24 return value
27def validate_count_tuple(
28 value: tuple[NonNegativeInt | None, NonNegativeInt | None],
29) -> tuple[NonNegativeInt | None, NonNegativeInt | None]:
30 """Validate that min is larger than max in a count tuple."""
31 count_min, count_max = value
32 if count_min is not None and count_max is not None and count_min > count_max:
33 raise ValueError(f"Minimum ({count_min}) is greater than maximum ({count_max}).")
34 if count_min is None and count_max is None:
35 raise ValueError("At least one of minimum or maximum must be specified.")
36 return value