Coverage for src / rendercv / schema / models / design / typst_dimension.py: 100%
11 statements
« prev ^ index » next coverage.py v7.13.4, created at 2026-03-25 13:49 +0000
« prev ^ index » next coverage.py v7.13.4, created at 2026-03-25 13:49 +0000
1import re
2from typing import Annotated
4import pydantic
5import pydantic_core
7from ...pydantic_error_handling import CustomPydanticErrorTypes
10def validate_typst_dimension(dimension: str) -> str:
11 """Validate Typst dimension format with unit.
13 Why:
14 Typst requires dimensions with explicit units (e.g., 1cm, 0.5in).
15 Validation prevents compilation errors from missing or invalid units.
17 Args:
18 dimension: Dimension string to validate.
20 Returns:
21 Original dimension if valid.
22 """
23 # Negative dimensions are valid in Typst for negative spacing/offsets:
24 if not re.fullmatch(r"-?\d+(?:\.\d+)?(cm|in|pt|mm|em)", dimension):
25 raise pydantic_core.PydanticCustomError(
26 CustomPydanticErrorTypes.other.value,
27 "The value must be a number followed by a unit (cm, in, pt, mm, em)."
28 " For example, 0.1cm.",
29 )
30 return dimension
33type TypstDimension = Annotated[str, pydantic.AfterValidator(validate_typst_dimension)]
35length_common_description = (
36 "It can be specified with units (cm, in, pt, mm, em). For example, `0.1cm`."
37)