active_validation 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.coveralls.yml +1 -0
- data/.gitignore +14 -0
- data/.rspec +4 -0
- data/.travis.yml +13 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +1099 -0
- data/Rakefile +1 -0
- data/active_validation.gemspec +29 -0
- data/config/locales/en.yml +109 -0
- data/lib/active_validation/matchers/ensure_valid_alpha_format_of.rb +26 -0
- data/lib/active_validation/matchers/ensure_valid_alpha_numeric_format_of.rb +26 -0
- data/lib/active_validation/matchers/ensure_valid_base64_format_of.rb +26 -0
- data/lib/active_validation/matchers/ensure_valid_boolean_format_of.rb +26 -0
- data/lib/active_validation/matchers/ensure_valid_credit_card_format_of.rb +26 -0
- data/lib/active_validation/matchers/ensure_valid_currency_format_of.rb +26 -0
- data/lib/active_validation/matchers/ensure_valid_cusip_format_of.rb +26 -0
- data/lib/active_validation/matchers/ensure_valid_email_format_of.rb +26 -0
- data/lib/active_validation/matchers/ensure_valid_equality_matcher_of.rb +40 -0
- data/lib/active_validation/matchers/ensure_valid_hex_format_of.rb +26 -0
- data/lib/active_validation/matchers/ensure_valid_imei_format_of.rb +26 -0
- data/lib/active_validation/matchers/ensure_valid_ip_format_of.rb +26 -0
- data/lib/active_validation/matchers/ensure_valid_isbn_format_of.rb +26 -0
- data/lib/active_validation/matchers/ensure_valid_isin_format_of.rb +26 -0
- data/lib/active_validation/matchers/ensure_valid_latitude_format_of.rb +26 -0
- data/lib/active_validation/matchers/ensure_valid_longitude_format_of.rb +26 -0
- data/lib/active_validation/matchers/ensure_valid_mac_address_format_of.rb +26 -0
- data/lib/active_validation/matchers/ensure_valid_name_format_of.rb +26 -0
- data/lib/active_validation/matchers/ensure_valid_password_format_of.rb +26 -0
- data/lib/active_validation/matchers/ensure_valid_phone_format_of.rb +26 -0
- data/lib/active_validation/matchers/ensure_valid_sedol_format_of.rb +26 -0
- data/lib/active_validation/matchers/ensure_valid_slug_format_of.rb +26 -0
- data/lib/active_validation/matchers/ensure_valid_ssn_format_of.rb +26 -0
- data/lib/active_validation/matchers/ensure_valid_url_format_of.rb +26 -0
- data/lib/active_validation/matchers/ensure_valid_username_format_of.rb +26 -0
- data/lib/active_validation/matchers/ensure_valid_uuid_format_of.rb +26 -0
- data/lib/active_validation/validators/alpha_numeric_validator.rb +30 -0
- data/lib/active_validation/validators/alpha_validator.rb +31 -0
- data/lib/active_validation/validators/base64_validator.rb +9 -0
- data/lib/active_validation/validators/boolean_validator.rb +9 -0
- data/lib/active_validation/validators/credit_card_validator.rb +133 -0
- data/lib/active_validation/validators/currency_validator.rb +23 -0
- data/lib/active_validation/validators/cusip_validator.rb +33 -0
- data/lib/active_validation/validators/email_validator.rb +25 -0
- data/lib/active_validation/validators/equality_validator.rb +27 -0
- data/lib/active_validation/validators/hex_validator.rb +9 -0
- data/lib/active_validation/validators/imei_validator.rb +37 -0
- data/lib/active_validation/validators/ip_validator.rb +9 -0
- data/lib/active_validation/validators/isbn_validator.rb +24 -0
- data/lib/active_validation/validators/isin_validator.rb +38 -0
- data/lib/active_validation/validators/latitude_validator.rb +9 -0
- data/lib/active_validation/validators/longitude_validator.rb +9 -0
- data/lib/active_validation/validators/mac_address_validator.rb +24 -0
- data/lib/active_validation/validators/name_validator.rb +9 -0
- data/lib/active_validation/validators/password_validator.rb +23 -0
- data/lib/active_validation/validators/phone_validator.rb +9 -0
- data/lib/active_validation/validators/sedol_validator.rb +32 -0
- data/lib/active_validation/validators/slug_validator.rb +9 -0
- data/lib/active_validation/validators/ssn_validator.rb +9 -0
- data/lib/active_validation/validators/url_validator.rb +36 -0
- data/lib/active_validation/validators/username_validator.rb +9 -0
- data/lib/active_validation/validators/uuid_validator.rb +28 -0
- data/lib/active_validation/version.rb +3 -0
- data/lib/active_validation.rb +91 -0
- data/spec/lib/alpha_numeric_validator_spec.rb +91 -0
- data/spec/lib/alpha_validator_spec.rb +182 -0
- data/spec/lib/base64_validator_spec.rb +33 -0
- data/spec/lib/boolean_validator_spec.rb +35 -0
- data/spec/lib/credit_card_validator_spec.rb +686 -0
- data/spec/lib/currency_validator_spec.rb +63 -0
- data/spec/lib/cusip_validator_spec.rb +27 -0
- data/spec/lib/email_validator_spec.rb +109 -0
- data/spec/lib/equality_validator_spec.rb +334 -0
- data/spec/lib/hex_validator_spec.rb +73 -0
- data/spec/lib/imei_validator_spec.rb +41 -0
- data/spec/lib/ip_validator_spec.rb +33 -0
- data/spec/lib/isbn_validator_spec.rb +41 -0
- data/spec/lib/isin_validator_spec.rb +35 -0
- data/spec/lib/latitude_validator_spec.rb +31 -0
- data/spec/lib/longitude_validator_spec.rb +31 -0
- data/spec/lib/mac_address_validator_spec.rb +54 -0
- data/spec/lib/name_validator_spec.rb +39 -0
- data/spec/lib/password_validator_spec.rb +85 -0
- data/spec/lib/phone_validator_spec.rb +42 -0
- data/spec/lib/sedol_validator_spec.rb +31 -0
- data/spec/lib/slug_validator_spec.rb +41 -0
- data/spec/lib/ssn_validator_spec.rb +36 -0
- data/spec/lib/url_validator_spec.rb +106 -0
- data/spec/lib/username_validator_spec.rb +37 -0
- data/spec/lib/uuid_validator_spec.rb +157 -0
- data/spec/spec_helper.rb +12 -0
- metadata +260 -0
@@ -0,0 +1,9 @@
|
|
1
|
+
class LongitudeValidator < ActiveModel::EachValidator
|
2
|
+
|
3
|
+
def validate_each(record, attribute, value)
|
4
|
+
unless value.present? && value >= -180 && value <= 180
|
5
|
+
record.errors[attribute] << (options[:message] || I18n.t('active_validation.errors.messages.longitude'))
|
6
|
+
end
|
7
|
+
end
|
8
|
+
|
9
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
class MacAddressValidator < ActiveModel::EachValidator
|
2
|
+
|
3
|
+
def validate_each(record, attribute, value)
|
4
|
+
unless valid?(value)
|
5
|
+
record.errors[attribute] << (options[:message] || I18n.t('active_validation.errors.messages.mac_address'))
|
6
|
+
end
|
7
|
+
end
|
8
|
+
|
9
|
+
private
|
10
|
+
|
11
|
+
def valid_format?(value)
|
12
|
+
(value =~ /^([\h]{2}:){5}[\h]{2}?$/i) ||
|
13
|
+
(value =~ /^([\h]{2}[-|\.|\s]){5}[\h]{2}?$/i) ||
|
14
|
+
(value =~ /^([\h]{6})[-|\.][\h]{6}?$/i) ||
|
15
|
+
(value =~ /^([\h]{6}):[\h]{6}?$/i) ||
|
16
|
+
(value =~ /^([\h]{4}[-|\.|\s]){2}[\h]{4}?$/i) ||
|
17
|
+
(value =~ /^[\h]{12}?$/i)
|
18
|
+
end
|
19
|
+
|
20
|
+
def valid?(value)
|
21
|
+
valid_format?(value)
|
22
|
+
end
|
23
|
+
|
24
|
+
end
|
@@ -0,0 +1,9 @@
|
|
1
|
+
class NameValidator < ActiveModel::EachValidator
|
2
|
+
|
3
|
+
def validate_each(record, attribute, value)
|
4
|
+
unless value =~ /\A([a-zA-Z'-]+\s+){1,4}[a-zA-Z'-]*\z/i
|
5
|
+
record.errors[attribute] << (options[:message] || I18n.t('active_validation.errors.messages.name'))
|
6
|
+
end
|
7
|
+
end
|
8
|
+
|
9
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
class PasswordValidator < ActiveModel::EachValidator
|
2
|
+
|
3
|
+
def validate_each(record, attribute, value)
|
4
|
+
unless valid?(value, options)
|
5
|
+
record.errors[attribute] << (options[:message] || I18n.t('active_validation.errors.messages.password'))
|
6
|
+
end
|
7
|
+
end
|
8
|
+
|
9
|
+
private
|
10
|
+
|
11
|
+
def valid_format?(value, options)
|
12
|
+
if options[:strict]
|
13
|
+
value =~ /^(?=^.{1,255}$)((?=.*[A-Za-z0-9])(?=.*[A-Z])(?=.*[a-z]))^.*$/
|
14
|
+
else
|
15
|
+
value =~ /^[a-z0-9!@#$%^&*_-]{1,255}$/
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
def valid?(value, options)
|
20
|
+
valid_format?(value, options)
|
21
|
+
end
|
22
|
+
|
23
|
+
end
|
@@ -0,0 +1,9 @@
|
|
1
|
+
class PhoneValidator < ActiveModel::EachValidator
|
2
|
+
|
3
|
+
def validate_each(record, attribute, value)
|
4
|
+
unless value =~ /^[0-9+\(\)#\.\s\/ext-]+$/i
|
5
|
+
record.errors[attribute] << (options[:message] || I18n.t('active_validation.errors.messages.phone'))
|
6
|
+
end
|
7
|
+
end
|
8
|
+
|
9
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
class SedolValidator < ActiveModel::EachValidator
|
2
|
+
|
3
|
+
def validate_each(record, attribute, value)
|
4
|
+
unless valid?(value)
|
5
|
+
record.errors[attribute] << (options[:message] || I18n.t('active_validation.errors.messages.sedol'))
|
6
|
+
end
|
7
|
+
end
|
8
|
+
|
9
|
+
private
|
10
|
+
|
11
|
+
def valid_format?(value)
|
12
|
+
value =~ /^([A-Z0-9]{6})(\d{1})$/
|
13
|
+
end
|
14
|
+
|
15
|
+
def valid_checksum?(value)
|
16
|
+
digits = value.split('').map { |i| i.match(/[A-Z]/) ? (i.ord - 55) : i.to_i }
|
17
|
+
weights = [1, 3, 1, 7, 3, 9, 1]
|
18
|
+
sum = 0
|
19
|
+
|
20
|
+
digits.each_with_index do |i, idx|
|
21
|
+
sum += weights[idx] * i
|
22
|
+
end
|
23
|
+
|
24
|
+
(10 - sum % 10) % 10
|
25
|
+
end
|
26
|
+
|
27
|
+
def valid?(value)
|
28
|
+
valid_format?(value) &&
|
29
|
+
valid_checksum?(value)
|
30
|
+
end
|
31
|
+
|
32
|
+
end
|
@@ -0,0 +1,9 @@
|
|
1
|
+
class SsnValidator < ActiveModel::EachValidator
|
2
|
+
|
3
|
+
def validate_each(record, attribute, value)
|
4
|
+
unless value =~ /^\A([\d]{3}\-[\d]{2}\-[\d]{4}|[\d]{9})\Z$/
|
5
|
+
record.errors[attribute] << (options[:message] || I18n.t('active_validation.errors.messages.ssn'))
|
6
|
+
end
|
7
|
+
end
|
8
|
+
|
9
|
+
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
require 'uri'
|
2
|
+
class UrlValidator < ActiveModel::EachValidator
|
3
|
+
|
4
|
+
def validate_each(record, attribute, value)
|
5
|
+
uri = URI.parse(value)
|
6
|
+
raise URI::InvalidURIError unless valid?(uri, options)
|
7
|
+
rescue URI::InvalidURIError
|
8
|
+
record.errors[attribute] << (options[:message] || I18n.t('active_validation.errors.messages.url'))
|
9
|
+
end
|
10
|
+
|
11
|
+
private
|
12
|
+
|
13
|
+
DEFAULT_SCHEMES = [:http, :https]
|
14
|
+
|
15
|
+
def valid_domain?(value, options)
|
16
|
+
value_downcased = value.host.to_s.downcase
|
17
|
+
options.empty? || options.any? { |domain| value_downcased.end_with?(".#{domain.downcase}") }
|
18
|
+
end
|
19
|
+
|
20
|
+
def valid_scheme?(value, options)
|
21
|
+
value_downcased = value.scheme.to_s.downcase
|
22
|
+
options.empty? || options.any? { |scheme| value_downcased == scheme.to_s.downcase }
|
23
|
+
end
|
24
|
+
|
25
|
+
def valid_root?(value)
|
26
|
+
['/', ''].include?(value.path) && value.query.blank? && value.fragment.blank?
|
27
|
+
end
|
28
|
+
|
29
|
+
def valid?(value, options)
|
30
|
+
value.kind_of?(URI::Generic) &&
|
31
|
+
valid_domain?(value, [*(options[:domain])]) &&
|
32
|
+
valid_scheme?(value, [*(options[:scheme] || UrlValidator::DEFAULT_SCHEMES)]) &&
|
33
|
+
(options[:root] ? valid_root?(value) : true)
|
34
|
+
end
|
35
|
+
|
36
|
+
end
|
@@ -0,0 +1,9 @@
|
|
1
|
+
class UsernameValidator < ActiveModel::EachValidator
|
2
|
+
|
3
|
+
def validate_each(record, attribute, value)
|
4
|
+
unless value =~ /^[a-z0-9_-]{2,16}$/
|
5
|
+
record.errors[attribute] << (options[:message] || I18n.t('active_validation.errors.messages.username'))
|
6
|
+
end
|
7
|
+
end
|
8
|
+
|
9
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
class UuidValidator < ActiveModel::EachValidator
|
2
|
+
|
3
|
+
def validate_each(record, attribute, value)
|
4
|
+
unless valid?(value, options)
|
5
|
+
record.errors[attribute] << (options[:message] || I18n.t('active_validation.errors.messages.uuid'))
|
6
|
+
end
|
7
|
+
end
|
8
|
+
|
9
|
+
private
|
10
|
+
|
11
|
+
def valid_format?(value, options)
|
12
|
+
case options[:version]
|
13
|
+
when 3
|
14
|
+
value =~ /^[0-9A-F]{8}-[0-9A-F]{4}-3[0-9A-F]{3}-[0-9A-F]{4}-[0-9A-F]{12}$/i
|
15
|
+
when 4
|
16
|
+
value =~ /^[0-9A-F]{8}-[0-9A-F]{4}-4[0-9A-F]{3}-[89AB][0-9A-F]{3}-[0-9A-F]{12}$/i
|
17
|
+
when 5
|
18
|
+
value =~ /^[0-9A-F]{8}-[0-9A-F]{4}-5[0-9A-F]{3}-[89AB][0-9A-F]{3}-[0-9A-F]{12}$/i
|
19
|
+
else
|
20
|
+
value =~ /^[0-9A-F]{8}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{12}$/i
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
def valid?(value, options)
|
25
|
+
valid_format?(value, options)
|
26
|
+
end
|
27
|
+
|
28
|
+
end
|
@@ -0,0 +1,91 @@
|
|
1
|
+
require 'active_model'
|
2
|
+
require 'active_support'
|
3
|
+
require 'active_validation/version'
|
4
|
+
|
5
|
+
require 'active_validation/validators/alpha_validator'
|
6
|
+
require 'active_validation/validators/alpha_numeric_validator'
|
7
|
+
require 'active_validation/validators/base64_validator'
|
8
|
+
require 'active_validation/validators/boolean_validator'
|
9
|
+
require 'active_validation/validators/credit_card_validator'
|
10
|
+
require 'active_validation/validators/currency_validator'
|
11
|
+
require 'active_validation/validators/cusip_validator'
|
12
|
+
require 'active_validation/validators/email_validator'
|
13
|
+
require 'active_validation/validators/equality_validator'
|
14
|
+
require 'active_validation/validators/hex_validator'
|
15
|
+
require 'active_validation/validators/imei_validator'
|
16
|
+
require 'active_validation/validators/ip_validator'
|
17
|
+
require 'active_validation/validators/isbn_validator'
|
18
|
+
require 'active_validation/validators/isin_validator'
|
19
|
+
require 'active_validation/validators/latitude_validator'
|
20
|
+
require 'active_validation/validators/longitude_validator'
|
21
|
+
require 'active_validation/validators/mac_address_validator'
|
22
|
+
require 'active_validation/validators/name_validator'
|
23
|
+
require 'active_validation/validators/password_validator'
|
24
|
+
require 'active_validation/validators/phone_validator'
|
25
|
+
require 'active_validation/validators/sedol_validator'
|
26
|
+
require 'active_validation/validators/slug_validator'
|
27
|
+
require 'active_validation/validators/ssn_validator'
|
28
|
+
require 'active_validation/validators/url_validator'
|
29
|
+
require 'active_validation/validators/username_validator'
|
30
|
+
require 'active_validation/validators/uuid_validator'
|
31
|
+
|
32
|
+
if defined?(RSpec)
|
33
|
+
require 'rspec/matchers'
|
34
|
+
require 'active_validation/matchers/ensure_valid_alpha_format_of'
|
35
|
+
require 'active_validation/matchers/ensure_valid_alpha_numeric_format_of'
|
36
|
+
require 'active_validation/matchers/ensure_valid_base64_format_of'
|
37
|
+
require 'active_validation/matchers/ensure_valid_boolean_format_of'
|
38
|
+
require 'active_validation/matchers/ensure_valid_credit_card_format_of'
|
39
|
+
require 'active_validation/matchers/ensure_valid_currency_format_of'
|
40
|
+
require 'active_validation/matchers/ensure_valid_cusip_format_of'
|
41
|
+
require 'active_validation/matchers/ensure_valid_email_format_of'
|
42
|
+
require 'active_validation/matchers/ensure_valid_equality_matcher_of'
|
43
|
+
require 'active_validation/matchers/ensure_valid_hex_format_of'
|
44
|
+
require 'active_validation/matchers/ensure_valid_imei_format_of'
|
45
|
+
require 'active_validation/matchers/ensure_valid_ip_format_of'
|
46
|
+
require 'active_validation/matchers/ensure_valid_isbn_format_of'
|
47
|
+
require 'active_validation/matchers/ensure_valid_isin_format_of'
|
48
|
+
require 'active_validation/matchers/ensure_valid_latitude_format_of'
|
49
|
+
require 'active_validation/matchers/ensure_valid_longitude_format_of'
|
50
|
+
require 'active_validation/matchers/ensure_valid_mac_address_format_of'
|
51
|
+
require 'active_validation/matchers/ensure_valid_name_format_of'
|
52
|
+
require 'active_validation/matchers/ensure_valid_password_format_of'
|
53
|
+
require 'active_validation/matchers/ensure_valid_phone_format_of'
|
54
|
+
require 'active_validation/matchers/ensure_valid_sedol_format_of'
|
55
|
+
require 'active_validation/matchers/ensure_valid_slug_format_of'
|
56
|
+
require 'active_validation/matchers/ensure_valid_ssn_format_of'
|
57
|
+
require 'active_validation/matchers/ensure_valid_url_format_of'
|
58
|
+
require 'active_validation/matchers/ensure_valid_username_format_of'
|
59
|
+
require 'active_validation/matchers/ensure_valid_uuid_format_of'
|
60
|
+
end
|
61
|
+
|
62
|
+
if defined?(Rails)
|
63
|
+
require 'rails'
|
64
|
+
|
65
|
+
module ActiveValidation
|
66
|
+
class Railtie < ::Rails::Railtie
|
67
|
+
initializer 'active_validation' do |app|
|
68
|
+
ActiveValidation::Railtie.instance_eval do
|
69
|
+
locales = locales_from(app.config.i18n.available_locales)
|
70
|
+
|
71
|
+
locales.each do |locale|
|
72
|
+
if File.file?(path(locale))
|
73
|
+
I18n.load_path << path(locale)
|
74
|
+
end
|
75
|
+
end
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
protected
|
80
|
+
|
81
|
+
def self.path(locale)
|
82
|
+
File.expand_path("../../config/locales/#{locale}.yml", __FILE__)
|
83
|
+
end
|
84
|
+
|
85
|
+
def self.locales_from(args)
|
86
|
+
array = Array(args || [])
|
87
|
+
array.blank? ? '*' : array
|
88
|
+
end
|
89
|
+
end
|
90
|
+
end
|
91
|
+
end
|
@@ -0,0 +1,91 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe AlphaNumericValidator do
|
4
|
+
|
5
|
+
context "has a valid value" do
|
6
|
+
let(:klass) do
|
7
|
+
Class.new do
|
8
|
+
include ActiveModel::Validations
|
9
|
+
attr_accessor :title, :name
|
10
|
+
validates :title, alpha_numeric: true
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
subject { klass.new }
|
15
|
+
|
16
|
+
it { should allow_value("Example").for(:title) }
|
17
|
+
it { should allow_value("Example Title").for(:title) }
|
18
|
+
it { should allow_value("Example1").for(:title) }
|
19
|
+
it { should allow_value("Example 1").for(:title) }
|
20
|
+
|
21
|
+
it { should_not allow_value('').for(:title) }
|
22
|
+
it { should_not allow_value(' ').for(:title) }
|
23
|
+
it { should_not allow_value(nil).for(:title) }
|
24
|
+
it { should_not allow_value("Ex-ample").for(:title) }
|
25
|
+
it { should_not allow_value("Ex-ample1").for(:title) }
|
26
|
+
it { should_not allow_value("! \#$%\`|").for(:title) }
|
27
|
+
it { should_not allow_value("<>@[]\`|").for(:title) }
|
28
|
+
|
29
|
+
it { should ensure_valid_alpha_numeric_format_of(:title) }
|
30
|
+
it { should_not ensure_valid_alpha_numeric_format_of(:name) }
|
31
|
+
end
|
32
|
+
|
33
|
+
context "with :strict option has a valid value" do
|
34
|
+
let(:klass) do
|
35
|
+
Class.new do
|
36
|
+
include ActiveModel::Validations
|
37
|
+
attr_accessor :title, :name
|
38
|
+
validates :title, alpha_numeric: { strict: true }
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
subject { klass.new }
|
43
|
+
|
44
|
+
it { should allow_value("Example").for(:title) }
|
45
|
+
it { should allow_value("Example1").for(:title) }
|
46
|
+
|
47
|
+
it { should_not allow_value('').for(:title) }
|
48
|
+
it { should_not allow_value(' ').for(:title) }
|
49
|
+
it { should_not allow_value(nil).for(:title) }
|
50
|
+
it { should_not allow_value("Example Title").for(:title) }
|
51
|
+
it { should_not allow_value("Example 1").for(:title) }
|
52
|
+
it { should_not allow_value("Ex-ample").for(:title) }
|
53
|
+
it { should_not allow_value("Ex-ample1").for(:title) }
|
54
|
+
it { should_not allow_value("! \#$%\`|").for(:title) }
|
55
|
+
it { should_not allow_value("<>@[]\`|").for(:title) }
|
56
|
+
|
57
|
+
it { should ensure_valid_alpha_numeric_format_of(:title) }
|
58
|
+
it { should_not ensure_valid_alpha_numeric_format_of(:name) }
|
59
|
+
end
|
60
|
+
|
61
|
+
context "with case: :lower option has a valid value" do
|
62
|
+
let(:klass) do
|
63
|
+
Class.new do
|
64
|
+
include ActiveModel::Validations
|
65
|
+
attr_accessor :title, :name
|
66
|
+
validates :title, alpha_numeric: { case: :lower }
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
subject { klass.new }
|
71
|
+
|
72
|
+
it { should allow_value("example1").for(:title) }
|
73
|
+
it { should allow_value("example title 1").for(:title) }
|
74
|
+
|
75
|
+
it { should_not allow_value('').for(:title) }
|
76
|
+
it { should_not allow_value(' ').for(:title) }
|
77
|
+
it { should_not allow_value(nil).for(:title) }
|
78
|
+
it { should_not allow_value("Example").for(:title) }
|
79
|
+
it { should_not allow_value("Example Title").for(:title) }
|
80
|
+
it { should_not allow_value("Example 1").for(:title) }
|
81
|
+
it { should_not allow_value("Example1").for(:title) }
|
82
|
+
it { should_not allow_value("Ex-ample").for(:title) }
|
83
|
+
it { should_not allow_value("Ex-ample1").for(:title) }
|
84
|
+
it { should_not allow_value("! \#$%\`|").for(:title) }
|
85
|
+
it { should_not allow_value("<>@[]\`|").for(:title) }
|
86
|
+
|
87
|
+
it { should ensure_valid_alpha_numeric_format_of(:title) }
|
88
|
+
it { should_not ensure_valid_alpha_numeric_format_of(:name) }
|
89
|
+
end
|
90
|
+
|
91
|
+
end
|
@@ -0,0 +1,182 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe AlphaValidator do
|
4
|
+
|
5
|
+
context "has a valid value" do
|
6
|
+
let(:klass) do
|
7
|
+
Class.new do
|
8
|
+
include ActiveModel::Validations
|
9
|
+
attr_accessor :title, :name
|
10
|
+
validates :title, alpha: true
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
subject { klass.new }
|
15
|
+
|
16
|
+
it { should allow_value("Example").for(:title) }
|
17
|
+
it { should allow_value("Example Title").for(:title) }
|
18
|
+
|
19
|
+
it { should_not allow_value('').for(:title) }
|
20
|
+
it { should_not allow_value(' ').for(:title) }
|
21
|
+
it { should_not allow_value(nil).for(:title) }
|
22
|
+
it { should_not allow_value("Example1").for(:title) }
|
23
|
+
it { should_not allow_value("Example 1").for(:title) }
|
24
|
+
it { should_not allow_value("Ex-ample1").for(:title) }
|
25
|
+
it { should_not allow_value("! \#$%\`|").for(:title) }
|
26
|
+
it { should_not allow_value("<>@[]\`|").for(:title) }
|
27
|
+
|
28
|
+
it { should ensure_valid_alpha_format_of(:title) }
|
29
|
+
it { should_not ensure_valid_alpha_format_of(:name) }
|
30
|
+
end
|
31
|
+
|
32
|
+
context "with :strict option has a valid value" do
|
33
|
+
let(:klass) do
|
34
|
+
Class.new do
|
35
|
+
include ActiveModel::Validations
|
36
|
+
attr_accessor :title, :name
|
37
|
+
validates :title, alpha: { strict: true }
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
subject { klass.new }
|
42
|
+
|
43
|
+
it { should allow_value("Example").for(:title) }
|
44
|
+
|
45
|
+
it { should_not allow_value('').for(:title) }
|
46
|
+
it { should_not allow_value(' ').for(:title) }
|
47
|
+
it { should_not allow_value(nil).for(:title) }
|
48
|
+
it { should_not allow_value("Example Title").for(:title) }
|
49
|
+
it { should_not allow_value("Example 1").for(:title) }
|
50
|
+
it { should_not allow_value("Example1").for(:title) }
|
51
|
+
it { should_not allow_value("Ex-ample").for(:title) }
|
52
|
+
it { should_not allow_value("Ex-ample1").for(:title) }
|
53
|
+
it { should_not allow_value("! \#$%\`|").for(:title) }
|
54
|
+
it { should_not allow_value("<>@[]\`|").for(:title) }
|
55
|
+
|
56
|
+
it { should ensure_valid_alpha_format_of(:title) }
|
57
|
+
it { should_not ensure_valid_alpha_format_of(:name) }
|
58
|
+
end
|
59
|
+
|
60
|
+
context "with case: :lower option has a valid value" do
|
61
|
+
let(:klass) do
|
62
|
+
Class.new do
|
63
|
+
include ActiveModel::Validations
|
64
|
+
attr_accessor :title, :name
|
65
|
+
validates :title, alpha: { case: :lower }
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
subject { klass.new }
|
70
|
+
|
71
|
+
it { should allow_value("example").for(:title) }
|
72
|
+
it { should allow_value("example title").for(:title) }
|
73
|
+
|
74
|
+
it { should_not allow_value('').for(:title) }
|
75
|
+
it { should_not allow_value(' ').for(:title) }
|
76
|
+
it { should_not allow_value(nil).for(:title) }
|
77
|
+
it { should_not allow_value("Example").for(:title) }
|
78
|
+
it { should_not allow_value("Example Title").for(:title) }
|
79
|
+
it { should_not allow_value("Example 1").for(:title) }
|
80
|
+
it { should_not allow_value("Example1").for(:title) }
|
81
|
+
it { should_not allow_value("Ex-ample").for(:title) }
|
82
|
+
it { should_not allow_value("Ex-ample1").for(:title) }
|
83
|
+
it { should_not allow_value("! \#$%\`|").for(:title) }
|
84
|
+
it { should_not allow_value("<>@[]\`|").for(:title) }
|
85
|
+
|
86
|
+
it { should ensure_valid_alpha_format_of(:title) }
|
87
|
+
it { should_not ensure_valid_alpha_format_of(:name) }
|
88
|
+
end
|
89
|
+
|
90
|
+
context "with case: :upper option has a valid value" do
|
91
|
+
let(:klass) do
|
92
|
+
Class.new do
|
93
|
+
include ActiveModel::Validations
|
94
|
+
attr_accessor :title, :name
|
95
|
+
validates :title, alpha: { case: :upper }
|
96
|
+
end
|
97
|
+
end
|
98
|
+
|
99
|
+
subject { klass.new }
|
100
|
+
|
101
|
+
it { should allow_value("EXAMPLE").for(:title) }
|
102
|
+
it { should allow_value("EXAMPLE TITLE").for(:title) }
|
103
|
+
|
104
|
+
it { should_not allow_value('').for(:title) }
|
105
|
+
it { should_not allow_value(' ').for(:title) }
|
106
|
+
it { should_not allow_value(nil).for(:title) }
|
107
|
+
it { should_not allow_value("example").for(:title) }
|
108
|
+
it { should_not allow_value("Example").for(:title) }
|
109
|
+
it { should_not allow_value("Example Title").for(:title) }
|
110
|
+
it { should_not allow_value("Example 1").for(:title) }
|
111
|
+
it { should_not allow_value("Example1").for(:title) }
|
112
|
+
it { should_not allow_value("Ex-ample").for(:title) }
|
113
|
+
it { should_not allow_value("Ex-ample1").for(:title) }
|
114
|
+
it { should_not allow_value("! \#$%\`|").for(:title) }
|
115
|
+
it { should_not allow_value("<>@[]\`|").for(:title) }
|
116
|
+
|
117
|
+
it { should ensure_valid_alpha_format_of(:title) }
|
118
|
+
it { should_not ensure_valid_alpha_format_of(:name) }
|
119
|
+
end
|
120
|
+
|
121
|
+
context "with case: :lower and :strict option has a valid value" do
|
122
|
+
let(:klass) do
|
123
|
+
Class.new do
|
124
|
+
include ActiveModel::Validations
|
125
|
+
attr_accessor :title, :name
|
126
|
+
validates :title, alpha: { case: :lower, strict: true }
|
127
|
+
end
|
128
|
+
end
|
129
|
+
|
130
|
+
subject { klass.new }
|
131
|
+
|
132
|
+
it { should allow_value("example").for(:title) }
|
133
|
+
|
134
|
+
it { should_not allow_value('').for(:title) }
|
135
|
+
it { should_not allow_value(' ').for(:title) }
|
136
|
+
it { should_not allow_value(nil).for(:title) }
|
137
|
+
it { should_not allow_value("Example").for(:title) }
|
138
|
+
it { should_not allow_value("Example Title").for(:title) }
|
139
|
+
it { should_not allow_value("Example 1").for(:title) }
|
140
|
+
it { should_not allow_value("Example1").for(:title) }
|
141
|
+
it { should_not allow_value("Ex-ample").for(:title) }
|
142
|
+
it { should_not allow_value("Ex-ample1").for(:title) }
|
143
|
+
it { should_not allow_value("example title").for(:title) }
|
144
|
+
it { should_not allow_value("! \#$%\`|").for(:title) }
|
145
|
+
it { should_not allow_value("<>@[]\`|").for(:title) }
|
146
|
+
|
147
|
+
it { should ensure_valid_alpha_format_of(:title) }
|
148
|
+
it { should_not ensure_valid_alpha_format_of(:name) }
|
149
|
+
end
|
150
|
+
|
151
|
+
context "with case: :upper and :strict option has a valid value" do
|
152
|
+
let(:klass) do
|
153
|
+
Class.new do
|
154
|
+
include ActiveModel::Validations
|
155
|
+
attr_accessor :title, :name
|
156
|
+
validates :title, alpha: { case: :upper, strict: true }
|
157
|
+
end
|
158
|
+
end
|
159
|
+
|
160
|
+
subject { klass.new }
|
161
|
+
|
162
|
+
it { should allow_value("EXAMPLE").for(:title) }
|
163
|
+
|
164
|
+
it { should_not allow_value('').for(:title) }
|
165
|
+
it { should_not allow_value(' ').for(:title) }
|
166
|
+
it { should_not allow_value(nil).for(:title) }
|
167
|
+
it { should_not allow_value("example").for(:title) }
|
168
|
+
it { should_not allow_value("Example").for(:title) }
|
169
|
+
it { should_not allow_value("Example Title").for(:title) }
|
170
|
+
it { should_not allow_value("Example 1").for(:title) }
|
171
|
+
it { should_not allow_value("Example1").for(:title) }
|
172
|
+
it { should_not allow_value("Ex-ample").for(:title) }
|
173
|
+
it { should_not allow_value("Ex-ample1").for(:title) }
|
174
|
+
it { should_not allow_value("EXAMPLE TITLE").for(:title) }
|
175
|
+
it { should_not allow_value("! \#$%\`|").for(:title) }
|
176
|
+
it { should_not allow_value("<>@[]\`|").for(:title) }
|
177
|
+
|
178
|
+
it { should ensure_valid_alpha_format_of(:title) }
|
179
|
+
it { should_not ensure_valid_alpha_format_of(:name) }
|
180
|
+
end
|
181
|
+
|
182
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Base64Validator do
|
4
|
+
|
5
|
+
context "has a valid value" do
|
6
|
+
let(:klass) do
|
7
|
+
Class.new do
|
8
|
+
include ActiveModel::Validations
|
9
|
+
attr_accessor :code, :name
|
10
|
+
validates :code, base64: true
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
subject { klass.new }
|
15
|
+
|
16
|
+
it { should allow_value("YW55IGNhcm5hbCBwbGVhcw==").for(:code) }
|
17
|
+
it { should allow_value("YW55IGNhcm5hbCBwbGVhc3U=").for(:code) }
|
18
|
+
it { should allow_value("YW55IGNhcm5hbCBwbGVhc3Vy").for(:code) }
|
19
|
+
it { should allow_value("YW55IGNhcm5hbCBwbGVhc3VyZQ==").for(:code) }
|
20
|
+
it { should allow_value("YW55IGNhcm5hbCBwbGVhc3VyZS4=").for(:code) }
|
21
|
+
|
22
|
+
it { should_not allow_value('').for(:code) }
|
23
|
+
it { should_not allow_value(' ').for(:code) }
|
24
|
+
it { should_not allow_value(nil).for(:code) }
|
25
|
+
it { should_not allow_value("1a.b2").for(:code) }
|
26
|
+
it { should_not allow_value("1a b2").for(:code) }
|
27
|
+
it { should_not allow_value("1a.b2==").for(:code) }
|
28
|
+
|
29
|
+
it { should ensure_valid_base64_format_of(:code) }
|
30
|
+
it { should_not ensure_valid_base64_format_of(:name) }
|
31
|
+
end
|
32
|
+
|
33
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe BooleanValidator do
|
4
|
+
|
5
|
+
context "has a valid value" do
|
6
|
+
let(:klass) do
|
7
|
+
Class.new do
|
8
|
+
include ActiveModel::Validations
|
9
|
+
attr_accessor :active, :name
|
10
|
+
validates :active, boolean: true
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
subject { klass.new }
|
15
|
+
|
16
|
+
it { should allow_value(true).for(:active) }
|
17
|
+
it { should allow_value(false).for(:active) }
|
18
|
+
it { should allow_value(1).for(:active) }
|
19
|
+
it { should allow_value(0).for(:active) }
|
20
|
+
|
21
|
+
it { should_not allow_value('').for(:active) }
|
22
|
+
it { should_not allow_value(' ').for(:active) }
|
23
|
+
it { should_not allow_value(nil).for(:active) }
|
24
|
+
it { should_not allow_value("true").for(:active) }
|
25
|
+
it { should_not allow_value("false").for(:active) }
|
26
|
+
it { should_not allow_value("1").for(:active) }
|
27
|
+
it { should_not allow_value("0").for(:active) }
|
28
|
+
it { should_not allow_value("! \#$%\`|").for(:active) }
|
29
|
+
it { should_not allow_value("<>@[]\`|").for(:active) }
|
30
|
+
|
31
|
+
it { should ensure_valid_boolean_format_of(:active) }
|
32
|
+
it { should_not ensure_valid_boolean_format_of(:name) }
|
33
|
+
end
|
34
|
+
|
35
|
+
end
|