Skip to content

Commit

Permalink
update regex for name validation
Browse files Browse the repository at this point in the history
  • Loading branch information
darknight committed May 16, 2024
1 parent b5589f2 commit f5f05f9
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 2 deletions.
2 changes: 1 addition & 1 deletion ee/tabby-schema/src/schema/auth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
Expand Down
38 changes: 37 additions & 1 deletion ee/tabby-schema/src/schema/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}

0 comments on commit f5f05f9

Please sign in to comment.