我对使用Pydantic相当陌生。
我现在面临的问题是,下面的模型在值超出范围时没有引发预期的异常。
例如,如果你将-1传递给这个模型,它应该理想地引发一个HTTPException,但什么也没发生。
我不确定我可能错在哪里。
任何建议都将是有益的。
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
FastAPI端点
@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
BaseModel
... - juanpa.arrivillagapydantic.errors.ConfigError: Validators defined with incorrect fields: check_if_interval_in_range, check_if_rank_in_range (use check_fields=False if you're inheriting from the model and intended this)
。 - Rajath Rao - Software Engineer