FastAPI request validation with i18n error message
Reason this release was yanked:
no logner support
Project description
error message with i18n support in FastAPI
{
"errors": [
{
"loc": [
"body",
"string"
],
"msg": "確保此值最多有 10 個字符",
"type": "value_error.any_str.max_length",
"ctx": {
"limit_value": 10
}
},
{
"loc": [
"body",
"nested",
"body"
],
"msg": "確保此值最多有 2 個字符",
"type": "value_error.any_str.max_length",
"ctx": {
"limit_value": 2
}
},
{
"loc": [
"body",
"nested",
"inner",
"inner_body"
],
"msg": "確保此值最多包含 2 個項目",
"type": "value_error.list.max_items",
"ctx": {
"limit_value": 2
}
},
{
"loc": [
"body",
"nested",
"inner",
"cat"
],
"msg": "鑑別器 'color' 和值 'ccc' 不匹配(允許的值: 'black', 'white')",
"type": "value_error.discriminated_union.invalid_discriminator",
"ctx": {
"discriminator_key": "color",
"discriminator_value": "ccc",
"allowed_values": "'black', 'white'"
}
}
]
}
Attention
- support pydantic v1 only now(2023/08/16)
- built-in locales are zh-TW, en-US, ja-JP, you can change the locales by yourself
How to run
- publish locales to your app path
poetry run publish-locale [your-path] # default to "locale" in your project path
- add middleware and exception handler to your FastAPI app
from fastapi import FastAPI
from fastapi_validation_i18n import I18nMiddleware, i18n_exception_handler
from fastapi.exceptions import RequestValidationError
app = FastAPI()
app.add_middleware(I18nMiddleware, locale_path='your-publish-path')
app.add_exception_handler(
RequestValidationError,
i18n_exception_handler
)
- start use it
there's 3 way to set locale
- set
accept-language
header to your request - set an API with
locale
in path - set
locale
query parameter to your request
you can see the example for more detail
todo
- support pydantic v2
- other i18n support
- support FastAPI>0.99
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Close
Hashes for fastapi_validation_i18n-0.3.3.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1a1a1efd0dc2f2718bc7831a5b07735944b038e5acdda63094a838e33e828638 |
|
MD5 | 6022e8f678a3bdfd75ae678e1d0ccc08 |
|
BLAKE2b-256 | 79d6a3e90c1b9d1fedfe0dfc90e5bf866aff570a11019742d599f66ce99a098c |
Close
Hashes for fastapi_validation_i18n-0.3.3-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 08edc5b4eb31e35e85fdca2d9d718e49a859e7990ba903ab41f06363a5d81211 |
|
MD5 | 29b73db439fe51a45830bb85c0ed98f5 |
|
BLAKE2b-256 | ad1d61dd6160c2c89a3f7a1b98d9b8f2c8b18f7b21e645a8a97c741275031e2b |