Repository: carpentries/amy

Issue:
Website validation gives incorrect answer for empty fields
1.  Go to https://amy.software-carpentry.org/workshops/event/2016-07-11-ttt-scipy.
2.  Click 'validate event'.
3.  Error message reports 'helper' and 'eventbrite' are invalid.

Both are present, but empty.



Discussion:
If `helper` is empty, it's invalid.

The behavior for `eventbrite` is slightly off, I'll fix it.


Resolving patch:
diff --git a/workshops/util.py b/workshops/util.py
--- a/workshops/util.py
+++ b/workshops/util.py
@@ -615,10 +615,11 @@ def validate_metadata_from_event_website(metadata):
         name_ = ('{display} ({name})'.format(**d_)
                  if requirement.display
                  else '{name}'.format(**d_))
-        type_ = 'required' if requirement.required else 'optional'
+        required_ = requirement.required
+        type_ = 'required' if required_ else 'optional'
         value_ = metadata.get(requirement.name)
 
-        if not value_:
+        if value_ is None:
             errors.append('Missing {} metadata {}.'.format(type_, name_))
 
         if value_ == 'FIXME':
@@ -626,12 +627,14 @@ def validate_metadata_from_event_website(metadata):
                           .format(type_, name_))
         else:
             try:
-                if not re.match(requirement.match_format, value_):
-                    errors.append(
-                        'Invalid value "{}" for {} metadata {}: should be in '
-                        'format "{}".'
-                        .format(value_, type_, name_, requirement.match_format)
-                    )
+                if required_ or value_:
+                    if not re.match(requirement.match_format, value_):
+                        errors.append(
+                            'Invalid value "{}" for {} metadata {}: should be'
+                            ' in format "{}".'
+                            .format(value_, type_, name_,
+                                    requirement.match_format)
+                        )
             except (re.error, TypeError):
                 pass
 


Test patch:
diff --git a/workshops/test/test_util.py b/workshops/test/test_util.py
--- a/workshops/test/test_util.py
+++ b/workshops/test/test_util.py
@@ -756,6 +756,27 @@ def test_validating_missing_metadata(self):
         assert len(errors) == 12
         assert all([error.startswith('Missing') for error in errors])
 
+    def test_validating_empty_metadata(self):
+        metadata = {
+            'slug': '',
+            'language': '',
+            'startdate': '',
+            'enddate': '',
+            'country': '',
+            'venue': '',
+            'address': '',
+            'latlng': '',
+            'instructor': '',
+            'helper': '',
+            'contact': '',
+            'eventbrite': '',
+        }
+        expected_errors = ['slug', 'startdate', 'country', 'latlng',
+                           'instructor', 'helper', 'contact']
+        errors = validate_metadata_from_event_website(metadata)
+        for error, key in zip(errors, expected_errors):
+            self.assertIn(key, error)
+
     def test_validating_default_metadata(self):
         metadata = {
             'slug': 'FIXME',