Coverage for jsonschema_diff/config_maker.py: 22%

40 statements  

« prev     ^ index     » next       coverage.py v7.10.5, created at 2025-08-25 07:00 +0000

1""" 

2Factory for a ready-to-use :class:`jsonschema_diff.core.Config` instance. 

3 

4All optional switches are enabled by default; pass ``False`` to disable. 

5""" 

6 

7from .core import Compare, Config 

8from .core.custom_compare import CompareList, CompareRange 

9from .core.tools.combine import COMBINE_RULES_TYPE 

10from .core.tools.compare import COMPARE_RULES_TYPE 

11from .core.tools.context import CONTEXT_RULES_TYPE, PAIR_CONTEXT_RULES_TYPE 

12from .core.tools.render import PATH_MAKER_IGNORE_RULES_TYPE 

13 

14 

15class ConfigMaker: 

16 """Helper that builds a fully populated :class:`~jsonschema_diff.core.Config`.""" 

17 

18 # pylint: disable=too-many-arguments 

19 @staticmethod 

20 def make( 

21 *, 

22 tab_size: int = 2, 

23 path_render_with_properies: bool = False, 

24 path_render_with_items: bool = False, 

25 list_comparator: bool = True, 

26 range_digit_comparator: bool = True, 

27 range_length_comparator: bool = True, 

28 range_items_comparator: bool = True, 

29 range_properties_comparator: bool = True, 

30 additional_compare_rules: COMPARE_RULES_TYPE = {}, 

31 additional_combine_rules: COMBINE_RULES_TYPE = [], 

32 additional_pair_context_rules: PAIR_CONTEXT_RULES_TYPE = [], 

33 additional_context_rules: CONTEXT_RULES_TYPE = {}, 

34 additional_path_maker_ignore: PATH_MAKER_IGNORE_RULES_TYPE = [], 

35 ) -> Config: 

36 """ 

37 Assemble and return a :class:`~jsonschema_diff.core.Config`. 

38 

39 Parameters 

40 ---------- 

41 tab_size : int 

42 Number of spaces per indentation level. 

43 path_render_with_properies, path_render_with_items : bool 

44 Include these schema service tokens in rendered paths. 

45 list_comparator : bool 

46 Enable :class:`~jsonschema_diff.core.custom_compare.CompareList`. 

47 range_*_comparator : bool 

48 Enable :class:`~jsonschema_diff.core.custom_compare.CompareRange` 

49 for numeric/length/items/properties limits. 

50 additional_* : collections 

51 User-supplied rules that override the built-ins. 

52 """ 

53 tab = " " * tab_size 

54 

55 compare_rules: COMPARE_RULES_TYPE = {} 

56 combine_rules: COMBINE_RULES_TYPE = [] 

57 pair_context_rules: list[list[str | type[Compare]]] = [] 

58 context_rules: dict[str | type[Compare], list[str | type[Compare]]] = {} 

59 path_maker_ignore: list[str] = [] 

60 

61 # Built-in comparators 

62 if list_comparator: 

63 compare_rules[list] = CompareList 

64 

65 def add_rule(keys: list[str], value: type[Compare]) -> None: 

66 combine_rules.append(keys) 

67 for key in keys: 

68 compare_rules[key] = value 

69 

70 ranger = CompareRange 

71 if range_digit_comparator: 

72 add_rule(["minimum", "maximum", "exclusiveMinimum", "exclusiveMaximum"], ranger) 

73 if range_length_comparator: 

74 add_rule(["minLength", "maxLength"], ranger) 

75 if range_items_comparator: 

76 add_rule(["minItems", "maxItems"], ranger) 

77 if range_properties_comparator: 

78 add_rule(["minProperties", "maxProperties"], ranger) 

79 

80 # Path-render filters 

81 if not path_render_with_properies: 

82 path_maker_ignore.append("properties") 

83 if not path_render_with_items: 

84 path_maker_ignore.append("items") 

85 

86 # User additions override defaults 

87 compare_rules.update(additional_compare_rules) 

88 combine_rules.extend(additional_combine_rules) 

89 pair_context_rules.extend([list(r) for r in additional_pair_context_rules]) 

90 context_rules.update({k: list(v) for k, v in additional_context_rules.items()}) 

91 path_maker_ignore.extend(additional_path_maker_ignore) 

92 

93 return Config( 

94 tab=tab, 

95 compare_rules=compare_rules, 

96 combine_rules=combine_rules, 

97 path_maker_ignore=path_maker_ignore, 

98 pair_context_rules=pair_context_rules, 

99 context_rules=context_rules, 

100 )