Values Check

fejao_config_parser.src.values.values_check

values.values_check.py file for the fejao_config_parser package.

This module defines the ValuesCheck class which is responsible for validating configuration values against expected types.

ValuesCheck

Validate parsed configuration values against expected types.

The ValuesCheck class checks configuration values produced by parser classes such as ParseFromDefault, ParseFromEnvironment, ParseFromFile, and ParseFromTerminal. It verifies that each found value matches the expected type defined in the configuration metadata.

Attributes:
  • config (list) –

    Configuration definitions containing name and type keys.

  • values_found (dict) –

    Parsed values to validate.

  • info_string (str) –

    Human-readable context used in error messages.

Example

config = [ ... {"name": "debug", "type": "bool", "default": False}, ... {"name": "port", "type": "int", "default": 8080} ... ] values = {"debug": True, "port": 8080} ValuesCheck(config=config, values_found=values, info_string="environment values")

Source code in fejao_config_parser/src/values/values_check.py
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
class ValuesCheck:
    """Validate parsed configuration values against expected types.

    The `ValuesCheck` class checks configuration values produced by parser classes
    such as `ParseFromDefault`, `ParseFromEnvironment`, `ParseFromFile`, and
    `ParseFromTerminal`. It verifies that each found value matches the expected
    type defined in the configuration metadata.

    Attributes:
        config (list): Configuration definitions containing `name` and `type` keys.
        values_found (dict): Parsed values to validate.
        info_string (str): Human-readable context used in error messages.

    Example:
        >>> config = [
        ...     {"name": "debug", "type": "bool", "default": False},
        ...     {"name": "port", "type": "int", "default": 8080}
        ... ]
        >>> values = {"debug": True, "port": 8080}
        >>> ValuesCheck(config=config, values_found=values, info_string="environment values")
    """

    def __init__(self, config: list, values_found: dict, info_string: str) -> None:
        """Create a ValuesCheck instance and validate parsed values.

        Args:
            config (list): List of configuration dictionaries.
            values_found (dict): Parsed values that should be validated.
            info_string (str): Context string used for error messages.

        Raises:
            ValueError: If a parsed value does not match its expected type.
        """
        self.config = config
        self.values_found = values_found
        self.info_string = info_string
        self.__check_values()

    def __check_types(self, name: str, value: object, expected_type: str) -> None:
        """Check if a value matches the expected type.

        Args:
            name (str): The name of the configuration item.
            value (object): The value to check.
            expected_type (str): The expected type.

        Raises:
            ValueError: If the value does not match the expected type.
        """
        value_error_str = (
            f"\n\nError checking the configuration values for '{self.info_string}'. "
            f"The value found for the name '{name}' that wanted to be used as type '{expected_type}' "
            f"but the value parsed is: '{value}'.\n")

        ### STR
        if expected_type == "str" and not isinstance(value, str):
            raise ValueError(value_error_str)

        ### BOOL
        if expected_type == "bool" and not isinstance(value, bool):
            raise ValueError(value_error_str)

        ### INT
        if expected_type == "int" and not isinstance(value, int):
            raise ValueError(value_error_str)

        ### FLOAT
        if expected_type == "float" and not isinstance(value, float):
            raise ValueError(value_error_str)

        ### LIST
        if expected_type == "list" and not isinstance(value, list):
            raise ValueError(value_error_str)

        ### DICT
        if expected_type == "dict" and not isinstance(value, dict):
            raise ValueError(value_error_str)

    def __check_values(self) -> None:
        """Validate found values against configuration type metadata.

        This private method iterates over the configuration items and checks
        each found value only if the configuration defines it. Supported types
        include string, bool, int, float, list, and dict.

        Raises:
            ValueError: If a parsed value fails type validation or if the
                configured type is not supported.

        Returns:
            None
        """
        for item in self.config:
            if item['name'] in self.values_found.keys():
                name_value = self.values_found[item['name']]
                if item["type"].lower() not in [
                    "string", "str",
                    "bool", "boolean",
                    "int", "integer",
                    "float", "double",
                    "list", "lst",
                    "dict", "dictionary"
                ]:
                    raise ValueError(
                        f"\n\nError checking the configuration values for '{self.info_string}'. "
                        f"The type '{item['type']}' for the name '{item['name']}' is not supported. "
                        f"The accepted types are: {DEFAULT_ACCEPTED_KEY_TYPES}. "
                        "Please refer to the documentation for more details.\n"
                    )

                ### STRING
                if item["type"].lower() == "string" or item["type"].lower() == "str":
                    self.__check_types(name=item['name'], value=name_value, expected_type="str")

                ### BOOL
                elif item["type"].lower() == "bool" or item["type"].lower() == "boolean":
                    self.__check_types(name=item['name'], value=name_value, expected_type="bool")

                ### INT
                elif item["type"].lower() == "int" or item["type"].lower() == "integer":
                    self.__check_types(name=item['name'], value=name_value, expected_type="int")

                ### FLOAT
                elif item["type"].lower() == "float" or item["type"].lower() == "double":
                    self.__check_types(name=item['name'], value=name_value, expected_type="float")

                ### LIST
                elif item["type"].lower() == "list" or item["type"].lower() == "lst":
                    self.__check_types(name=item['name'], value=name_value, expected_type="list")

                ### DICT
                elif item["type"].lower() == "dict" or item["type"].lower() == "dictionary":
                    self.__check_types(name=item['name'], value=name_value, expected_type="dict")
Methods:
__check_types
__check_types(name: str, value: object, expected_type: str) -> None

Check if a value matches the expected type.

Parameters:
  • name (str) –

    The name of the configuration item.

  • value (object) –

    The value to check.

  • expected_type (str) –

    The expected type.

Raises:
  • ValueError –

    If the value does not match the expected type.

Source code in fejao_config_parser/src/values/values_check.py
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
def __check_types(self, name: str, value: object, expected_type: str) -> None:
    """Check if a value matches the expected type.

    Args:
        name (str): The name of the configuration item.
        value (object): The value to check.
        expected_type (str): The expected type.

    Raises:
        ValueError: If the value does not match the expected type.
    """
    value_error_str = (
        f"\n\nError checking the configuration values for '{self.info_string}'. "
        f"The value found for the name '{name}' that wanted to be used as type '{expected_type}' "
        f"but the value parsed is: '{value}'.\n")

    ### STR
    if expected_type == "str" and not isinstance(value, str):
        raise ValueError(value_error_str)

    ### BOOL
    if expected_type == "bool" and not isinstance(value, bool):
        raise ValueError(value_error_str)

    ### INT
    if expected_type == "int" and not isinstance(value, int):
        raise ValueError(value_error_str)

    ### FLOAT
    if expected_type == "float" and not isinstance(value, float):
        raise ValueError(value_error_str)

    ### LIST
    if expected_type == "list" and not isinstance(value, list):
        raise ValueError(value_error_str)

    ### DICT
    if expected_type == "dict" and not isinstance(value, dict):
        raise ValueError(value_error_str)
__check_values
__check_values() -> None

Validate found values against configuration type metadata.

This private method iterates over the configuration items and checks each found value only if the configuration defines it. Supported types include string, bool, int, float, list, and dict.

Raises:
  • ValueError –

    If a parsed value fails type validation or if the configured type is not supported.

Returns:
  • None –

    None

Source code in fejao_config_parser/src/values/values_check.py
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
def __check_values(self) -> None:
    """Validate found values against configuration type metadata.

    This private method iterates over the configuration items and checks
    each found value only if the configuration defines it. Supported types
    include string, bool, int, float, list, and dict.

    Raises:
        ValueError: If a parsed value fails type validation or if the
            configured type is not supported.

    Returns:
        None
    """
    for item in self.config:
        if item['name'] in self.values_found.keys():
            name_value = self.values_found[item['name']]
            if item["type"].lower() not in [
                "string", "str",
                "bool", "boolean",
                "int", "integer",
                "float", "double",
                "list", "lst",
                "dict", "dictionary"
            ]:
                raise ValueError(
                    f"\n\nError checking the configuration values for '{self.info_string}'. "
                    f"The type '{item['type']}' for the name '{item['name']}' is not supported. "
                    f"The accepted types are: {DEFAULT_ACCEPTED_KEY_TYPES}. "
                    "Please refer to the documentation for more details.\n"
                )

            ### STRING
            if item["type"].lower() == "string" or item["type"].lower() == "str":
                self.__check_types(name=item['name'], value=name_value, expected_type="str")

            ### BOOL
            elif item["type"].lower() == "bool" or item["type"].lower() == "boolean":
                self.__check_types(name=item['name'], value=name_value, expected_type="bool")

            ### INT
            elif item["type"].lower() == "int" or item["type"].lower() == "integer":
                self.__check_types(name=item['name'], value=name_value, expected_type="int")

            ### FLOAT
            elif item["type"].lower() == "float" or item["type"].lower() == "double":
                self.__check_types(name=item['name'], value=name_value, expected_type="float")

            ### LIST
            elif item["type"].lower() == "list" or item["type"].lower() == "lst":
                self.__check_types(name=item['name'], value=name_value, expected_type="list")

            ### DICT
            elif item["type"].lower() == "dict" or item["type"].lower() == "dictionary":
                self.__check_types(name=item['name'], value=name_value, expected_type="dict")
__init__
__init__(config: list, values_found: dict, info_string: str) -> None

Create a ValuesCheck instance and validate parsed values.

Parameters:
  • config (list) –

    List of configuration dictionaries.

  • values_found (dict) –

    Parsed values that should be validated.

  • info_string (str) –

    Context string used for error messages.

Raises:
  • ValueError –

    If a parsed value does not match its expected type.

Source code in fejao_config_parser/src/values/values_check.py
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
def __init__(self, config: list, values_found: dict, info_string: str) -> None:
    """Create a ValuesCheck instance and validate parsed values.

    Args:
        config (list): List of configuration dictionaries.
        values_found (dict): Parsed values that should be validated.
        info_string (str): Context string used for error messages.

    Raises:
        ValueError: If a parsed value does not match its expected type.
    """
    self.config = config
    self.values_found = values_found
    self.info_string = info_string
    self.__check_values()