No project description provided
Reason this release was yanked:
no github statics
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.3.0.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5cc1ebdfe734983f090b72038415f1077e9767bb3abc82c02c10dcd43ee2b78d |
|
MD5 | ab99cbf372898aa5aeea6c4c028abd47 |
|
BLAKE2b-256 | 8e10288a5eda01920f43bc4d7f48da79392e7621886c5a4b8306a7a27f9516db |
Close
Hashes for fastapi_validation_i18n-0.3.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9f332e8490978c8c433c1e093d3d5f38641f5dc3230ccb8905e351aa9d6743e3 |
|
MD5 | c5b0682a112df276140f505c102e2e05 |
|
BLAKE2b-256 | 1b65fa1c63b8fd84558797d8d44dbfe248053f977d625763ca78f792eee0ca4f |