Skip to content

Commit

Permalink
Update FastAPI and Pydantic.
Browse files Browse the repository at this point in the history
  • Loading branch information
vivekashok1221 committed Jul 13, 2023
1 parent eb46f88 commit 209776e
Show file tree
Hide file tree
Showing 3 changed files with 236 additions and 113 deletions.
40 changes: 17 additions & 23 deletions app/schemas.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import re
from datetime import datetime

from pydantic import BaseModel, EmailStr, validator
from pydantic import BaseModel, ConfigDict, EmailStr, field_validator

USERNAME_RE = re.compile("^[A-Za-z0-9_-]*$")

Expand All @@ -13,7 +13,8 @@ class UserCreate(BaseModel):
password: str
email: EmailStr

@validator("username")
@classmethod
@field_validator("username")
def _validate_username(cls, username: str) -> str:
"""Checks if username only contains alphanumeric characters, - and _."""
if not USERNAME_RE.match(username):
Expand All @@ -22,7 +23,8 @@ def _validate_username(cls, username: str) -> str:
)
return username

@validator("password")
@classmethod
@field_validator("password")
def _validate_password(cls, password: str) -> str:
"""Checks if password is longer than 6 characters."""
if len(password) < 6:
Expand All @@ -34,23 +36,19 @@ class SignupResponse(BaseModel):
"""Response model for user signup."""

message: str

class Config:
"""Extra schema information for the model."""

schema_extra = {"example": {"message": "User registered successfully"}}
model_config = ConfigDict(
json_schema_extra={"example": {"message": "User registered successfully"}}
)


class TokenResponse(BaseModel):
"""Response model for token generation."""

access_token: str
token_type: str

class Config:
"""Extra schema information for the model."""

schema_extra = {"example": {"access_token": "<token>", "token_type": "bearer"}}
model_config = ConfigDict(
json_schema_extra={"example": {"access_token": "<token>", "token_type": "bearer"}}
)


class LinkCreate(BaseModel):
Expand All @@ -70,11 +68,9 @@ class LinkDeleteResponse(BaseModel):
"""Response model for link deletion."""

message: str

class Config:
"""Extra schema information for the model."""

schema_extra = {"example": {"message": "Link deleted successfully"}}
model_config = ConfigDict(
json_schema_extra={"example": {"message": "Link deleted successfully"}}
)


class PingResponse(BaseModel):
Expand All @@ -83,14 +79,12 @@ class PingResponse(BaseModel):
message: str
timestamp: datetime
version: str

class Config:
"""Extra schema information for the model."""

schema_extra = {
model_config = ConfigDict(
json_schema_extra={
"example": {
"message": "Pong!",
"timestamp": "2023-07-12T23:14:46.380726",
"version": "1.0.0",
}
}
)
Loading

0 comments on commit 209776e

Please sign in to comment.