Repository: janehmueller/python-json-config

Issue:
Validation of optional fields throws errors when the field does not exist
This was an oversight in #8 

Comments:

Patch:
diff --git a/python_json_config/__version__.py b/python_json_config/__version__.py
index b3ddbc4..7b344ec 100644
--- a/python_json_config/__version__.py
+++ b/python_json_config/__version__.py
@@ -1,1 +1,1 @@
-__version__ = '1.1.1'
+__version__ = '1.1.2'
diff --git a/python_json_config/bin/activate b/python_json_config/bin/activate
new file mode 120000
index 0000000..a045e45
--- /dev/null
+++ b/python_json_config/bin/activate
@@ -0,0 +1,1 @@
+//anaconda/bin/activate
\ No newline at end of file
diff --git a/python_json_config/bin/conda b/python_json_config/bin/conda
new file mode 120000
index 0000000..6f1d222
--- /dev/null
+++ b/python_json_config/bin/conda
@@ -0,0 +1,1 @@
+//anaconda/bin/conda
\ No newline at end of file
diff --git a/python_json_config/bin/deactivate b/python_json_config/bin/deactivate
new file mode 120000
index 0000000..5c79faf
--- /dev/null
+++ b/python_json_config/bin/deactivate
@@ -0,0 +1,1 @@
+//anaconda/bin/deactivate
\ No newline at end of file
diff --git a/python_json_config/config_builder.py b/python_json_config/config_builder.py
index 8607631..5eb57f0 100644
--- a/python_json_config/config_builder.py
+++ b/python_json_config/config_builder.py
@@ -29,6 +29,9 @@ class ConfigBuilder(object):
     def __validate_types(self):
         for field_name, field_type in self.__validation_types.items():
             value = self.__config.get(field_name)
+            # skip optional fields that do not exist
+            if value is None:
+                continue
             assert isinstance(value, field_type), f'Config field "{field_name}" with value "{value}" is not of type {field_type}'
 
     def validate_field_value(self, field_name: str, validation_function):
@@ -45,6 +48,9 @@ class ConfigBuilder(object):
     def __validate_field_values(self):
         for field_name, validation_functions in self.__validation_functions.items():
             value = self.__config.get(field_name)
+            # skip optional fields that do not exist
+            if value is None:
+                continue
             for validation_function in validation_functions:
                 validation_result = validation_function(value)
                 error_message = f'Error validating field "{field_name}" with value "{value}"'
@@ -62,6 +68,9 @@ class ConfigBuilder(object):
     def __transform_field_values(self):
         for field_name, transformation_function in self.__transformation_functions.items():
             value = self.__config.get(field_name)
+            # skip optional fields that do not exist
+            if value is None:
+                continue
             new_value = transformation_function(value)
             self.__config.update(field_name, new_value)
 


Test patch:
diff --git a/tests/config_builder_test.py b/tests/config_builder_test.py
index 9d9a492..6e5c8dc 100644
--- a/tests/config_builder_test.py
+++ b/tests/config_builder_test.py
@@ -142,3 +142,10 @@ class ConfigBuilderTest(TestCase):
             config.cache.name2
 
 
+    def test_optional_validation(self):
+        builder = ConfigBuilder()
+        builder.set_field_access_optional()
+        builder.validate_field_type('cache.name', str)
+        builder.validate_field_value('cache.host', 'localhost')
+        builder.transform_field_value('cache.host', lambda name: f"https://{name}")
+        builder.parse_config(self.path)