From 6a11672f9b3a0b4b48843ff5e39daae1f0e5ea89 Mon Sep 17 00:00:00 2001
From: Elena ``of Valhalla'' Grandi <valhalla@trueelena.org>
Date: Sun, 9 Feb 2020 18:28:46 +0100
Subject: Fail validation for entries with empty list fields

---
 lesana/collection.py                                      | 15 ++++++++++++---
 .../complex/items/28b15099c84b41ab892133cd64876a32.yaml   |  3 ++-
 .../complex/items/73097121f1874a6ea2f927db7dc4f11e.yaml   |  1 +
 .../complex/items/d4f361b0e3e541508eaf82c04451797f.yaml   |  4 ++--
 .../wrong/items/b9a832309c984ada9f267471660c1313.yaml     |  1 +
 tests/data/wrong/settings.yaml                            |  7 ++++++-
 tests/test_collection.py                                  |  8 ++++----
 7 files changed, 28 insertions(+), 11 deletions(-)

diff --git a/lesana/collection.py b/lesana/collection.py
index d198ef0..4bec594 100644
--- a/lesana/collection.py
+++ b/lesana/collection.py
@@ -86,10 +86,10 @@ class Entry(object):
         valid = True
         for field in self.collection.settings['fields']:
             value = self.data.get(field['name'], None)
-            if not value:
-                # empty fields are always fine
-                continue
             t = field['type']
+            if t != 'list' and not value:
+                # empty fields are always fine except for lists
+                continue
             if t == 'integer':
                 try:
                     int(value)
@@ -123,6 +123,15 @@ class Entry(object):
                             value
                             ),
                         })
+            elif t == 'list':
+                if not hasattr(value, '__iter__'):
+                    valid = False
+                    errors.append({
+                        'field': field['name'],
+                        'error': 'Invalid value for list field: {}'.format(
+                            value
+                            ),
+                        })
         return valid, errors
 
     def render(self, template, searchpath='.'):
diff --git a/tests/data/complex/items/28b15099c84b41ab892133cd64876a32.yaml b/tests/data/complex/items/28b15099c84b41ab892133cd64876a32.yaml
index 58b84bb..54db12f 100644
--- a/tests/data/complex/items/28b15099c84b41ab892133cd64876a32.yaml
+++ b/tests/data/complex/items/28b15099c84b41ab892133cd64876a32.yaml
@@ -3,4 +3,5 @@ description: |
   .
 position: 'somewhere'
 something: ''
-tags:
+tags: []
+keywords: []
diff --git a/tests/data/complex/items/73097121f1874a6ea2f927db7dc4f11e.yaml b/tests/data/complex/items/73097121f1874a6ea2f927db7dc4f11e.yaml
index b6787b0..f38a56a 100644
--- a/tests/data/complex/items/73097121f1874a6ea2f927db7dc4f11e.yaml
+++ b/tests/data/complex/items/73097121f1874a6ea2f927db7dc4f11e.yaml
@@ -9,3 +9,4 @@ tags:
     - this
     - that
 exists: true
+keywords: []
diff --git a/tests/data/complex/items/d4f361b0e3e541508eaf82c04451797f.yaml b/tests/data/complex/items/d4f361b0e3e541508eaf82c04451797f.yaml
index 2331e69..d2be3f3 100644
--- a/tests/data/complex/items/d4f361b0e3e541508eaf82c04451797f.yaml
+++ b/tests/data/complex/items/d4f361b0e3e541508eaf82c04451797f.yaml
@@ -3,5 +3,5 @@ description: |
     This entry has no tags and no keywords
 position: ''
 something:
-tags:
-keywords:
+tags: []
+keywords: []
diff --git a/tests/data/wrong/items/b9a832309c984ada9f267471660c1313.yaml b/tests/data/wrong/items/b9a832309c984ada9f267471660c1313.yaml
index b36cc4e..1fa0c74 100644
--- a/tests/data/wrong/items/b9a832309c984ada9f267471660c1313.yaml
+++ b/tests/data/wrong/items/b9a832309c984ada9f267471660c1313.yaml
@@ -5,3 +5,4 @@ position: 'somewhere'
 number: 'four'
 float: 'half and a bit'
 price: 'cheap'
+things:
diff --git a/tests/data/wrong/settings.yaml b/tests/data/wrong/settings.yaml
index 243ab62..83a542b 100644
--- a/tests/data/wrong/settings.yaml
+++ b/tests/data/wrong/settings.yaml
@@ -1,5 +1,6 @@
 name: "Lesana collection with certain errors"
 lang: 'somethingish'
+entry_label: '{{ short_id }}: {{ name }} - {{ things | join("; ") }}'
 fields:
     - name: name
       type: string
@@ -8,7 +9,7 @@ fields:
       type: text
     - name: position
       type: string
-      index: facet
+      index: field
     - name: number
       type: integer
       help: "Enter an integer here"
@@ -18,3 +19,7 @@ fields:
     - name: price
       type: decimal
       help: 'prices are never float!'
+    - name: things
+      type: list
+      list: string
+      index: field
diff --git a/tests/test_collection.py b/tests/test_collection.py
index 798e561..e97e912 100644
--- a/tests/test_collection.py
+++ b/tests/test_collection.py
@@ -54,8 +54,8 @@ class testCollection(unittest.TestCase):
         self.assertIsNotNone(self.collection.settings)
         self.assertIsNotNone(self.collection.stemmer)
         # Fields with no "index" entry are not indexed
-        self.assertEqual(len(self.collection.settings['fields']), 6)
-        self.assertEqual(len(self.collection.indexed_fields), 1)
+        self.assertEqual(len(self.collection.settings['fields']), 7)
+        self.assertEqual(len(self.collection.indexed_fields), 3)
 
     def test_load_safe(self):
         self.collection = lesana.Collection('tests/data/simple')
@@ -326,9 +326,9 @@ class testCollectionWithErrors(unittest.TestCase):
             self.collection.settings['name'],
             "Lesana collection with certain errors"
             )
-        self.assertEqual(len(self.collection.settings['fields']), 6)
+        self.assertEqual(len(self.collection.settings['fields']), 7)
         self.assertIsNotNone(self.collection.stemmer)
-        self.assertEqual(len(self.collection.indexed_fields), 1)
+        self.assertEqual(len(self.collection.indexed_fields), 3)
 
     def test_index(self):
         loaded = self.collection.update_cache()
-- 
cgit v1.2.3