Skip to content

Commit

Permalink
сheck specializations for unique values
Browse files Browse the repository at this point in the history
  • Loading branch information
Genek91 committed Sep 20, 2024
1 parent 662fa21 commit 54c58f7
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions src/api/schemas/external_site_user.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,20 +33,26 @@ class BaseExternalSiteUserVolunteer(RequestBase):
specializations: list[int] | None = Field(None)
has_mailing_new_tasks: bool = None

@classmethod
def validate_unique_values(cls, values: list[int]) -> list[int]:
if len(set(values)) != len(values):
raise ValueError("В поле specializations не должно быть дублирующихся значений")
return values

@field_validator("specializations", mode="before")
@classmethod
def specializations_str_validation(cls, value: str | list[int]) -> list[int] | None:
def specializations_validation(cls, value: str | list[int]) -> list[int] | None:
if not isinstance(value, str):
return value
return cls.validate_unique_values(value)
if value == "":
return []
try:
new_value = [int(value) for value in value.replace(" ", "").split(",")]
return new_value
except ValueError as exc:
raise ValueError(
'Для передачи строки с числами в поле specializations используйте формат: "1, 2, 3"'
) from exc
return cls.validate_unique_values(new_value)


class ExternalSiteUserRequest(BaseExternalSiteUser):
Expand Down

0 comments on commit 54c58f7

Please sign in to comment.