Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable whitespace selection in choices #175

Merged
merged 1 commit into from
Jul 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading