I need to parse version parameter from URL path in FastAPI endpoint. The valid value for version parameter is either a fixed string e.g. active or any integer e.g. 1.
Is there an elegant way to make FastAPI or pydantic validate this path parameter with such rules so that I don't have to perform validation manually?
@app.get("/{version}")
def endpoint(version):
# version could be either 'active' or valid integer e.g. 1
return {"version": version}
from typing import Union, Literal,version: Union[Literal['active'], int]could work?