JoeCode

Sinatra password complexity helper module

May 30, 2023

module code (password-utility.rb)

module PasswordUtility
  module_function
  def complex_password?(password)
    complexity = /\A 
      (?=.{8,})           # 8 chars
      (?=.*\d)            # >= 1 number
      (?=.*[a-z])         # >= 1 lowercase letter
      (?=.*[A-Z])         # >= 1 uppercase letter
      (?=.*[[:^alnum:]])  # >= 1 symbol
    /x
    return password.match(complexity)
  end
end

usage (app.rb)

require_relative 'password-utility'
helpers PasswordUtility

my_password = 'P@ssw0rd!'
puts 'good password' if complex_password?(my_password)

Resources and inspiration