Skip to content
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
8 changes: 6 additions & 2 deletions data_spec_validator/spec/validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -370,8 +370,12 @@ def validate(value, extra, data) -> Tuple[bool, Union[Exception, str]]:
RuntimeError('Lower boundary cannot less than 0 for length validator'),
)

ok = lower_bound <= len(value) <= upper_bound if upper_bound else lower_bound <= len(value)
info = '' if ok else ValueError(f'Length of {repr(value)} must be between {lower_bound} and {upper_bound}')
if upper_bound:
ok = lower_bound <= len(value) <= upper_bound
info = '' if ok else ValueError(f'Length of {repr(value)} must be between {lower_bound} and {upper_bound}')
else:
ok = lower_bound <= len(value)
info = '' if ok else ValueError(f'Length of {repr(value)} must be greater than or equal to {lower_bound}')
return ok, info


Expand Down
18 changes: 18 additions & 0 deletions test/test_spec.py
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,24 @@ class LengthSpec:
nok_data = dict(length_field='exceed')
assert is_something_error(ValueError, validate_data_spec, nok_data, LengthSpec)

# assert error message
with self.assertRaises(ValueError) as e:
validate_data_spec(dict(length_field='ah'), LengthSpec)

expected_error_msg = "field: LengthSpec.length_field, reason: Length of 'ah' must be between 3 and 5"
self.assertEqual(str(e.exception), expected_error_msg)

def test_length__without_upper_limit(self):
class LengthSpec:
length_field = Checker([LENGTH], LENGTH=dict(min=3))

# assert error message
with self.assertRaises(ValueError) as e:
validate_data_spec(dict(length_field='ah'), LengthSpec)

expected_error_msg = "field: LengthSpec.length_field, reason: Length of 'ah' must be greater than or equal to 3"
self.assertEqual(str(e.exception), expected_error_msg)

def test_decimal_place(self):
class DecimalPlaceSpec:
decimal_place_field = Checker([DECIMAL_PLACE], DECIMAL_PLACE=4)
Expand Down