Integer enum query parameters validation always fails
See original GitHub issueFirst check
- I added a very descriptive title to this issue.
- I used the GitHub search to find a similar issue and didn’t find it.
- I searched the FastAPI documentation, with the integrated search.
- I already searched in Google “How to X in FastAPI” and didn’t find any information.
- I already read and followed all the tutorial in the docs and didn’t find an answer.
- I already checked if it is not related to FastAPI but to Pydantic.
- I already checked if it is not related to FastAPI but to Swagger UI.
- I already checked if it is not related to FastAPI but to ReDoc.
- After submitting this, I commit to one of:
- Read open issues with questions until I find 2 issues where I can help someone and add a comment to help there.
- I already hit the “watch” button in this repository to receive notifications and I commit to help at least 2 people that ask questions in the future.
- Implement a Pull Request for a confirmed bug.
Example
Here’s a self-contained, minimal, reproducible, example with my use case:
from enum import Enum
from fastapi import FastAPI
app = FastAPI()
class Category(int, Enum):
MISC = 0
LIFE = 1
CODE = 2
GUIDES = 3
PROJECTS = 4
@app.get("/stuff")
def read_root(category: Category):
return {"Hello": "World"}
Run $uvicorn test:app
Description
- Open the browser and call the endpoint
/stuff?category=1
. - It returns a 422 error
"value is not a valid enumeration member; permitted: 0, 1, 2, 3, 4"
even though the correct int enum value is provided. - Expected: It returns a JSON with
{"Hello": "World"}
.
Environment
- OS: macOS
- FastAPI Version: 0.61.1
- Python version: 3.8.5
Additional context
I’ve looked through all pydantic issues, found this one, but I don’t know if it’s related, or what it’s current status (whether it has been resolved).
Issue Analytics
- State:
- Created 3 years ago
- Reactions:5
- Comments:14 (3 by maintainers)
Top Results From Across the Web
Why doesn't FastAPI handle types derived from int and Enum ...
Seems like validators are just hardcoded for IntEnum to be an integer validator + enum validator, and the integer validator converts from string ......
Read more >Validation for integer enums fails with good values. - Bitbucket
Using 1.4.2 with a definition such as "type": "integer", "enum": [ 5, 10, 30 ] Passing any integer value such as 10 fails...
Read more >Validations for Enum Types - Baeldung
Learn how to build validations for enums using Java custom annotations.
Read more >Validating numeric query parameters in NestJS
What? But country is a numeric field! It's even an integer! Unfortunately, not for our application. Let's try to make one step back ......
Read more >Common Problems Found in RAML 1.0 API Specifications
This error occurs when a facet or data type is defined as having a specific set of properties, yet an API specification defines...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
With
IntEnum
type validation works as expectedSure, using the IntEnum example as above, all you see is this:
But as a consumer of the API, I have no idea what those numbers mean. I need to see their human-readable names in order to understand my options. If the names were listed in the schema, I would know that: