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

1import re 

2from typing import Annotated 

3 

4import pydantic 

5import pydantic_core 

6 

7from ...pydantic_error_handling import CustomPydanticErrorTypes 

8 

9 

10def validate_typst_dimension(dimension: str) -> str: 

11 """Validate Typst dimension format with unit. 

12 

13 Why: 

14 Typst requires dimensions with explicit units (e.g., 1cm, 0.5in). 

15 Validation prevents compilation errors from missing or invalid units. 

16 

17 Args: 

18 dimension: Dimension string to validate. 

19 

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 

31 

32 

33type TypstDimension = Annotated[str, pydantic.AfterValidator(validate_typst_dimension)] 

34 

35length_common_description = ( 

36 "It can be specified with units (cm, in, pt, mm, em). For example, `0.1cm`." 

37)