How to mark pydantic model filed as secret so it will not shown in the repr str and will be excluded from dict and etc...
from pydantic import BaseModel
class User(BaseModel):
name: str
password_hash: str # I do not want this field to leak out.
I write my code with security in mind and I afraid that in the future someone else will write non secure code that will leak the 'password_hash' field outside to the logs etc...
Is there a way to mark the field as secret to be sure it not leak out?
CodePudding user response:
Pydantic provides convenience Secret*
classes for this exact purpose:
from pydantic import BaseModel, SecretStr
class User(BaseModel):
name: str
password_hash: SecretStr