Skip to content
This repository has been archived by the owner on Apr 10, 2023. It is now read-only.

Add equal and length validators #69

Merged
merged 2 commits into from
Jan 25, 2019
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 5 additions & 1 deletion RELEASES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,14 @@ Releases

*Unreleased*

- Add basestring and unicode to built-in Field map (`#68`_)
- Add equal and length validators. (`#67`_, `#69`_)
- Add basestring and unicode to built-in Field map. (`#68`_)

.. _#69: https://github.com/rossmacarthur/serde/pull/69
.. _#68: https://github.com/rossmacarthur/serde/pull/68

.. _#67: https://github.com/rossmacarthur/serde/issues/67

0.4.1
-----

Expand Down
139 changes: 139 additions & 0 deletions src/serde/validate.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,36 @@ def instance_(value):
return instance_


def equal(to):
"""
Validate that a value is equal to something.

Args:
to: the value to check against.

Returns:
function: the validator function.
"""
def equal_(value):
"""
Validate that the given value is equal to the specified value.

Args:
value: the value to validate.

Raises:
`~serde.exceptions.ValidationError`: when the value is not equal to
the expected value.
"""
if value != to:
raise ValidationError(
'expected {!r} but got {!r}'
.format(to, value)
)

return equal_


def min(endpoint, inclusive=True):
"""
Validate that a value is greater than and/or equal to the given endpoint.
Expand Down Expand Up @@ -159,6 +189,115 @@ def between_(value):
return between_


def length(length):
"""
Validate that the given value has a particular length.

Args:
length (int): the length allowed.

Returns:
function: the validator function.
"""
def length_(value):
"""
Validate that the given value has the expected length.

Args:
value: the value to validate.

Raises:
`~serde.exceptions.ValidationError`: when the value does not have
the expected length.
"""
equal(length)(len(value))

return length_


def length_min(endpoint, inclusive=True):
"""
Validate that a value's length is greater than/or equal to a minimum.

Args:
endpoint: the minimum length allowed.
inclusive (bool): whether the minimum length value itself allowed.

Returns:
function: the validator function.
"""
def length_min_(value):
"""
Validate that a value's length is greater than/or equal to a minimum.

Args:
value: the value to validate.

Raises:
`~serde.exceptions.ValidationError`: when the value's length is
greater than and/or equal to the maximum.
"""
min(endpoint, inclusive=inclusive)(len(value))

return length_min_


def length_max(endpoint, inclusive=True):
"""
Validate that a value's length is less than/or equal to a maximum.

Args:
endpoint: the maximum length allowed.
inclusive (bool): whether the maximum length value itself allowed.

Returns:
function: the validator function.
"""
def length_max_(value):
"""
Validate that a value's length is less than/or equal to a maximum.

Args:
value: the value to validate.

Raises:
`~serde.exceptions.ValidationError`: when the value's length is
less than and/or equal to the maximum.
"""
max(endpoint, inclusive=inclusive)(len(value))

return length_max_


def length_between(min_endpoint, max_endpoint, inclusive=True):
"""
Validate that the given value's length is between a minimum and maximum.

Args:
min_endpoint: the minimum length allowed.
max_endpoint: the maximum length allowed.
inclusive (bool): whether the endpoint length values are allowed.

Returns:
function: the validator function.
"""
def length_between_(value):
"""
Validate that the given value's length is between a minimum and maximum.

Args:
value: the value to validate.

Raises:
`~serde.exceptions.ValidationError`: when the value's length is not
between the minimum and maximum.
"""
min(min_endpoint, inclusive=inclusive)(len(value))
max(max_endpoint, inclusive=inclusive)(len(value))

return length_between_


def contains(allowed):
"""
Validate that the given list/range/tuple contains the given value.
Expand Down
47 changes: 47 additions & 0 deletions tests/test_validate.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,14 @@ def test_instance():
validate.instance(float)(1)


def test_equal():
value = object()
validate.equal(value)(value)

with raises(ValidationError):
validate.equal(object())(20)


def test_min():
validate.min(100)(100)
validate.min(100)(1000)
Expand Down Expand Up @@ -53,6 +61,45 @@ def test_between():
validate.between(-100, 100, inclusive=False)(100)


def test_length():
validate.length(10)(range(10))
validate.length(3)([1, 2, 3])

with raises(ValidationError):
validate.length(10)((1, 2))


def test_length_min():
validate.length_min(5)(range(10))
validate.length_min(5, inclusive=False)(range(6))

with raises(ValidationError):
validate.length_min(5)(range(2))

with raises(ValidationError):
validate.length_min(5, inclusive=False)(range(5))


def test_length_max():
validate.length_max(5)(range(2))

with raises(ValidationError):
validate.length_max(5)(range(10))

with raises(ValidationError):
validate.length_max(5, inclusive=False)(range(5))


def test_length_between():
validate.length_between(0, 10)(range(10))

with raises(ValidationError):
validate.length_between(10, 100)(range(5))

with raises(ValidationError):
validate.length_between(0, 10, inclusive=False)([])


def test_contains():
validate.contains(range(5))(3)
validate.contains((0, 1, 2, 3, 4))(3)
Expand Down