Skip to content

Commit

Permalink
feat: adds a validation method to array validator to test if an item …
Browse files Browse the repository at this point in the history
…is included on a iterable
  • Loading branch information
danielmbomfim committed Nov 28, 2023
1 parent c2aba5e commit d5b3da0
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 1 deletion.
32 changes: 31 additions & 1 deletion PyYep/validators/array.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import collections
from typing import TypeVar
from typing import TypeVar, Any
from collections.abc import Iterable
from PyYep.validators.validator import Validator
from PyYep.exceptions import ValidationError
Expand Down Expand Up @@ -155,6 +155,36 @@ def max(
if len(value) > max:
raise ValidationError(self.name, "Value too large received")

@validatorMethod
def includes(
self, item: Any, value: Iterable[IterableValueT]
) -> "ArrayValidator":
"""
Verify if iterable contains a given item
Parameters
----------
value : (Iterable[IterableValueT])
the list that will be checked
item : (int)
the value expected to be found on the iterable
Raises
----------
ValidationError:
if the item is not contained on the value
Returns
________
validator (ArrayValidator):
the validator being used
"""

if item not in value:
raise ValidationError(
self.name, f"Value '{item}' not included on iterable"
)

def verify(self) -> dict:
"""
Get the validator's input value, verify if its a list
Expand Down
13 changes: 13 additions & 0 deletions tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,19 @@ def test_min_and_max(self):
with self.assertRaises(ValidationError):
form.validate()

def test_includes(self):
input_ = SimpleInput([1, 2, 3])
form = Schema(
[InputItem("test", input_, "getValue").array().includes(3)]
)

self.assertEqual(form.validate()["test"], [1, 2, 3])

input_.value = [1, 2]

with self.assertRaises(ValidationError):
form.validate()


class SimpleInput:
def __init__(self, value):
Expand Down

0 comments on commit d5b3da0

Please sign in to comment.