-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
29a1b89
commit f1da544
Showing
5 changed files
with
80 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,15 @@ | ||
from pydantic import BaseModel, EmailStr | ||
from pydantic import BaseModel, EmailStr, Field | ||
from typing import Optional | ||
|
||
# Pydantic model for creating a new user | ||
class UserCreateSchema(BaseModel): | ||
name: str | ||
email: EmailStr | ||
password: str | ||
password: str = Field(..., min_length=8) | ||
|
||
|
||
# Pydantic model for updating user data | ||
class UserUpdateSchema(BaseModel): | ||
name: Optional[str] = None | ||
email: Optional[EmailStr] = None | ||
password: Optional[str] = None | ||
password: Optional[str] = Field(None, min_length=8) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
from fastapi import Request, HTTPException | ||
from fastapi.responses import JSONResponse | ||
from fastapi import FastAPI | ||
from pydantic import BaseModel, ValidationError | ||
from functools import wraps | ||
from fastapi_project.utils.base import the_query | ||
|
||
# Add the ValidationException class | ||
class ValidationException(Exception): | ||
def __init__(self, errors: dict): | ||
self.errors = errors | ||
|
||
# Add the setup_validation_exception_handler function | ||
def setup_validation_exception_handler(app: FastAPI): | ||
@app.exception_handler(ValidationException) | ||
async def validation_exception_handler(request: Request, exc: ValidationException): | ||
return JSONResponse( | ||
status_code=422, | ||
content=exc.errors | ||
) | ||
|
||
# Add the dto decorator | ||
def dto(schema: BaseModel): | ||
def decorator(func): | ||
@wraps(func) | ||
async def wrapper(request: Request, *args, **kwargs): | ||
try: | ||
request_data = await the_query(request) | ||
validated_data = schema(**request_data) | ||
return await func(validated_data, *args, **kwargs) | ||
except ValidationError as e: | ||
errors = {} | ||
for error in e.errors(): | ||
field = error['loc'][0] | ||
message = field + " " + error['msg'] | ||
if field not in errors: | ||
errors[field] = [] | ||
errors[field].append(message) | ||
|
||
raise ValidationException({ | ||
'success': False, | ||
'errors': errors | ||
}) | ||
except ValueError: | ||
raise HTTPException(status_code=400, detail="Invalid JSON") | ||
|
||
return wrapper | ||
return decorator | ||
|
||
# Update the __all__ list | ||
__all__ = ['dto', 'ValidationException', 'setup_validation_exception_handler'] |