Skip to content
This repository has been archived by the owner on Oct 11, 2021. It is now read-only.

Commit

Permalink
Merge branch 'main' into cleanup-version-0.3
Browse files Browse the repository at this point in the history
  • Loading branch information
binaryDiv committed Aug 5, 2021
2 parents 10dc882 + 9f52d91 commit 95d3e5d
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 7 deletions.
10 changes: 5 additions & 5 deletions src/wtfjson/validators/enum_validator.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"""

from enum import Enum
from typing import Optional, Any
from typing import Optional, Any, Type

from ..abstract_input import AbstractInput
from ..fields import Field
Expand All @@ -18,11 +18,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: AbstractInput, 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)
2 changes: 1 addition & 1 deletion src/wtfjson/validators/list_length.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,5 @@ def __init__(self,
self.max_entries = max_entries

def __call__(self, value: Any, form: AbstractInput, 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)
2 changes: 1 addition & 1 deletion tests/fields/enum_field_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class EnumDictInput(DictInput):

class EnumFieldTest(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 == {}
Expand Down

0 comments on commit 95d3e5d

Please sign in to comment.