Skip to content

custom validator that validates username is not included in the list of reserved names for ActiveModel

License

Notifications You must be signed in to change notification settings

kwappa/username_not_reserved_validator

Repository files navigation

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 🍣

About

custom validator that validates username is not included in the list of reserved names for ActiveModel

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages