UsernameNotReservedValidator
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)
endoptions
-
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_insencitivewill 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
}
)
endReferenced resources
- http://qiita.com/phimcall/items/4c559b70f70ea7f1953b
- http://bitarts.jp/blog/archives/004363.html
- https://github.com/balexand/email_validator
With tons of thanks 🍣