Module: Securial::Helpers::RegexHelper

Extended by:
RegexHelper
Included in:
RegexHelper
Defined in:
lib/securial/helpers/regex_helper.rb

Overview

Regular expression patterns and validation methods for common data types.

This module centralizes regex patterns used throughout Securial for validating user input. It provides both the raw regex patterns as constants and convenience methods for validation.

Constant Summary collapse

EMAIL_REGEX =

RFC-compliant email address validation pattern.

Uses Ruby’s built-in URI::MailTo::EMAIL_REGEXP which follows RFC 3696 specifications for email address validation.

Returns:

  • (Regexp)

    Email validation regular expression

URI::MailTo::EMAIL_REGEXP
USERNAME_REGEX =

Username validation pattern with specific formatting rules.

Enforces the following username requirements:

  • Must start with a letter (not a number)

  • Can contain letters, numbers, periods, and underscores

  • Cannot have consecutive periods or underscores

  • Must end with a letter or number

  • Minimum length of 2 characters

Returns:

  • (Regexp)

    Username validation regular expression

/\A(?![0-9])[a-zA-Z](?:[a-zA-Z0-9]|[._](?![._]))*[a-zA-Z0-9]\z/
PASSWORD_REGEX =

Password complexity validation pattern.

Enforces strong password requirements including:

  • At least one digit (0-9)

  • At least one lowercase letter (a-z)

  • At least one uppercase letter (A-Z)

  • At least one special character (non-alphanumeric)

  • Must start with a letter

Returns:

  • (Regexp)

    Password complexity validation regular expression

%r{\A(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[^a-zA-Z0-9])[a-zA-Z].*\z}

Instance Method Summary collapse

Instance Method Details

#valid_email?(email) ⇒ Boolean

Validates if an email address matches the RFC-compliant pattern.

Uses the EMAIL_REGEX pattern to check if the provided email address follows proper email formatting standards.

Examples:

Valid email addresses

valid_email?("[email protected]")        # => true
valid_email?("[email protected]") # => true
valid_email?("[email protected]")    # => true

Invalid email addresses

valid_email?("invalid.email")           # => false
valid_email?("@example.com")            # => false
valid_email?("user@")                   # => false

Parameters:

  • email (String)

    The email address to validate

Returns:

  • (Boolean)

    true if the email is valid, false otherwise



89
90
91
# File 'lib/securial/helpers/regex_helper.rb', line 89

def valid_email?(email)
  email.match?(EMAIL_REGEX)
end

#valid_password?(password) ⇒ Boolean

Validates if a password meets complexity requirements.

Uses the PASSWORD_REGEX pattern to check if the provided password meets the application’s security standards.

Examples:

Valid passwords

valid_password?("MyPass123!")    # => true
valid_password?("Secure@2024")   # => true
valid_password?("aB3#defgh")     # => true

Invalid passwords

valid_password?("password")      # => false (no uppercase, number, or special char)
valid_password?("PASSWORD123")   # => false (no lowercase or special char)
valid_password?("123Password!")  # => false (starts with number)

Parameters:

  • password (String)

    The password to validate

Returns:

  • (Boolean)

    true if the password meets complexity requirements, false otherwise



135
136
137
# File 'lib/securial/helpers/regex_helper.rb', line 135

def valid_password?(password)
  password.match?(PASSWORD_REGEX)
end

#valid_username?(username) ⇒ Boolean

Validates if a username follows the defined formatting rules.

Uses the USERNAME_REGEX pattern to check if the provided username meets the application’s username requirements.

Examples:

Valid usernames

valid_username?("john_doe")      # => true
valid_username?("user123")       # => true
valid_username?("test.user")     # => true
valid_username?("a1")            # => true

Invalid usernames

valid_username?("123user")       # => false (starts with number)
valid_username?("user__name")    # => false (consecutive underscores)
valid_username?("user.")         # => false (ends with period)
valid_username?("a")             # => false (too short)

Parameters:

  • username (String)

    The username to validate

Returns:

  • (Boolean)

    true if the username is valid, false otherwise



113
114
115
# File 'lib/securial/helpers/regex_helper.rb', line 113

def valid_username?(username)
  username.match?(USERNAME_REGEX)
end