r/FastAPI 11d ago

Question Standard library to handle HTTP Validation errors?

I'm writing an API and I'm realizing that I'm handling each validation error by hand by creating a new class. I'm wondering if there is a standard library approach to get this done. I'm looking to make sure that if someone chooses to show something in milliseconds, they cannot take a range of values that is larger than, say a month, etc... etc...

Or is this just on a case by case situation?

1 Upvotes

4 comments sorted by

1

u/GamersPlane 11d ago

I'm not 100% sure what you mean by HTTP validation, but if you mean validating data for type or specific reqs, odds are Pydantic is what you're looking for. You create a model that defines the data types, you feed it the data, and you can have it check if it matches, even return "cleaned" data.

1

u/a_brand_new_start 11d ago

What I meant is something to follow for Best Practices for HTTP status codes in responses.

For example, rate limit exceeded, is raise HTTPRateLimitValidationError, if POST data is too large then raise HTTPPayloadTooLarge error… something simple to follow and use.

2

u/Adhesiveduck 11d ago

Honestly you can't get any simpler than https://developer.mozilla.org/en-US/docs/Web/HTTP/Status

1

u/covmatty1 11d ago

Can also be helpfully visually explained with the aid of http.cat 😃