mirror of
https://github.com/fastapi-users/fastapi-users.git
synced 2026-03-13 07:49:55 +08:00
Fix #396: add password validation example
This commit is contained in:
@@ -40,6 +40,23 @@ class UserDB(User, models.BaseUserDB):
|
||||
|
||||
You can of course add you own properties there to fit to your needs!
|
||||
|
||||
## Password validation
|
||||
|
||||
**FastAPI Users** doesn't provide a default password validation, but you can implement it easily with a [Pydantic validator](https://pydantic-docs.helpmanual.io/usage/validators/) on the `UserCreate` class. Here is a simple example to check if the password is at least six characters long:
|
||||
|
||||
```py
|
||||
from fastapi_users import models
|
||||
from pydantic import validator
|
||||
|
||||
|
||||
class UserCreate(models.BaseUserCreate):
|
||||
@validator('password')
|
||||
def valid_password(cls, v: str):
|
||||
if len(v) < 6:
|
||||
raise ValueError('Password should be at least 6 characters')
|
||||
return v
|
||||
```
|
||||
|
||||
## Next steps
|
||||
|
||||
Depending on your database backend, database configuration will differ a bit.
|
||||
|
||||
Reference in New Issue
Block a user