This commit is contained in:
@@ -1,6 +1,8 @@
|
||||
from typing import Self
|
||||
|
||||
from fastapi import FastAPI, HTTPException
|
||||
from fastapi.middleware.cors import CORSMiddleware
|
||||
from pydantic import BaseModel
|
||||
from pydantic import BaseModel, Field, field_validator, model_validator
|
||||
from sqlalchemy import select
|
||||
from sqlalchemy.exc import SQLAlchemyError
|
||||
|
||||
@@ -18,13 +20,43 @@ app.add_middleware(
|
||||
|
||||
|
||||
class todocreate(BaseModel):
|
||||
task: str
|
||||
task: str = Field(min_length=1, max_length=200)
|
||||
|
||||
@field_validator("task")
|
||||
@classmethod
|
||||
def task_must_not_be_blank(cls, value: str) -> str:
|
||||
if not value.strip():
|
||||
raise ValueError("task must not be blank")
|
||||
|
||||
return value
|
||||
|
||||
class todoupdate(BaseModel):
|
||||
completion: bool | None = None
|
||||
task: str | None = None
|
||||
task: str | None = Field(
|
||||
default=None,
|
||||
min_length=1,
|
||||
max_length=200,
|
||||
)
|
||||
|
||||
@field_validator("task")
|
||||
@classmethod
|
||||
def task_must_not_be_blank(
|
||||
cls,
|
||||
value: str | None,
|
||||
) -> str | None:
|
||||
if value is not None and not value.strip():
|
||||
raise ValueError("task must not be blank")
|
||||
|
||||
return value
|
||||
|
||||
@model_validator(mode="after")
|
||||
def at_least_one_field_must_be_provided(self) -> Self:
|
||||
if self.task is None and self.completion is None:
|
||||
raise ValueError(
|
||||
"task or completion must be provided"
|
||||
)
|
||||
|
||||
return self
|
||||
|
||||
class todoresponse(BaseModel):
|
||||
id: int
|
||||
|
||||
Reference in New Issue
Block a user