Skip to content

Commit

Permalink
Merge cc6816b into a484752
Browse files Browse the repository at this point in the history
  • Loading branch information
gbastien committed Oct 15, 2018
2 parents a484752 + cc6816b commit eea8b02
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 7 deletions.
6 changes: 6 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@ Changelog
[sgeulette]
- Added methods content.uuidsToCatalogBrains and content.uuidsToObjects.
[gbastien]
- Adapted `content.validate_fields` to bypass validation when field.required=False,
value is None and field type is other than Bool.
[gbastien]
- Added parameter raise_on_errors to content.validate_fields to raise a ValueError
when errors are found instead simply returning it.
[gbastien]

0.13 (2018-08-31)
-----------------
Expand Down
12 changes: 7 additions & 5 deletions src/imio/helpers/content.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from zope.annotation import IAnnotations
from zope.component import getUtility
from zope.interface.interfaces import IMethod
from zope.schema._field import Choice
from zope.schema._field import Bool

import logging
import os
Expand Down Expand Up @@ -236,7 +236,7 @@ def get_schema_fields(obj=None, type_name=None, behaviors=True):
return fields


def validate_fields(obj, behaviors=True):
def validate_fields(obj, behaviors=True, raise_on_errors=False):
"""
Validates every fields of given p_obj.
"""
Expand All @@ -245,13 +245,15 @@ def validate_fields(obj, behaviors=True):
for (field_name, field) in fields:
field = field.bind(obj)
value = getattr(obj, field_name)
# accept None for most of fields if required=False
if value is None and not field.required and not (isinstance(field, (Bool, ))):
continue
try:
field._validate(value)
except Exception, exc:
# bypass for Choice field not required, accept a None value
if isinstance(field, Choice) and not field.required and value is None:
continue
errors.append(exc)
if raise_on_errors and errors:
raise ValueError(str(errors))
return errors


Expand Down
10 changes: 10 additions & 0 deletions src/imio/helpers/profiles/testing/types/testingtype.xml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,16 @@
<title>Enabled</title>
<required>False</required>
</field>
<field name="textline" type="zope.schema.TextLine">
<description/>
<title>Text line</title>
<required>False</required>
</field>
<field name="mandatory_textline" type="zope.schema.TextLine">
<description/>
<title>Mandatory text line</title>
<required>True</required>
</field>
</schema>
</model>
</property>
Expand Down
14 changes: 12 additions & 2 deletions src/imio/helpers/tests/test_content.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,9 +138,10 @@ def test_get_schema_fields(self):
type='testingtype',
enabled='Should be a boolean')
self.assertListEqual([name for (name, fld) in get_schema_fields(obj=obj, behaviors=False)],
['text', 'enabled'])
['text', 'enabled', 'mandatory_textline', 'textline'])
self.assertListEqual([name for (name, fld) in get_schema_fields(obj=obj)],
['text', 'enabled', 'description', 'title', 'title',
['text', 'enabled', 'mandatory_textline', 'textline',
'description', 'title', 'title',
'tal_condition', 'roles_bypassing_talcondition'])
self.assertListEqual([name for (name, fld) in get_schema_fields(type_name='portnawak')],
[])
Expand All @@ -151,6 +152,8 @@ def test_validate_fields(self):
id='tt',
type='testingtype',
enabled='Should be a boolean',
textline=None,
mandatory_textline=u'Some text',
tal_condition=u'',
roles_bypassing_talcondition=set())
self.assertEqual(validate_fields(obj),
Expand All @@ -159,6 +162,13 @@ def test_validate_fields(self):
self.assertFalse(validate_fields(obj))
obj.enabled = True
self.assertFalse(validate_fields(obj))
# not required fields other than Bool must contain something
# else than None if field is required=True
obj.mandatory_textline = None
self.assertEqual(validate_fields(obj),
[WrongType(None, unicode, 'mandatory_textline')])
obj.mandatory_textline = u'Some text'
self.assertFalse(validate_fields(obj))

def test_safe_encode(self):
self.assertEqual(safe_encode('héhé'), 'héhé')
Expand Down

0 comments on commit eea8b02

Please sign in to comment.