Skip to content

Commit

Permalink
Валидация URLSModel вывод url_checks от большего к меньшему
Browse files Browse the repository at this point in the history
  • Loading branch information
KAnanev committed Jul 5, 2023
1 parent 4400256 commit 4926ea4
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions page_analyzer/models.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import Optional, List
from typing import Optional, List, Dict, Union
from urllib.parse import urlparse

import validators
Expand All @@ -18,12 +18,12 @@ class URLModel(BaseModel):
name: str = Field(max_length=URL_MAX_LENGTH)

@field_validator('name')
def validate_url(cls, value: str) -> str:
def validate_url(cls, value: str) -> str | None:

if validators.url(value):
url = urlparse(value)
return f'{url.scheme}:https://{url.netloc}'.lower()
raise ValueError('Некорректный URL')
return None


class URLChecks(URLBaseMixin):
Expand All @@ -36,3 +36,8 @@ class URLChecks(URLBaseMixin):

class URLSModel(URLBaseMixin, URLModel):
url_checks: Optional[List[URLChecks]] = []

@field_validator('url_checks')
def validate_url_checks(cls, value: List) -> List[Dict[str, Union[str, int]]]:
value.sort(key=lambda x: -x.id)
return value

0 comments on commit 4926ea4

Please sign in to comment.