I am quite new to using Pydantic.
The Issue I am facing right now is that the Model Below is not raising the Expected Exception when the value is out of range.
For example, if you pass -1 into this model it should ideally raise an HTTPException. but nothing happens
I am not sure where I might be going wrong.
Any Advice would be great.
class GetInput:
"""
for the fields endpoint
"""
def __init__(self,
rank: Optional[int] = None,
interval: Optional[int] = None):
self.rank = rank
self.interval = interval
@validator('rank')
def check_if_rank_in_range(cls, v):
"""
check if input rank is within range
"""
if not 0 < v < 1000001:
raise HTTPException(
status_code=400, detail="Rank Value Must be within range (0,1000000)")
return v
@validator('interval')
def check_if_interval_in_range(cls, v):
"""
check if input rank is within range
"""
if not 0 < v < 1000001:
raise HTTPException(
status_code=400, detail="Interval Value Must be within range (0,1000000)")
return v
The FastAPI Endpoint
@router.get('fields/',status_code=200)
def get_data(params: GetInput = Depends()):
if params.rank:
result = get_info_by_rank(params.rank)
elif params.interval:
result = get_info_by_interval(params.interval)
return result
class GetInput(BaseModel):
rank: Optional[int]=None
interval: Optional[int]=None
@validator("*")
def check_range(cls, v):
if v:
if not 0 < v < 1000001:
raise HTTPException(status_code=400, detail="Value Must be within range (0,1000000)")
return v