Skip to content

Commit

Permalink
<branch> pydantic v2
Browse files Browse the repository at this point in the history
  • Loading branch information
a-pertsev committed Nov 13, 2023
1 parent cf8dc65 commit e7a7f34
Show file tree
Hide file tree
Showing 6 changed files with 157 additions and 71 deletions.
4 changes: 2 additions & 2 deletions docs/arguments-validation.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ For example:
```python
class CustomValidationModel(BaseModel):
custom_int: int
custom_arg: Optional[int]
custom_arg: int | None = None

@validator('custom_arg')
@field_validator('custom_arg')
@classmethod
def validate_custom_arg(cls, value):
assert value % 5 == 0
Expand Down
4 changes: 2 additions & 2 deletions frontik/handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ def get_validated_argument(
if default is not _ARG_DEFAULT and default is not None:
try:
params = {validator: default}
validated_default = self._validation_model(**params).dict().get(validator)
validated_default = self._validation_model(**params).model_dump().get(validator)
except ValidationError:
raise DefaultValueError()
else:
Expand All @@ -247,7 +247,7 @@ def get_validated_argument(

try:
params = {validator: value}
validated_value = self._validation_model(**params).dict().get(validator)
validated_value = self._validation_model(**params).model_dump().get(validator)
except ValidationError:
if default is _ARG_DEFAULT:
raise TypedArgumentError(http.client.BAD_REQUEST, f'"{name}" argument is invalid')
Expand Down
20 changes: 10 additions & 10 deletions frontik/validator.py
Original file line number Diff line number Diff line change
@@ -1,28 +1,28 @@
from enum import Enum

from pydantic import BaseModel, validator
from pydantic import BaseModel, field_validator


class Validators(Enum):
BOOLEAN = 'boolean'
STRING = 'string'
INTEGER = 'integer'
FLOAT = 'float'
FLOAT = 'float_'
LIST_INT = 'list_int'
LIST_STR = 'list_str'
PATH_SAFE_STRING = 'path_safe_string'


class BaseValidationModel(BaseModel):
boolean: bool | None
string: str | None
integer: int | None
float: float | None
list_int: list[int] | None
list_str: list[str] | None
path_safe_string: str | None
boolean: bool | None = None
string: str | None = None
integer: int | None = None
float_: float | None = None
list_int: list[int] | None = None
list_str: list[str] | None = None
path_safe_string: str | None = None

@validator('path_safe_string', pre=True)
@field_validator('path_safe_string', mode='before')
@classmethod
def check_path_safe_string(cls, value):
assert isinstance(value, str)
Expand Down
Loading

0 comments on commit e7a7f34

Please sign in to comment.