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.5.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 61e45c118072b8802ddaf0d5c1689de7a0781215582d378e15930591bf125162 |
|
MD5 | 640f185b9146a14b3f18942649715f50 |
|
BLAKE2b-256 | c36fc263217a4918bf4debcb5c8f84803135f79c3cc2c7ba37d50677b0af10e6 |
Close
Hashes for fastapi_validation_i18n-0.1.5-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | c6f50852d323710a32d81d45cf7956a0e247b620fdc9c3b8717ded61946d2b2a |
|
MD5 | d4015cc4b4e55f91de55697136fe876e |
|
BLAKE2b-256 | 4797d90084e00ee10071bedb89b2d0043318d518b2f75ba65528958ea41d2879 |