FastAPI request validation with i18n error message
Reason this release was yanked:
testing
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>
- add middlewares 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.1.61.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | ced95a0da9fe7164d1cef757a86e190151c6ee08de5340c41dc456d7192d7681 |
|
MD5 | b010a74f50cc1663038c685a5530fda3 |
|
BLAKE2b-256 | f48abf0d167aafe702f1f958f1707e3e75f94e1b5a83eb5500545eb2963b492c |
Close
Hashes for fastapi_validation_i18n-0.1.61-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5269149b0f395393d97d71af42e79bfaa22c772a412721b2e70664692c226166 |
|
MD5 | eaaa0104d1aa095e53b76d8966f8dfb8 |
|
BLAKE2b-256 | bf772bd27dbb355a5a524207d4663db2fc25466a7561e5a0cc96a850d6e7dd90 |