Skip to content

Commit

Permalink
Merge pull request #90 from spiralover/refactor/app
Browse files Browse the repository at this point in the history
feat(regex): add "Alphabetic" regex
  • Loading branch information
Ahmard authored Nov 18, 2024
2 parents 2db2543 + eed6df0 commit 46909e6
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 3 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "medullah-web"
version = "0.22.0"
version = "0.22.1"
edition = "2021"
license = "MIT"
description = "Micro-Framework Based on Ntex"
Expand Down
31 changes: 30 additions & 1 deletion src/helpers/regex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ pub struct Regex;
/// Enum representing different types of regex patterns for username validation.
pub enum RegexType {
/// Allows only lowercase letters (1 to 38 characters).
Alphabetic,

/// Allows only lowercase letters and numbers (1 to 38 characters), also must start with char.
AlphaNumeric,

/// Allows lowercase letters, digits, and hyphens (`-`), but no consecutive or trailing hyphens.
Expand Down Expand Up @@ -97,7 +100,8 @@ impl Regex {
/// A string slice (`&'static str`) containing the regex pattern associated with the `RegexType` variant.
fn acquire_regex(rt: RegexType) -> &'static str {
match rt {
RegexType::AlphaNumeric => r"^[a-z]{1,38}$", // Only lowercase letters, 1-38 characters.
RegexType::Alphabetic => r"^[a-z]{1,38}$", // Only lowercase letters, 1-38 characters.
RegexType::AlphaNumeric => r"^[a-z][a-z0-9]{0,37}$", // Only lowercase letters, 1-38 characters.
RegexType::AlphaNumericDash => r"^[a-z](?!.*\-\-)(?!.*\-$)[a-z\d\-]{0,37}$", // Letters, digits, and dashes, no consecutive or trailing dashes.
RegexType::AlphaNumericDot => r"^[a-z](?!.*\.\.)(?!.*\.$)[a-z\d\.]{0,37}$", // Letters, digits, and dots, no consecutive or trailing dots.
RegexType::AlphaNumericUnderscore => r"^[a-z](?!.*\_\_)(?!.*\_$)[a-z\d\_]{0,37}$", // Letters, digits, and underscores, no consecutive or trailing underscores.
Expand All @@ -114,11 +118,36 @@ impl Regex {
mod tests {
use super::*;

// Test for Alphabetic regex type
#[test]
fn test_alphabetic_valid() {
let result = Regex::validate("username", RegexType::Alphabetic);
assert!(result.is_ok() && result.unwrap());
}

#[test]
fn test_alphabetic_invalid() {
let result = Regex::validate("user1name", RegexType::Alphabetic);
assert!(result.is_ok() && !result.unwrap());

let result = Regex::validate("1username", RegexType::Alphabetic);
assert!(result.is_ok() && !result.unwrap());

let result = Regex::validate("username1", RegexType::Alphabetic);
assert!(result.is_ok() && !result.unwrap());
}

// Test for AlphaNumeric regex type
#[test]
fn test_alpha_numeric_valid() {
let result = Regex::validate("username", RegexType::AlphaNumeric);
assert!(result.is_ok() && result.unwrap());

let result = Regex::validate("user1name", RegexType::AlphaNumeric);
assert!(result.is_ok() && result.unwrap());

let result = Regex::validate("user1name2", RegexType::AlphaNumeric);
assert!(result.is_ok() && result.unwrap());
}

#[test]
Expand Down

0 comments on commit 46909e6

Please sign in to comment.