From 2538758771e7f9ef8c8fd2ff33b24d2a6fef2661 Mon Sep 17 00:00:00 2001 From: Ernesto Ruge Date: Sun, 25 Jul 2021 08:59:58 +0200 Subject: [PATCH 1/2] use enum values for enumfield --- src/wtfjson/validators/EnumValidator.py | 10 +++++----- tests/EnumTest.py | 2 +- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/wtfjson/validators/EnumValidator.py b/src/wtfjson/validators/EnumValidator.py index 4571b06..49f50a4 100644 --- a/src/wtfjson/validators/EnumValidator.py +++ b/src/wtfjson/validators/EnumValidator.py @@ -7,7 +7,7 @@ """ from enum import Enum -from typing import Optional, Any, Union, TYPE_CHECKING +from typing import Optional, Any, Union, TYPE_CHECKING, Type from ..fields import Field from ..validators import Validator @@ -20,11 +20,11 @@ class EnumValidator(Validator): default_message = 'value not in enum' - def __init__(self, enum: Enum, message: Optional[str] = None): + def __init__(self, enum: Type[Enum], message: Optional[str] = None): super().__init__(message) - self.enum = enum + self.enum: Type[Enum] = enum def __call__(self, value: Any, form: Union['DictInput', 'ListInput'], field: Field): - if type(field.data_processed) is not str or not hasattr(self.enum, field.data_processed): + if type(field.data_processed) is not str or field.data_processed not in [item.value for item in self.enum]: raise ValidationError(self.message) - field.data_processed = getattr(self.enum, field.data_processed) + field.data_processed = self.enum(field.data_processed) diff --git a/tests/EnumTest.py b/tests/EnumTest.py index 5eee89f..5551fc6 100644 --- a/tests/EnumTest.py +++ b/tests/EnumTest.py @@ -23,7 +23,7 @@ class EnumDictInput(DictInput): class EnumTest(TestCase): def test_success(self): - form = EnumDictInput(data={'test_field': 'apple'}) + form = EnumDictInput(data={'test_field': 'juicy apple'}) assert form.validate() is True assert form.has_errors is False assert form.errors == {} From 9f52d91cbe5dfe5d356227e35c0466e336d2e4d3 Mon Sep 17 00:00:00 2001 From: Ernesto Ruge Date: Tue, 3 Aug 2021 09:08:59 +0200 Subject: [PATCH 2/2] fix list length validator --- src/wtfjson/validators/ListLength.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/wtfjson/validators/ListLength.py b/src/wtfjson/validators/ListLength.py index fdb2603..f78c2fa 100644 --- a/src/wtfjson/validators/ListLength.py +++ b/src/wtfjson/validators/ListLength.py @@ -28,5 +28,5 @@ def __init__(self, self.max_entries = max_entries def __call__(self, value: Any, form: Union['ListInput', 'DictInput'], field: Field): - if len(value) < self.min_entries or (self.max_entries is not None and len(field.data) < self.max_entries): + if len(value) < self.min_entries or (self.max_entries is not None and len(value) > self.max_entries): raise ValidationError(self.message)