custom validator that validates username is not included in the list of reserved names for ActiveModel
Ruby
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
lib
spec
.coveralls.yml
.gitignore
.travis.yml
Gemfile
LICENSE.txt
README.md
Rakefile
username_not_reserved_validator.gemspec

README.md

UsernameNotReservedValidator

Build Status Coverage Status Code Climate

Custom validator for ActiveModel.

Validates that username is not included in the list of reserved names.

e.g:

  • index
  • home
  • top
  • ...

Installation

Add this line to your application's Gemfile:

gem 'username_not_reserved_validator'

And then execute:

$ bundle

Or install it yourself as:

$ gem install username_not_reserved_validator

Usage

validation settings

Add validation setting on your ActiveModel.

class User < ActiveRecord::Base
  validates(:name, username_not_reserved: true)
end

options

  • additional_reserved_names (Array of String / default: [])
    • specify additional reserved names
  • case_insensitive (Boolean / default: true)
    • if set to false, comparison is case sencitive
    • also key case_insencitive will be accepted due to historical typo
  • message (Symbol / default: :invalid)
    • specify custom key of error message

e.g.)

class User < ActiveRecord::Base
  validates(:name,
            username_not_reserved: {
              additional_reserved_names: %w[foo bar],
              case_insensitive: true,
              message: :reserved_username
            }
           )
end

Referenced resources

With tons of thanks 🍣