Develop/FastAPI

FastAPI Data Validation(추가) - 16

김잠봉 2022. 12. 22. 10:27
728x90

1. 새로운 Field를 추가

class Book(BaseModel):
    id: UUID
    title: str = Field(min_length=1)
    author: str = Field(min_length=1, max_length=100) #최소길이는 1, 최대길이는 100
    description: Optional[str] = Field(title="Description of the book",
                             max_length=100,
                             min_length=1)
    rating: int = Field(gt=-1, lt=101) #-1보다는 크고 101보다는 작다 (0~100사이)

 

2. Swagger 확인

  • Schemas에 추가 된 것을 볼 수 있다.

 

3. rating의 Field의 유효성 검사

  • rating에 101과 102를 넣으면 유효성 검사 에러가 나온다.
 
 
  • 101보다 미만인 값을 넣었기 때문에 100부터 값을 넣을 수 있다.
 
 
  • 100에서는 Code가 200이 나오는것을 볼 수 있다.
728x90
SMALL