Description
Suppose that a given route returns tabular data, and the user can specify the order of the retrieval. I wish to declare that order as a GET parameter of the following form:
sort=+amountto sort by columnamountascending;sort=-amountto sort by columnamountdescending. I would like this to be described in the swagger (as free text and with an example).
Currently I have:
class Sort(str):
@classmethod
def __get_validators__(cls):
yield cls.validate
@classmethod
def validate(cls, v):
if not isinstance(v, str):
raise ValueError(f'Sort must be a string.')
if len(v) == 0:
raise ValueError(f'Sort must be non-empty.')
if v[0] not in {'+', '-'}:
raise ValueError(f'Sort\'s first character must be either "+" or "-"')
return v
def data(sort: Sort = None, search: str = None, page: int = 1):
if sort is not None:
order, column = sort[0], sort[1:]
if column not in ['amount', 'foo']:
raise ...[what?]
...
Hey @jorgecarleitao, I think you just need to read this docs section carefully :) https://fastapi.tiangolo.com/tutorial/query-params-str-validations/
@jorgecarleitao it seems you just need an int that can be positive or negative, right?
As @shifr points out, check those docs, and these: https://fastapi.tiangolo.com/tutorial/path-params-numeric-validations/
from fastapi import FastAPI
app = FastAPI()
@app.get("/")
def data(sort: int = None, search: str = None, page: int = 1):
return {"some": "data"}