From f5f05f9f8e68ba038e5f18b40bd19135cc1118c3 Mon Sep 17 00:00:00 2001 From: darknight Date: Thu, 16 May 2024 23:44:06 +0800 Subject: [PATCH] update regex for name validation --- ee/tabby-schema/src/schema/auth.rs | 2 +- ee/tabby-schema/src/schema/constants.rs | 38 ++++++++++++++++++++++++- 2 files changed, 38 insertions(+), 2 deletions(-) diff --git a/ee/tabby-schema/src/schema/auth.rs b/ee/tabby-schema/src/schema/auth.rs index 13a32f47dd05..6abc06e2adff 100644 --- a/ee/tabby-schema/src/schema/auth.rs +++ b/ee/tabby-schema/src/schema/auth.rs @@ -290,7 +290,7 @@ pub struct UpdateUserNameInput { #[validate(regex( code = "name", path = "crate::schema::constants::USERNAME_REGEX", - message = "Invalid name, only alphabet and space character are allowed, and it must start and end with alphabet" + message = "Invalid name, name may contain numbers or special characters which are not supported" ))] pub name: String, } diff --git a/ee/tabby-schema/src/schema/constants.rs b/ee/tabby-schema/src/schema/constants.rs index 84619760e1f7..497b8a743b2a 100644 --- a/ee/tabby-schema/src/schema/constants.rs +++ b/ee/tabby-schema/src/schema/constants.rs @@ -3,5 +3,41 @@ use regex::Regex; lazy_static! { pub static ref REPOSITORY_NAME_REGEX: Regex = Regex::new("^[a-zA-Z][\\w.-]+$").unwrap(); - pub static ref USERNAME_REGEX: Regex = Regex::new("^[A-Za-z][A-Za-z ]*[A-Za-z]$").unwrap(); + pub static ref USERNAME_REGEX: Regex = Regex::new( + r"^[^0-9±!@£$%^&*_+§¡€#¢¶•ªº«\\/<>?:;|=.,]{2,20}$").unwrap(); +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_username_regex() { + let test_cases = vec![ + ("John", true), // English name + ("Müller", true), // German name + ("Jørgensen", true), // Danish name + ("李雷", true), // Chinese name + ("あきは", true), // Japanese name + ("김민수", true), // Korean name + ("Алексей", true), // Russian name + ("José", true), // Spanish names + ("علی", true), // Iranian names + // Edge cases + ("", false), // Empty string + ("JohnDoeIsAReallyLongName", false), // More than 20 characters + ("John!", false), // Invalid character '!' + ("José@", false), // Invalid character '@' + ("12345", false), // Invalid character Numbers + ("John_Doe", false), // Underscore character + ("Anna-Marie", true), // Hyphen character + ("O'Connor", true), // Apostrophe + ("李@伟", false) + ]; + + for (name, expected) in test_cases { + let result = USERNAME_REGEX.is_match(name); + assert_eq!(result, expected, "Failed for name: {}", name); + } + } }