Coverage for src / rendercv / schema / models / validation_context.py: 100%

23 statements  

« prev     ^ index     » next       coverage.py v7.13.4, created at 2026-03-25 13:49 +0000

1import pathlib 

2from dataclasses import dataclass 

3from datetime import date as Date 

4from typing import Any, cast 

5 

6import pydantic 

7 

8 

9@dataclass 

10class ValidationContext: 

11 input_file_path: pathlib.Path | None = None 

12 current_date: Any = None 

13 

14 

15def get_input_file_path(info: pydantic.ValidationInfo) -> pathlib.Path | None: 

16 """Extract input file path from validation context. 

17 

18 Why: 

19 Relative paths in YAML (like photo references) must resolve relative 

20 to the input file's directory. Validators access this path via context 

21 to compute absolute paths during validation. 

22 

23 Args: 

24 info: Pydantic validation info containing context. 

25 

26 Returns: 

27 Input file path if available, otherwise None. 

28 """ 

29 if isinstance(info.context, dict): 

30 context = cast(ValidationContext, info.context["context"]) 

31 if context.input_file_path: 

32 return context.input_file_path 

33 return None 

34 

35 

36def get_current_date(info: pydantic.ValidationInfo) -> Date: 

37 """Extract current date from validation context or default to today. 

38 

39 Why: 

40 Date calculations (like months of experience) must use consistent 

41 reference dates. Users can override via settings.current_date for 

42 reproducible builds, otherwise defaults to today. The ``"today"`` 

43 keyword is resolved to the actual current date. Invalid values fall 

44 back to today so the Settings model can report the error properly. 

45 

46 Args: 

47 info: Pydantic validation info containing context. 

48 

49 Returns: 

50 Current date from context or Date.today(). 

51 """ 

52 if isinstance(info.context, dict): 

53 context = cast(ValidationContext, info.context["context"]) 

54 if isinstance(context.current_date, Date): 

55 return context.current_date 

56 if context.current_date == "today": 

57 return Date.today() 

58 return Date.today()