Skip to content

Commit

Permalink
Merge pull request #17 from Cadasta/default-none
Browse files Browse the repository at this point in the history
Allow empty strings for non-required fields
  • Loading branch information
oliverroick authored Apr 20, 2017
2 parents ee6cb98 + 322fb08 commit 76bdf43
Show file tree
Hide file tree
Showing 2 changed files with 133 additions and 3 deletions.
15 changes: 12 additions & 3 deletions jsonattrs/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -281,13 +281,22 @@ def choice_labels(self, value):
self.choice_labels_xlat = value

def validate(self, value):
empty_vals = ('', [''], )

if (self.required and self.default == '' and
(value is None or value == '')):
(value is None or value in empty_vals)):
raise ValidationError(
_('Missing required field %(field)s'),
params={'field': self.name}
)
if self.choices is not None and self.choices != []:

atype = self.attr_type
if (value in empty_vals and
atype.name in ('integer', 'decimal', 'select_one',
'select_multiple')):
value = None

if self.choices is not None and self.choices != [] and value:
if type(value) == list:
for v in value:
if v not in self.choices:
Expand All @@ -300,7 +309,7 @@ def validate(self, value):
_('Invalid choice for %(field)s: "%(value)s"'),
params={'field': self.name, 'value': value}
)
atype = self.attr_type

if isinstance(value, str):
if (atype.validator_re is not None and
re.match(atype.validator_re, value) is None):
Expand Down
121 changes: 121 additions & 0 deletions tests/test_validation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
import pytest
from django.test import TestCase
from django.core.exceptions import ValidationError
from jsonattrs.models import Schema, Attribute, AttributeType

from .fixtures import create_fixtures


class ValidationTest(TestCase):
def setUp(self):
self.fixtures = create_fixtures(do_schemas=False, load_attr_types=True)
self.schema = Schema.objects.create(
content_type=self.fixtures['party_t'], selectors=()
)

def test_validate_empty_integer(self):
attr = Attribute.objects.create(
schema=self.schema,
name='testattr',
long_name='Test attribute',
index=1,
attr_type=AttributeType.objects.get(name='integer')
)

attr.validate('')
# No assertion here, validation should pass without exceptions

def test_validate_empty_integer_on_required_attribute(self):
attr = Attribute.objects.create(
schema=self.schema,
name='testattr',
long_name='Test attribute',
index=1,
attr_type=AttributeType.objects.get(name='integer'),
required=True
)
with pytest.raises(ValidationError):
attr.validate('')

def test_validate_empty_decimal(self):
attr = Attribute.objects.create(
schema=self.schema,
name='testattr',
long_name='Test attribute',
index=1,
attr_type=AttributeType.objects.get(name='decimal')
)

attr.validate('')
# No assertion here, validation should pass without exceptions

def test_validate_empty_decimal_on_required_attribute(self):
attr = Attribute.objects.create(
schema=self.schema,
name='testattr',
long_name='Test attribute',
index=1,
attr_type=AttributeType.objects.get(name='decimal'),
required=True
)
with pytest.raises(ValidationError):
attr.validate('')

def test_validate_empty_select_one(self):
attr = Attribute.objects.create(
schema=self.schema,
name='testattr',
long_name='Test attribute',
index=1,
attr_type=AttributeType.objects.get(name='select_one'),
choices=['a', 'b', 'c']
)

attr.validate('')
attr.validate([''])
# No assertion here, validation should pass without exceptions

def test_validate_empty_select_one_on_required_attribute(self):
attr = Attribute.objects.create(
schema=self.schema,
name='testattr',
long_name='Test attribute',
index=1,
attr_type=AttributeType.objects.get(name='select_one'),
choices=['a', 'b', 'c'],
required=True
)
with pytest.raises(ValidationError):
attr.validate('')
with pytest.raises(ValidationError):
attr.validate([''])

def test_validate_empty_select_multiple(self):
attr = Attribute.objects.create(
schema=self.schema,
name='testattr',
long_name='Test attribute',
index=1,
attr_type=AttributeType.objects.get(name='select_multiple'),
choices=['a', 'b', 'c']
)

attr.validate('')
attr.validate([''])
# No assertion here, validation should pass without exceptions

def test_validate_empty_select_multiple_on_required_attribute(self):
attr = Attribute.objects.create(
schema=self.schema,
name='testattr',
long_name='Test attribute',
index=1,
attr_type=AttributeType.objects.get(name='select_multiple'),
choices=['a', 'b', 'c'],
required=True
)

with pytest.raises(ValidationError):
attr.validate('')
with pytest.raises(ValidationError):
attr.validate([''])

0 comments on commit 76bdf43

Please sign in to comment.