README.md in flash_validators-0.0.1 vs README.md in flash_validators-1.0.0

- old
+ new

@@ -4,11 +4,11 @@ [![Coverage Status](https://coveralls.io/repos/drexed/flash_validators/badge.png)](https://coveralls.io/r/drexed/flash_validators) [![Code Climate](https://codeclimate.com/github/drexed/flash_validators.png)](https://codeclimate.com/github/drexed/flash_validators) Flash Validators is a collection of custom validators that are often required in Rails applications plus shoulda-style RSpec matchers to test the validation rules. -Currently supported validators: boolean, currency, email, equality, hex, IMEI, IP, latitude, longitude, mac address, name, password, phone, slug, ssn, url, and username. +Currently supported validators: alpha, alpha-numeric, boolean, credit card, currency, email, equality, hex, IMEI, IP, latitude, longitude, mac address, name, password, phone, slug, ssn, url, and username. Highly recommended validators: * **DateTime:** Validates Timeliness - https://github.com/adzap/validates_timeliness * **Existence:** Validates Existence - https://github.com/perfectline/validates_existence * **Group:** Group Validations - https://github.com/adzap/grouped_validations @@ -28,39 +28,178 @@ $ gem install flash_validators ## Usage +### AlphaValidator + +**Ex:** Example or Example Title + +**Rules:** + * Characters: A-Z a-z + * Must include: A-Z a-z + +With an ActiveRecord model: + +```ruby +class Book < ActiveRecord::Base + attr_accessor :title, :name + validates :title, alpha: true +end +``` + +Or any ruby class: + +```ruby +class Book + include ActiveModel::Validations + attr_accessor :title, :name + validates :title, alpha: true +end +``` + +Options: :strict, :lowercase, :uppercase + +```ruby +validates :title, alpha: { strict: true } +validates :title, alpha: { lowercase: true } +validates :title, alpha: { uppsercase: true } +``` + +RSpec matcher is also available for your convenience: + +```ruby +describe Product do + it { should ensure_valid_alpha_format_of(:title) } + it { should_not ensure_valid_alpha_format_of(:name) } +end +``` + +### AlphaNumericValidator + +**Ex:** Example1 or Example Title 1 + +**Rules:** + * Characters: A-Z a-z 0-9 + * Must include: A-Z a-z 0-9 + +With an ActiveRecord model: + +```ruby +class Book < ActiveRecord::Base + attr_accessor :title, :name + validates :title, alpha_numeric: true +end +``` + +Or any ruby class: + +```ruby +class Book + include ActiveModel::Validations + attr_accessor :title, :name + validates :title, alpha_numeric: true +end +``` + +Strict: requires not including spaces + +```ruby +validates :title, alpha_numeric: { strict: true } +``` + +RSpec matcher is also available for your convenience: + +```ruby +describe Product do + it { should ensure_valid_alpha_numeric_format_of(:title) } + it { should_not ensure_valid_alpha_numeric_format_of(:name) } +end +``` + ### BooleanValidator **Ex:** true or false or 1 or 0 **Rules:** * Characters: 0-1 true or false With an ActiveRecord model: - class User < ActiveRecord::Base - attr_accessor :active, :name - validates :active, boolean: true - end +```ruby +class User < ActiveRecord::Base + attr_accessor :active, :name + validates :active, boolean: true +end +``` Or any ruby class: - class User - include ActiveModel::Validations - attr_accessor :active, :name - validates :active, boolean: true - end +```ruby +class User + include ActiveModel::Validations + attr_accessor :active, :name + validates :active, boolean: true +end +``` RSpec matcher is also available for your convenience: - describe Product do - it { should ensure_valid_boolean_format_of(:active) } - it { should_not ensure_valid_boolean_format_of(:name) } - end +```ruby +describe Product do + it { should ensure_valid_boolean_format_of(:active) } + it { should_not ensure_valid_boolean_format_of(:name) } +end +``` +### CreditCardValidator +**Ex:** 370000000000002 + +**Rules:** + * Characters: 0-9 + * Must include: 0-9 + * Range for card digits: 15-16 + +With an ActiveRecord model: + +```ruby +class Invoice < ActiveRecord::Base + attr_accessor :cc_number, :name + validates :cc_number, credit_card: true +end +``` + +Or any ruby class: + +```ruby +class Invoice + include ActiveModel::Validations + attr_accessor :cc_number, :name + validates :cc_number, credit_card: true +end +``` + +Options: :american_express, :diners_club, :discover, :jbc, :master_card, :visa + +```ruby +validates :cc_number, currency: { american_express: true } +validates :cc_number, currency: { diners_club: true } +validates :cc_number, currency: { discover: true } +validates :cc_number, currency: { jbc: true } +validates :cc_number, currency: { master_card: true } +validates :cc_number, currency: { visa: true } +``` + +RSpec matcher is also available for your convenience: + +```ruby +describe Invoice do + it { should ensure_valid_credit_card_format_of(:cc_number) } + it { should_not ensure_valid_credit_card_format_of(:name) } +end +``` + ### CurrencyValidator **Ex:** 123.00 or .1 **Rules:** @@ -68,33 +207,41 @@ * Must include: . * Range for cents: 1-2 With an ActiveRecord model: - class Product < ActiveRecord::Base - attr_accessor :price, :name - validates :price, currency: true - end +```ruby +class Product < ActiveRecord::Base + attr_accessor :price, :name + validates :price, currency: true +end +``` Or any ruby class: - class Product - include ActiveModel::Validations - attr_accessor :price, :name - validates :price, currency: true - end +```ruby +class Product + include ActiveModel::Validations + attr_accessor :price, :name + validates :price, currency: true +end +``` Strict: requires leading number and exactly two decimals, 1.45 - validates :price, currency: { strict: true } +```ruby +validates :price, currency: { strict: true } +``` RSpec matcher is also available for your convenience: - describe Product do - it { should ensure_valid_currency_format_of(:price) } - it { should_not ensure_valid_currency_format_of(:name) } - end +```ruby +describe Product do + it { should ensure_valid_currency_format_of(:price) } + it { should_not ensure_valid_currency_format_of(:name) } +end +``` ### EmailValidator **Ex:** user@example.com or user+123@example-site.com @@ -104,35 +251,43 @@ * Characters in domain: a-z 0-9 - * Must include extension: .co, .org, .museum With an ActiveRecord model: - class User < ActiveRecord::Base - attr_accessor :email, :name - validates :email, email: true - end +```ruby +class User < ActiveRecord::Base + attr_accessor :email, :name + validates :email, email: true +end +``` Or any ruby class: - class User - include ActiveModel::Validations - attr_accessor :email, :name - validates :email, email: true - end +```ruby +class User + include ActiveModel::Validations + attr_accessor :email, :name + validates :email, email: true +end +``` You can specify domains to which the email domain should belong in one of the folowing ways: - validates :email, email: { domains: 'com' } - validates :email, email: { domains: :com } - validates :email, email: { domains: [:com, 'edu'] } +```ruby +validates :email, email: { domains: 'com' } +validates :email, email: { domains: :com } +validates :email, email: { domains: [:com, 'edu'] } +``` RSpec matcher is also available for your convenience: - describe User do - it { should ensure_valid_email_format_of(:email) } - it { should_not ensure_valid_email_format_of(:name) } - end +```ruby +describe User do + it { should ensure_valid_email_format_of(:email) } + it { should_not ensure_valid_email_format_of(:name) } +end +``` ### EqualityValidator **Operators:** * Less than: x < y @@ -146,29 +301,35 @@ **Rules:** * Equal and not equal to: cannot be nil With an ActiveRecord model: - class Auction < ActiveRecord::Base - attr_accessor :bid, :price, :product - validates :bid, equality: { operator: :greater_than_or_equal_to, to: :price } - end +```ruby +class Auction < ActiveRecord::Base + attr_accessor :bid, :price, :product + validates :bid, equality: { operator: :greater_than_or_equal_to, to: :price } +end +``` Or any ruby class: - class Auction - include ActiveModel::Validations - attr_accessor :bid, :price, :product - validates :bid, equality: { operator: :greater_than_or_equal_to, to: :price } - end +```ruby +class Auction + include ActiveModel::Validations + attr_accessor :bid, :price, :product + validates :bid, equality: { operator: :greater_than_or_equal_to, to: :price } +end +``` RSpec matcher is also available for your convenience: - describe Auction do - it { should ensure_equality_of(:bid).to(:price) } - it { should_not ensure_equality_of(:bid).to(:product) } - end +```ruby +describe Auction do + it { should ensure_equality_of(:bid).to(:price) } + it { should_not ensure_equality_of(:bid).to(:product) } +end +``` ### HexValidator **Ex:** #a9a9a9 or #999 or aaaaaa @@ -177,29 +338,35 @@ * Length: 3 or 6 * Characters: a-f 0-9 With an ActiveRecord model: - class Profile < ActiveRecord::Base - attr_accessor :color, :trim - validates :color, hex: true - end +```ruby +class Profile < ActiveRecord::Base + attr_accessor :color, :trim + validates :color, hex: true +end +``` Or any ruby class: - class Profile - include ActiveModel::Validations - attr_accessor :color, :trim - validates :color, hex: true - end +```ruby +class Profile + include ActiveModel::Validations + attr_accessor :color, :trim + validates :color, hex: true +end +``` RSpec matcher is also available for your convenience: - describe Color do - it { should ensure_valid_hex_format_of(:color) } - it { should_not ensure_valid_hex_format_of(:trim) } - end +```ruby +describe Color do + it { should ensure_valid_hex_format_of(:color) } + it { should_not ensure_valid_hex_format_of(:trim) } +end +``` ### ImeiValidator **Ex:** 356843052637512 or 35-6843052-637512 or 35.6843052.637512 @@ -207,29 +374,35 @@ * Length: min 14 * Characters: 0-9 -. With an ActiveRecord model: - class User < ActiveRecord::Base - attr_accessor :imei, :name - validates :imei, imei: true - end +```ruby +class User < ActiveRecord::Base + attr_accessor :imei, :name + validates :imei, imei: true +end +``` Or any ruby class: - class User - include ActiveModel::Validations - attr_accessor :imei, :name - validates :imei, imei: true - end +```ruby +class User + include ActiveModel::Validations + attr_accessor :imei, :name + validates :imei, imei: true +end +``` RSpec matcher is also available for your convenience: - describe User do - it { should ensure_valid_imei_format_of(:imei) } - it { should_not ensure_valid_imei_format_of(:name) } - end +```ruby +describe User do + it { should ensure_valid_imei_format_of(:imei) } + it { should_not ensure_valid_imei_format_of(:name) } +end +``` ### IpValidator **Ex:** 0.0.0.0 or 127.0.0.1 or 167.39.240.31 @@ -237,29 +410,35 @@ * Length: min 7 * Characters: 0-9 . With an ActiveRecord model: - class User < ActiveRecord::Base - attr_accessor :ip, :name - validates :ip, ip: true - end +```ruby +class User < ActiveRecord::Base + attr_accessor :ip, :name + validates :ip, ip: true +end +``` Or any ruby class: - class User - include ActiveModel::Validations - attr_accessor :ip, :name - validates :ip, ip: true - end +```ruby +class User + include ActiveModel::Validations + attr_accessor :ip, :name + validates :ip, ip: true +end +``` RSpec matcher is also available for your convenience: - describe User do - it { should ensure_valid_ip_format_of(:ip) } - it { should_not ensure_valid_ip_format_of(:name) } - end +```ruby +describe User do + it { should ensure_valid_ip_format_of(:ip) } + it { should_not ensure_valid_ip_format_of(:name) } +end +``` ### LatitudeValidator **Ex:** 78.213 or -34.985 @@ -267,29 +446,35 @@ * Range: 90 to -90 * Characters: 0-9 With an ActiveRecord model: - class Location < ActiveRecord::Base - attr_accessor :lat, :name - validates :lat, latitude: true - end +```ruby +class Location < ActiveRecord::Base + attr_accessor :lat, :name + validates :lat, latitude: true +end +``` Or any ruby class: - class Location - include ActiveModel::Validations - attr_accessor :lat, :name - validates :lat, latitude: true - end +```ruby +class Location + include ActiveModel::Validations + attr_accessor :lat, :name + validates :lat, latitude: true +end +``` RSpec matcher is also available for your convenience: - describe Location do - it { should ensure_valid_latitude_format_of(:lat) } - it { should_not ensure_valid_latitude_format_of(:name) } - end +```ruby +describe Location do + it { should ensure_valid_latitude_format_of(:lat) } + it { should_not ensure_valid_latitude_format_of(:name) } +end +``` ### LongitudeValidator **Ex:** 165.371 or -56.152 @@ -297,29 +482,35 @@ * Range: 180 to -180 * Characters: 0-9 With an ActiveRecord model: - class Location < ActiveRecord::Base - attr_accessor :lon, :name - validates :lon, longitude: true - end +```ruby +class Location < ActiveRecord::Base + attr_accessor :lon, :name + validates :lon, longitude: true +end +``` Or any ruby class: - class Location - include ActiveModel::Validations - attr_accessor :lon, :name - validates :lon, longitude: true - end +```ruby +class Location + include ActiveModel::Validations + attr_accessor :lon, :name + validates :lon, longitude: true +end +``` RSpec matcher is also available for your convenience: - describe Location do - it { should ensure_valid_longitude_format_of(:lon) } - it { should_not ensure_valid_longitude_format_of(:name) } - end +```ruby +describe Location do + it { should ensure_valid_longitude_format_of(:lon) } + it { should_not ensure_valid_longitude_format_of(:name) } +end +``` ### MacAddressValidator **Ex:** '08:00:2b:01:02:03' @@ -332,29 +523,35 @@ **Rules:** * Characters: a-z 0-9 -.: With an ActiveRecord model: - class Device < ActiveRecord::Base - attr_accessor :mac, :name - validates :mac, mac_address: true - end +```ruby +class Device < ActiveRecord::Base + attr_accessor :mac, :name + validates :mac, mac_address: true +end +``` Or any ruby class: - class Device - include ActiveModel::Validations - attr_accessor :mac, :name - validates :mac, mac_address: true - end +```ruby +class Device + include ActiveModel::Validations + attr_accessor :mac, :name + validates :mac, mac_address: true +end +``` RSpec matcher is also available for your convenience: - describe Device do - it { should ensure_valid_mac_address_format_of } - it { should_not ensure_valid_mac_address_format_of(:name) } - end +```ruby +describe Device do + it { should ensure_valid_mac_address_format_of } + it { should_not ensure_valid_mac_address_format_of(:name) } +end +``` ### NameValidator **Ex:** James Brown or Billy Bob Thorton Jr @@ -363,29 +560,35 @@ * Characters: a-z - * Must include: First Last With an ActiveRecord model: - class User < ActiveRecord::Base - attr_accessor :name, :email - validates :name, name: true - end +```ruby +class User < ActiveRecord::Base + attr_accessor :name, :email + validates :name, name: true +end +``` Or any ruby class: - class User - include ActiveModel::Validations - attr_accessor :name, :email - validates :name, name: true - end +```ruby +class User + include ActiveModel::Validations + attr_accessor :name, :email + validates :name, name: true +end +``` RSpec matcher is also available for your convenience: - describe User do - it { should ensure_valid_name_format_of(:name) } - it { should_not ensure_valid_name_format_of(:email) } - end +```ruby +describe User do + it { should ensure_valid_name_format_of(:name) } + it { should_not ensure_valid_name_format_of(:email) } +end +``` ### PasswordValidator **Ex:** password or password123 or pa!!word @@ -393,116 +596,146 @@ * Range: 6-18 * Characters: a-z 0-9 -_!@#$%^&* With an ActiveRecord model: - class User < ActiveRecord::Base - attr_accessor :password, :name - validates :password, password: true - end +```ruby +class User < ActiveRecord::Base + attr_accessor :password, :name + validates :password, password: true +end +``` Or any ruby class: - class User - include ActiveModel::Validations - attr_accessor :password, :name - validates :password, name: true - end +```ruby +class User + include ActiveModel::Validations + attr_accessor :password, :name + validates :password, name: true +end +``` +Strict: requires length between 6 and 18, one number, lowercase, upcase letter + +```ruby +validates :password, password: { strict: true } +``` + RSpec matcher is also available for your convenience: - describe User do - it { should ensure_valid_password_format_of(:password) } - it { should_not ensure_valid_password_format_of(:name) } - end +```ruby +describe User do + it { should ensure_valid_password_format_of(:password) } + it { should_not ensure_valid_password_format_of(:name) } +end +``` ### PhoneValidator **Ex:** 555 333 4444 or (555) 123-4567 or +1 (555) 123 4567 ext-890 **Rules:** * Characters: a-z 0-9 -()+ With an ActiveRecord model: - class User < ActiveRecord::Base - attr_accessor :phone, :name - validates :phone, phone: true - end +```ruby +class User < ActiveRecord::Base + attr_accessor :phone, :name + validates :phone, phone: true +end +``` Or any ruby class: - class User - include ActiveModel::Validations - attr_accessor :phone, :name - validates :phone, phone: true - end +```ruby +class User + include ActiveModel::Validations + attr_accessor :phone, :name + validates :phone, phone: true +end +``` RSpec matcher is also available for your convenience: - describe User do - it { should ensure_valid_phone_format_of(:phone) } - it { should_not ensure_valid_phone_format_of(:name) } - end +```ruby +describe User do + it { should ensure_valid_phone_format_of(:phone) } + it { should_not ensure_valid_phone_format_of(:name) } +end +``` ### SlugValidator **Ex:** slug1234 or slug-1234 **Rules:** * Characters: a-z 0-9 - With an ActiveRecord model: - class User < ActiveRecord::Base - attr_accessor :slug, :name - validates :slug, slug: true - end +```ruby +class User < ActiveRecord::Base + attr_accessor :slug, :name + validates :slug, slug: true +end +``` Or any ruby class: - class User - include ActiveModel::Validations - attr_accessor :slug, :name - validates :slug, slug: true - end +```ruby +class User + include ActiveModel::Validations + attr_accessor :slug, :name + validates :slug, slug: true +end +``` RSpec matcher is also available for your convenience: - describe User do - it { should ensure_valid_slug_format_of(:slug) } - it { should_not ensure_valid_slug_format_of(:name) } - end +```ruby +describe User do + it { should ensure_valid_slug_format_of(:slug) } + it { should_not ensure_valid_slug_format_of(:name) } +end +``` ### SsnValidator **Ex:** 333-22-4444 or 333224444 **Rules:** * Characters: 0-9 - With an ActiveRecord model: - class User < ActiveRecord::Base - attr_accessor :ssn, :name - validates :ssn, ssn: true - end +```ruby +class User < ActiveRecord::Base + attr_accessor :ssn, :name + validates :ssn, ssn: true +end +``` Or any ruby class: - class User - include ActiveModel::Validations - attr_accessor :ssn, :name - validates :ssn, ssn: true - end +```ruby +class User + include ActiveModel::Validations + attr_accessor :ssn, :name + validates :ssn, ssn: true +end +``` RSpec matcher is also available for your convenience: - describe User do - it { should ensure_valid_ssn_format_of(:ssn) } - it { should_not ensure_valid_ssn_format_of(:name) } - end +```ruby +describe User do + it { should ensure_valid_ssn_format_of(:ssn) } + it { should_not ensure_valid_ssn_format_of(:name) } +end +``` ### UrlValidator **Ex:** example.com or http://www.example.com @@ -511,44 +744,56 @@ * Characters in domain: a-z 0-9 - * Must include extension: .co, .org, .museum With an ActiveRecord model: - class User < ActiveRecord::Base - attr_accessor :url, :name - validates :url, url: true - end +```ruby +class User < ActiveRecord::Base + attr_accessor :url, :name + validates :url, url: true +end +``` Or any ruby class: - class User - include ActiveModel::Validations - attr_accessor :url, :name - validates :url, url: true - end +```ruby +class User + include ActiveModel::Validations + attr_accessor :url, :name + validates :url, url: true +end +``` You can specify domains to which the URL domain should belong in one of the folowing ways: - validates :url, url: { domains: 'com' } - validates :url, url: { domains: :com } - validates :url, url: { domains: [:com, 'edu'] } +```ruby +validates :url, url: { domains: 'com' } +validates :url, url: { domains: :com } +validates :url, url: { domains: [:com, 'edu'] } +``` You can specify if the URL should the site root: - validates :url, url: { root: true } +```ruby +validates :url, url: { root: true } +``` You can specify the URL scheme: - validates :url, url: { scheme: :http } - validates :url, url: { scheme: [:http, 'https'] } +```ruby +validates :url, url: { scheme: :http } +validates :url, url: { scheme: [:http, 'https'] } +``` RSpec matcher is also available for your convenience: - describe User do - it { should ensure_valid_url_format_of(:url) } - it { should_not ensure_valid_url_format_of(:name) } - end +```ruby +describe User do + it { should ensure_valid_url_format_of(:url) } + it { should_not ensure_valid_url_format_of(:name) } +end +``` ### UsernameValidator **Ex:** username123 or username @@ -556,28 +801,34 @@ * Range: 2-16 * Characters: a-z 0-9 -_ With an ActiveRecord model: - class User < ActiveRecord::Base - attr_accessor :username, :name - validates :username, username: true - end +```ruby +class User < ActiveRecord::Base + attr_accessor :username, :name + validates :username, username: true +end +``` Or any ruby class: - class User - include ActiveModel::Validations - attr_accessor :username, :name - validates :username, username: true - end +```ruby +class User + include ActiveModel::Validations + attr_accessor :username, :name + validates :username, username: true +end +``` RSpec matcher is also available for your convenience: - describe User do - it { should ensure_valid_username_format_of(:username) } - it { should_not ensure_valid_username_format_of(:name) } - end +```ruby +describe User do + it { should ensure_valid_username_format_of(:username) } + it { should_not ensure_valid_username_format_of(:name) } +end +``` ## Contributing Your contribution is welcome.