From d930830b7b22f12a3aae763e2d4c734c4dbd306b Mon Sep 17 00:00:00 2001 From: Deimos Date: Mon, 4 Feb 2019 15:28:59 -0700 Subject: [PATCH] Strip whitespace around username before validating This is mostly for when people are logging in. Mobile keyboards especially like to add a space after the username, which previously would cause an error unless they manually deleted the trailing space. --- tildes/tildes/schemas/user.py | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/tildes/tildes/schemas/user.py b/tildes/tildes/schemas/user.py index afd9733..6747ebc 100644 --- a/tildes/tildes/schemas/user.py +++ b/tildes/tildes/schemas/user.py @@ -92,6 +92,17 @@ class UserSchema(Schema): if is_breached_password(value): raise ValidationError("That password exists in a data breach (see sidebar)") + @pre_load + def prepare_username(self, data: dict) -> dict: + """Prepare the username value before it's validated.""" + if "username" not in data: + return data + + # strip any leading/trailing whitespace + data["username"] = data["username"].strip() + + return data + @pre_load def prepare_email_address(self, data: dict) -> dict: """Prepare the email address value before it's validated."""