Skip to content

Commit

Permalink
added validation schemas for patch method
Browse files Browse the repository at this point in the history
  • Loading branch information
novichikhin committed Dec 3, 2023
1 parent 651d3e0 commit cbc9fca
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 22 deletions.
22 changes: 17 additions & 5 deletions talkcorner/server/schemas/forum.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,13 @@

from talkcorner.server.schemas.base import BaseSchema, BasePatch

TITLE_MIN_LENGTH, TITLE_MAX_LENGTH = 10, 64
DESCRIPTION_MIN_LENGTH, DESCRIPTION_MAX_LENGTH = 10, 256


class ForumValidators(BaseSchema):
title: str = Field(min_length=10, max_length=64)
description: Optional[str] = Field(min_length=10, max_length=256)
title: str = Field(min_length=TITLE_MIN_LENGTH, max_length=TITLE_MAX_LENGTH)
description: Optional[str] = Field(min_length=DESCRIPTION_MIN_LENGTH, max_length=DESCRIPTION_MAX_LENGTH)


class Forum(ForumValidators):
Expand All @@ -24,6 +27,15 @@ class ForumCreate(ForumValidators):
pass


class ForumPatch(ForumValidators, BasePatch):
title: Optional[str] = Field(default=None) # type: ignore
description: Optional[str] = Field(default=None) # type: ignore
class ForumPatch(BasePatch):
title: Optional[str] = Field(
default=None,
min_length=TITLE_MIN_LENGTH,
max_length=TITLE_MAX_LENGTH
)

description: Optional[str] = Field(
default=None,
min_length=DESCRIPTION_MIN_LENGTH,
max_length=DESCRIPTION_MAX_LENGTH
)
14 changes: 10 additions & 4 deletions talkcorner/server/schemas/subforum.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import uuid
from typing import Optional, Any
from typing import Optional

from pydantic import model_validator, Field

Expand Down Expand Up @@ -31,6 +31,12 @@ class SubforumCreate(SubforumValidators):
child_forum_id: int


class SubforumPatch(SubforumValidators, BasePatch):
parent_forum_id: Optional[int] = Field(default=None) # type: ignore
child_forum_id: Optional[int] = Field(default=None) # type: ignore
class SubforumPatch(BasePatch):
parent_forum_id: Optional[int] = Field(default=None)
child_forum_id: Optional[int] = Field(default=None)

@model_validator(mode="after")
def model_validator_subforum(self) -> "SubforumPatch":
if self.parent_forum_id and self.child_forum_id and self.parent_forum_id == self.child_forum_id:
raise ValueError("parent_forum_id should not be equal child_forum_id")
return self
25 changes: 19 additions & 6 deletions talkcorner/server/schemas/topic/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,13 @@

from talkcorner.server.schemas.base import BaseSchema, BasePatch

TITLE_MIN_LENGTH, TITLE_MAX_LENGTH = 10, 48
BODY_MIN_LENGTH, BODY_MAX_LENGTH = 1, 4096


class TopicValidators(BaseSchema):
title: str = Field(min_length=10, max_length=48)
body: str = Field(min_length=1, max_length=4096)
title: str = Field(min_length=TITLE_MIN_LENGTH, max_length=TITLE_MAX_LENGTH)
body: str = Field(min_length=BODY_MIN_LENGTH, max_length=BODY_MAX_LENGTH)


class Topic(TopicValidators):
Expand All @@ -26,7 +29,17 @@ class TopicCreate(TopicValidators):
forum_id: int


class TopicPatch(TopicValidators, BasePatch):
forum_id: Optional[int] = Field(default=None) # type: ignore
title: Optional[str] = Field(default=None) # type: ignore
body: Optional[str] = Field(default=None) # type: ignore
class TopicPatch(BasePatch):
forum_id: Optional[int] = Field(default=None)

title: Optional[str] = Field(
default=None,
min_length=TITLE_MIN_LENGTH,
max_length=TITLE_MAX_LENGTH
)

body: Optional[str] = Field(
default=None,
min_length=BODY_MIN_LENGTH,
max_length=BODY_MAX_LENGTH
)
12 changes: 9 additions & 3 deletions talkcorner/server/schemas/topic/message.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@

from talkcorner.server.schemas.base import BaseSchema, BasePatch

BODY_MIN_LENGTH, BODY_MAX_LENGTH = 1, 4096


class TopicMessageValidators(BaseSchema):
body: str = Field(min_length=1, max_length=4096)
body: str = Field(min_length=BODY_MIN_LENGTH, max_length=BODY_MAX_LENGTH)


class TopicMessage(TopicMessageValidators):
Expand All @@ -25,5 +27,9 @@ class TopicMessageCreate(TopicMessageValidators):
topic_id: uuid.UUID


class TopicMessagePatch(TopicMessageValidators, BasePatch):
body: Optional[str] = Field(default=None) # type: ignore
class TopicMessagePatch(BasePatch):
body: Optional[str] = Field(
default=None,
min_length=BODY_MIN_LENGTH,
max_length=BODY_MAX_LENGTH
)
15 changes: 11 additions & 4 deletions talkcorner/server/schemas/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,13 @@

from talkcorner.server.schemas.base import BaseSchema, BasePatch

USERNAME_MIN_LENGTH, USERNAME_MAX_LENGTH = 4, 24
PASSWORD_MIN_LENGTH, PASSWORD_MAX_LENGTH = 8, 64


class UserValidators(BaseSchema):
username: str = Field(min_length=4, max_length=24)
password: str = Field(min_length=8, max_length=64)
username: str = Field(min_length=USERNAME_MIN_LENGTH, max_length=USERNAME_MAX_LENGTH)
password: str = Field(min_length=PASSWORD_MIN_LENGTH, max_length=PASSWORD_MAX_LENGTH)


class User(UserValidators):
Expand All @@ -30,5 +33,9 @@ class UserCreate(UserValidators):
email: EmailStr


class UserPatch(UserValidators, BasePatch):
password: Optional[str] = Field(default=None) # type: ignore
class UserPatch(BasePatch):
password: Optional[str] = Field(
default=None,
min_length=PASSWORD_MIN_LENGTH,
max_length=PASSWORD_MAX_LENGTH
)

0 comments on commit cbc9fca

Please sign in to comment.