From 42340d992bace7490832b38d89345500f04e6b60 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Voron?= Date: Mon, 7 Dec 2020 07:57:22 +0100 Subject: [PATCH] Fix #396: add password validation example --- docs/configuration/model.md | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/docs/configuration/model.md b/docs/configuration/model.md index e5213bd2..9c6a9b03 100644 --- a/docs/configuration/model.md +++ b/docs/configuration/model.md @@ -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.