24 lines
812 B
Python
24 lines
812 B
Python
from __future__ import annotations
|
|
|
|
from fastapi import HTTPException
|
|
|
|
|
|
def parse_positive_int_ids(value, field_name: str = "ids") -> list[int]:
|
|
detail = f"{field_name} must be a list of positive integers"
|
|
if not isinstance(value, list):
|
|
raise HTTPException(status_code=400, detail=detail)
|
|
ids: list[int] = []
|
|
for item in value:
|
|
if isinstance(item, bool):
|
|
raise HTTPException(status_code=400, detail=detail)
|
|
if isinstance(item, int):
|
|
item_id = item
|
|
elif isinstance(item, str) and item.isdecimal():
|
|
item_id = int(item)
|
|
else:
|
|
raise HTTPException(status_code=400, detail=detail)
|
|
if item_id <= 0:
|
|
raise HTTPException(status_code=400, detail=detail)
|
|
ids.append(item_id)
|
|
return ids
|