Skip to content

Commit

Permalink
Enable whitespace selection in choices (#175)
Browse files Browse the repository at this point in the history
  • Loading branch information
kyleperik authored Jul 18, 2024
1 parent b0be700 commit 3892068
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 5 deletions.
10 changes: 7 additions & 3 deletions django_jsonform/validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -296,17 +296,21 @@ def validate_object(self, schema, data, coords, raise_exc=False):

def validate_string(self, schema, data, coords, raise_exc=False):
if isinstance(data, str):
data = data.strip()
trimmed_data = data.strip()
else:
trimmed_data = data

is_empty = not trimmed_data

if schema.get('required') and not data:
if schema.get('required') and is_empty:
self.add_error(coords, 'This field is required.', raise_exc=raise_exc)
return

if not isinstance(data, str):
self.add_error(coords, 'This value is invalid. Must be a valid string.', raise_exc=raise_exc)
return

if not data:
if is_empty:
# data not required and is empty
return

Expand Down
4 changes: 2 additions & 2 deletions tests/test_validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -1226,9 +1226,9 @@ def test_validate_string_method(self):
validator(data)

# 7. test choices
schema['properties']['a']['choices'] = ['one', 'two']
schema['properties']['a']['choices'] = ['one', 'two ']
wrong_data = {'a': 'xxx'}
data = {'a': 'two'}
data = {'a': 'two '} # White space in choices is valid
self.assertRaises(JSONSchemaValidationError, validator, wrong_data)
validator(data)

Expand Down

0 comments on commit 3892068

Please sign in to comment.