Metadata-Version: 2.1
Name: fastapi_validation_i18n
Version: 0.3.1
Summary: FastAPI request validation with i18n error message
Project-URL: Repository, https://github.com/whchi/fastapi-validation-i18n
Author-email: whchi <whccchi@gmail.com>
License-Expression: MIT
License-File: LICENSE
Keywords: fastapi,i18n,pydantic,validation
Requires-Python: >=3.8
Requires-Dist: fastapi<0.100.0
Requires-Dist: pydantic<2.0,>=1.10
Description-Content-Type: text/markdown

error message with i18n support in FastAPI

```json
{
  "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
1. publish locales to your app path 
```bash
poetry run publish-locale <your-path>
```
2. add middlewares and exception handler to your FastAPI app
```py
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
)
```
3. start use it 
 
there's 3 way to set locale
 
1. set `accept-language` header to your request
2. set an API with `locale` in path
3. set `locale` query parameter to your request

you can see the [example](example) for more detail 


## todo
- [ ] support pydantic v2
- [ ] other i18n support
- [ ] support FastAPI>0.99