Page 1 of 1

Passwortvalidierung mit Pydantic

Posted: 05 Jan 2025, 10:51
by Guest
Um die Verwendung einer if-else-Schleife zu vermeiden, habe ich Folgendes getan, um die Passwortvalidierung in Pydantic hinzuzufügen.

Code: Select all

    @field_validator("password")
def check_password(cls, value):
# Convert the password to a string if it is not already
value = str(value)

# Check that the password meets the criteria
if len(value) < 8:
raise ValueError("Password must have at least 8 characters")

if not any(c.isupper() and c.islower() and c.isdigit() and c in string.punctuation for c in value):
raise ValueError("Password must have at least one uppercase letter, one lowercase letter, and one digit")

return value
Aber leider funktioniert diese Bedingung nicht richtig, wenn nicht. Wie kann ich das Problem beheben?