active_validation 2.6.0 → 3.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.coveralls.yml +1 -1
- data/.gitignore +1 -1
- data/.rspec +1 -1
- data/.travis.yml +1 -1
- data/CODE_OF_CONDUCT.md +1 -1
- data/Gemfile +2 -2
- data/LICENSE.txt +1 -1
- data/README.md +1 -1
- data/Rakefile +1 -1
- data/active_validation.gemspec +14 -15
- data/bin/console +1 -1
- data/bin/rake +6 -6
- data/bin/setup +1 -1
- data/lib/active_validation/matchers/ensure_valid_alpha_format_of.rb +4 -4
- data/lib/active_validation/matchers/ensure_valid_alpha_numeric_format_of.rb +4 -4
- data/lib/active_validation/matchers/ensure_valid_base64_format_of.rb +4 -4
- data/lib/active_validation/matchers/ensure_valid_boolean_format_of.rb +4 -4
- data/lib/active_validation/matchers/ensure_valid_coordinate_format_of.rb +4 -4
- data/lib/active_validation/matchers/ensure_valid_credit_card_format_of.rb +4 -4
- data/lib/active_validation/matchers/ensure_valid_currency_format_of.rb +4 -4
- data/lib/active_validation/matchers/ensure_valid_cusip_format_of.rb +4 -4
- data/lib/active_validation/matchers/ensure_valid_email_format_of.rb +4 -4
- data/lib/active_validation/matchers/ensure_valid_equality_matcher_of.rb +4 -4
- data/lib/active_validation/matchers/ensure_valid_hex_format_of.rb +4 -4
- data/lib/active_validation/matchers/ensure_valid_imei_format_of.rb +4 -4
- data/lib/active_validation/matchers/ensure_valid_ip_format_of.rb +4 -4
- data/lib/active_validation/matchers/ensure_valid_isbn_format_of.rb +4 -4
- data/lib/active_validation/matchers/ensure_valid_isin_format_of.rb +4 -4
- data/lib/active_validation/matchers/ensure_valid_mac_address_format_of.rb +4 -4
- data/lib/active_validation/matchers/ensure_valid_name_format_of.rb +4 -4
- data/lib/active_validation/matchers/ensure_valid_password_format_of.rb +4 -4
- data/lib/active_validation/matchers/ensure_valid_phone_format_of.rb +4 -4
- data/lib/active_validation/matchers/ensure_valid_sedol_format_of.rb +4 -4
- data/lib/active_validation/matchers/ensure_valid_slug_format_of.rb +4 -4
- data/lib/active_validation/matchers/ensure_valid_ssn_format_of.rb +4 -4
- data/lib/active_validation/matchers/ensure_valid_tracking_number_format_of.rb +4 -4
- data/lib/active_validation/matchers/ensure_valid_type_format_of.rb +3 -3
- data/lib/active_validation/matchers/ensure_valid_url_format_of.rb +4 -4
- data/lib/active_validation/matchers/ensure_valid_username_format_of.rb +4 -4
- data/lib/active_validation/matchers/ensure_valid_uuid_format_of.rb +4 -4
- data/lib/active_validation/validators/alpha_numeric_validator.rb +3 -4
- data/lib/active_validation/validators/alpha_validator.rb +3 -4
- data/lib/active_validation/validators/base64_validator.rb +3 -4
- data/lib/active_validation/validators/boolean_validator.rb +4 -4
- data/lib/active_validation/validators/coordinate_validator.rb +11 -12
- data/lib/active_validation/validators/credit_card_validator.rb +31 -39
- data/lib/active_validation/validators/currency_validator.rb +3 -4
- data/lib/active_validation/validators/cusip_validator.rb +7 -9
- data/lib/active_validation/validators/email_validator.rb +5 -5
- data/lib/active_validation/validators/equality_validator.rb +9 -13
- data/lib/active_validation/validators/hex_validator.rb +3 -4
- data/lib/active_validation/validators/imei_validator.rb +6 -8
- data/lib/active_validation/validators/ip_validator.rb +3 -4
- data/lib/active_validation/validators/isbn_validator.rb +8 -8
- data/lib/active_validation/validators/isin_validator.rb +15 -13
- data/lib/active_validation/validators/mac_address_validator.rb +8 -12
- data/lib/active_validation/validators/name_validator.rb +4 -5
- data/lib/active_validation/validators/password_validator.rb +3 -4
- data/lib/active_validation/validators/phone_validator.rb +3 -4
- data/lib/active_validation/validators/sedol_validator.rb +6 -8
- data/lib/active_validation/validators/slug_validator.rb +3 -4
- data/lib/active_validation/validators/ssn_validator.rb +3 -4
- data/lib/active_validation/validators/tracking_number_validator.rb +39 -46
- data/lib/active_validation/validators/type_validator.rb +1 -1
- data/lib/active_validation/validators/url_validator.rb +6 -6
- data/lib/active_validation/validators/username_validator.rb +3 -4
- data/lib/active_validation/validators/uuid_validator.rb +3 -4
- data/lib/active_validation/version.rb +1 -1
- data/lib/active_validation.rb +60 -61
- metadata +3 -17
|
@@ -4,13 +4,13 @@ RSpec::Matchers.define :ensure_valid_name_format_of do |attribute|
|
|
|
4
4
|
model.valid?
|
|
5
5
|
|
|
6
6
|
if model.errors.has_key?(attribute)
|
|
7
|
-
model.errors[attribute].include?(I18n.t(
|
|
7
|
+
model.errors[attribute].include?(I18n.t("active_validation.errors.messages.name"))
|
|
8
8
|
end
|
|
9
9
|
end
|
|
10
10
|
|
|
11
11
|
failure_message do |model|
|
|
12
12
|
I18n.t(
|
|
13
|
-
|
|
13
|
+
"active_validation.errors.matchers.ensure_valid_name_format_of.failure_message_for_should",
|
|
14
14
|
attribute: attribute.inspect,
|
|
15
15
|
model: model.class.name
|
|
16
16
|
)
|
|
@@ -18,9 +18,9 @@ RSpec::Matchers.define :ensure_valid_name_format_of do |attribute|
|
|
|
18
18
|
|
|
19
19
|
failure_message_when_negated do |model|
|
|
20
20
|
I18n.t(
|
|
21
|
-
|
|
21
|
+
"active_validation.errors.matchers.ensure_valid_name_format_of.failure_message_for_should_not",
|
|
22
22
|
attribute: attribute.inspect,
|
|
23
23
|
model: model.class.name
|
|
24
24
|
)
|
|
25
25
|
end
|
|
26
|
-
end
|
|
26
|
+
end
|
|
@@ -4,13 +4,13 @@ RSpec::Matchers.define :ensure_valid_password_format_of do |attribute|
|
|
|
4
4
|
model.valid?
|
|
5
5
|
|
|
6
6
|
if model.errors.has_key?(attribute)
|
|
7
|
-
model.errors[attribute].include?(I18n.t(
|
|
7
|
+
model.errors[attribute].include?(I18n.t("active_validation.errors.messages.password"))
|
|
8
8
|
end
|
|
9
9
|
end
|
|
10
10
|
|
|
11
11
|
failure_message do |model|
|
|
12
12
|
I18n.t(
|
|
13
|
-
|
|
13
|
+
"active_validation.errors.matchers.ensure_valid_password_format_of.failure_message_for_should",
|
|
14
14
|
attr: attribute.inspect,
|
|
15
15
|
model: model.class.name
|
|
16
16
|
)
|
|
@@ -18,9 +18,9 @@ RSpec::Matchers.define :ensure_valid_password_format_of do |attribute|
|
|
|
18
18
|
|
|
19
19
|
failure_message_when_negated do |model|
|
|
20
20
|
I18n.t(
|
|
21
|
-
|
|
21
|
+
"active_validation.errors.matchers.ensure_valid_password_format_of.failure_message_for_should_not",
|
|
22
22
|
attr: attribute.inspect,
|
|
23
23
|
model: model.class.name
|
|
24
24
|
)
|
|
25
25
|
end
|
|
26
|
-
end
|
|
26
|
+
end
|
|
@@ -4,13 +4,13 @@ RSpec::Matchers.define :ensure_valid_phone_format_of do |attribute|
|
|
|
4
4
|
model.valid?
|
|
5
5
|
|
|
6
6
|
if model.errors.has_key?(attribute)
|
|
7
|
-
model.errors[attribute].include?(I18n.t(
|
|
7
|
+
model.errors[attribute].include?(I18n.t("active_validation.errors.messages.phone"))
|
|
8
8
|
end
|
|
9
9
|
end
|
|
10
10
|
|
|
11
11
|
failure_message do |model|
|
|
12
12
|
I18n.t(
|
|
13
|
-
|
|
13
|
+
"active_validation.errors.matchers.ensure_valid_phone_format_of.failure_message_for_should",
|
|
14
14
|
attr: attribute.inspect,
|
|
15
15
|
model: model.class.name
|
|
16
16
|
)
|
|
@@ -18,9 +18,9 @@ RSpec::Matchers.define :ensure_valid_phone_format_of do |attribute|
|
|
|
18
18
|
|
|
19
19
|
failure_message_when_negated do |model|
|
|
20
20
|
I18n.t(
|
|
21
|
-
|
|
21
|
+
"active_validation.errors.matchers.ensure_valid_phone_format_of.failure_message_for_should_not",
|
|
22
22
|
attr: attribute.inspect,
|
|
23
23
|
model: model.class.name
|
|
24
24
|
)
|
|
25
25
|
end
|
|
26
|
-
end
|
|
26
|
+
end
|
|
@@ -4,13 +4,13 @@ RSpec::Matchers.define :ensure_valid_sedol_format_of do |attribute|
|
|
|
4
4
|
model.valid?
|
|
5
5
|
|
|
6
6
|
if model.errors.has_key?(attribute)
|
|
7
|
-
model.errors[attribute].include?(I18n.t(
|
|
7
|
+
model.errors[attribute].include?(I18n.t("active_validation.errors.messages.sedol"))
|
|
8
8
|
end
|
|
9
9
|
end
|
|
10
10
|
|
|
11
11
|
failure_message do |model|
|
|
12
12
|
I18n.t(
|
|
13
|
-
|
|
13
|
+
"active_validation.errors.matchers.ensure_valid_sedol_format_of.failure_message_for_should",
|
|
14
14
|
attr: attribute.inspect,
|
|
15
15
|
model: model.class.name
|
|
16
16
|
)
|
|
@@ -18,9 +18,9 @@ RSpec::Matchers.define :ensure_valid_sedol_format_of do |attribute|
|
|
|
18
18
|
|
|
19
19
|
failure_message_when_negated do |model|
|
|
20
20
|
I18n.t(
|
|
21
|
-
|
|
21
|
+
"active_validation.errors.matchers.ensure_valid_sedol_format_of.failure_message_for_should_not",
|
|
22
22
|
attr: attribute.inspect,
|
|
23
23
|
model: model.class.name
|
|
24
24
|
)
|
|
25
25
|
end
|
|
26
|
-
end
|
|
26
|
+
end
|
|
@@ -4,13 +4,13 @@ RSpec::Matchers.define :ensure_valid_slug_format_of do |attribute|
|
|
|
4
4
|
model.valid?
|
|
5
5
|
|
|
6
6
|
if model.errors.has_key?(attribute)
|
|
7
|
-
model.errors[attribute].include?(I18n.t(
|
|
7
|
+
model.errors[attribute].include?(I18n.t("active_validation.errors.messages.slug"))
|
|
8
8
|
end
|
|
9
9
|
end
|
|
10
10
|
|
|
11
11
|
failure_message do |model|
|
|
12
12
|
I18n.t(
|
|
13
|
-
|
|
13
|
+
"active_validation.errors.matchers.ensure_valid_slug_format_of.failure_message_for_should",
|
|
14
14
|
attr: attribute.inspect,
|
|
15
15
|
model: model.class.name
|
|
16
16
|
)
|
|
@@ -18,9 +18,9 @@ RSpec::Matchers.define :ensure_valid_slug_format_of do |attribute|
|
|
|
18
18
|
|
|
19
19
|
failure_message_when_negated do |model|
|
|
20
20
|
I18n.t(
|
|
21
|
-
|
|
21
|
+
"active_validation.errors.matchers.ensure_valid_slug_format_of.failure_message_for_should_not",
|
|
22
22
|
attr: attribute.inspect,
|
|
23
23
|
model: model.class.name
|
|
24
24
|
)
|
|
25
25
|
end
|
|
26
|
-
end
|
|
26
|
+
end
|
|
@@ -4,13 +4,13 @@ RSpec::Matchers.define :ensure_valid_ssn_format_of do |attribute|
|
|
|
4
4
|
model.valid?
|
|
5
5
|
|
|
6
6
|
if model.errors.has_key?(attribute)
|
|
7
|
-
model.errors[attribute].include?(I18n.t(
|
|
7
|
+
model.errors[attribute].include?(I18n.t("active_validation.errors.messages.ssn"))
|
|
8
8
|
end
|
|
9
9
|
end
|
|
10
10
|
|
|
11
11
|
failure_message do |model|
|
|
12
12
|
I18n.t(
|
|
13
|
-
|
|
13
|
+
"active_validation.errors.matchers.ensure_valid_ssn_format_of.failure_message_for_should",
|
|
14
14
|
attr: attribute.inspect,
|
|
15
15
|
model: model.class.name
|
|
16
16
|
)
|
|
@@ -18,9 +18,9 @@ RSpec::Matchers.define :ensure_valid_ssn_format_of do |attribute|
|
|
|
18
18
|
|
|
19
19
|
failure_message_when_negated do |model|
|
|
20
20
|
I18n.t(
|
|
21
|
-
|
|
21
|
+
"active_validation.errors.matchers.ensure_valid_ssn_format_of.failure_message_for_should_not",
|
|
22
22
|
attr: attribute.inspect,
|
|
23
23
|
model: model.class.name
|
|
24
24
|
)
|
|
25
25
|
end
|
|
26
|
-
end
|
|
26
|
+
end
|
|
@@ -4,13 +4,13 @@ RSpec::Matchers.define :ensure_valid_tracking_number_format_of do |attribute|
|
|
|
4
4
|
model.valid?
|
|
5
5
|
|
|
6
6
|
if model.errors.has_key?(attribute)
|
|
7
|
-
model.errors[attribute].include?(I18n.t(
|
|
7
|
+
model.errors[attribute].include?(I18n.t("active_validation.errors.messages.tracking_number"))
|
|
8
8
|
end
|
|
9
9
|
end
|
|
10
10
|
|
|
11
11
|
failure_message do |model|
|
|
12
12
|
I18n.t(
|
|
13
|
-
|
|
13
|
+
"active_validation.errors.matchers.ensure_valid_tracking_number_format_of.failure_message_for_should",
|
|
14
14
|
attr: attribute.inspect,
|
|
15
15
|
model: model.class.name
|
|
16
16
|
)
|
|
@@ -18,9 +18,9 @@ RSpec::Matchers.define :ensure_valid_tracking_number_format_of do |attribute|
|
|
|
18
18
|
|
|
19
19
|
failure_message_when_negated do |model|
|
|
20
20
|
I18n.t(
|
|
21
|
-
|
|
21
|
+
"active_validation.errors.matchers.ensure_valid_tracking_number_format_of.failure_message_for_should_not",
|
|
22
22
|
attr: attribute.inspect,
|
|
23
23
|
model: model.class.name
|
|
24
24
|
)
|
|
25
25
|
end
|
|
26
|
-
end
|
|
26
|
+
end
|
|
@@ -4,13 +4,13 @@ RSpec::Matchers.define :ensure_valid_type_format_of do |attribute|
|
|
|
4
4
|
model.valid?
|
|
5
5
|
|
|
6
6
|
if model.errors.has_key?(attribute)
|
|
7
|
-
model.errors[attribute].include?(I18n.t(
|
|
7
|
+
model.errors[attribute].include?(I18n.t("active_validation.errors.messages.type"))
|
|
8
8
|
end
|
|
9
9
|
end
|
|
10
10
|
|
|
11
11
|
failure_message do |model|
|
|
12
12
|
I18n.t(
|
|
13
|
-
|
|
13
|
+
"active_validation.errors.matchers.ensure_valid_type_format_of.failure_message_for_should",
|
|
14
14
|
attr: attribute.inspect,
|
|
15
15
|
model: model.class.name
|
|
16
16
|
)
|
|
@@ -18,7 +18,7 @@ RSpec::Matchers.define :ensure_valid_type_format_of do |attribute|
|
|
|
18
18
|
|
|
19
19
|
failure_message_when_negated do |model|
|
|
20
20
|
I18n.t(
|
|
21
|
-
|
|
21
|
+
"active_validation.errors.matchers.ensure_valid_type_format_of.failure_message_for_should_not",
|
|
22
22
|
attr: attribute.inspect,
|
|
23
23
|
model: model.class.name
|
|
24
24
|
)
|
|
@@ -4,13 +4,13 @@ RSpec::Matchers.define :ensure_valid_url_format_of do |attribute|
|
|
|
4
4
|
model.valid?
|
|
5
5
|
|
|
6
6
|
if model.errors.has_key?(attribute)
|
|
7
|
-
model.errors[attribute].include?(I18n.t(
|
|
7
|
+
model.errors[attribute].include?(I18n.t("active_validation.errors.messages.url"))
|
|
8
8
|
end
|
|
9
9
|
end
|
|
10
10
|
|
|
11
11
|
failure_message do |model|
|
|
12
12
|
I18n.t(
|
|
13
|
-
|
|
13
|
+
"active_validation.errors.matchers.ensure_valid_url_format_of.failure_message_for_should",
|
|
14
14
|
attr: attribute.inspect,
|
|
15
15
|
model: model.class.name
|
|
16
16
|
)
|
|
@@ -18,9 +18,9 @@ RSpec::Matchers.define :ensure_valid_url_format_of do |attribute|
|
|
|
18
18
|
|
|
19
19
|
failure_message_when_negated do |model|
|
|
20
20
|
I18n.t(
|
|
21
|
-
|
|
21
|
+
"active_validation.errors.matchers.ensure_valid_url_format_of.failure_message_for_should_not",
|
|
22
22
|
attr: attribute.inspect,
|
|
23
23
|
model: model.class.name
|
|
24
24
|
)
|
|
25
25
|
end
|
|
26
|
-
end
|
|
26
|
+
end
|
|
@@ -4,13 +4,13 @@ RSpec::Matchers.define :ensure_valid_username_format_of do |attribute|
|
|
|
4
4
|
model.valid?
|
|
5
5
|
|
|
6
6
|
if model.errors.has_key?(attribute)
|
|
7
|
-
model.errors[attribute].include?(I18n.t(
|
|
7
|
+
model.errors[attribute].include?(I18n.t("active_validation.errors.messages.username"))
|
|
8
8
|
end
|
|
9
9
|
end
|
|
10
10
|
|
|
11
11
|
failure_message do |model|
|
|
12
12
|
I18n.t(
|
|
13
|
-
|
|
13
|
+
"active_validation.errors.matchers.ensure_valid_username_format_of.failure_message_for_should",
|
|
14
14
|
attr: attribute.inspect,
|
|
15
15
|
model: model.class.name
|
|
16
16
|
)
|
|
@@ -18,9 +18,9 @@ RSpec::Matchers.define :ensure_valid_username_format_of do |attribute|
|
|
|
18
18
|
|
|
19
19
|
failure_message_when_negated do |model|
|
|
20
20
|
I18n.t(
|
|
21
|
-
|
|
21
|
+
"active_validation.errors.matchers.ensure_valid_username_format_of.failure_message_for_should_not",
|
|
22
22
|
attr: attribute.inspect,
|
|
23
23
|
model: model.class.name
|
|
24
24
|
)
|
|
25
25
|
end
|
|
26
|
-
end
|
|
26
|
+
end
|
|
@@ -4,13 +4,13 @@ RSpec::Matchers.define :ensure_valid_uuid_format_of do |attribute|
|
|
|
4
4
|
model.valid?
|
|
5
5
|
|
|
6
6
|
if model.errors.has_key?(attribute)
|
|
7
|
-
model.errors[attribute].include?(I18n.t(
|
|
7
|
+
model.errors[attribute].include?(I18n.t("active_validation.errors.messages.uuid"))
|
|
8
8
|
end
|
|
9
9
|
end
|
|
10
10
|
|
|
11
11
|
failure_message do |model|
|
|
12
12
|
I18n.t(
|
|
13
|
-
|
|
13
|
+
"active_validation.errors.matchers.ensure_valid_uuid_format_of.failure_message_for_should",
|
|
14
14
|
attr: attribute.inspect,
|
|
15
15
|
model: model.class.name
|
|
16
16
|
)
|
|
@@ -18,9 +18,9 @@ RSpec::Matchers.define :ensure_valid_uuid_format_of do |attribute|
|
|
|
18
18
|
|
|
19
19
|
failure_message_when_negated do |model|
|
|
20
20
|
I18n.t(
|
|
21
|
-
|
|
21
|
+
"active_validation.errors.matchers.ensure_valid_uuid_format_of.failure_message_for_should_not",
|
|
22
22
|
attr: attribute.inspect,
|
|
23
23
|
model: model.class.name
|
|
24
24
|
)
|
|
25
25
|
end
|
|
26
|
-
end
|
|
26
|
+
end
|
|
@@ -2,7 +2,7 @@ class AlphaNumericValidator < ActiveModel::EachValidator
|
|
|
2
2
|
|
|
3
3
|
def validate_each(record, attribute, value)
|
|
4
4
|
unless valid?(value.to_s, options)
|
|
5
|
-
record.errors[attribute] <<
|
|
5
|
+
record.errors[attribute] << options.fetch(:message, I18n.t("active_validation.errors.messages.alpha_numeric"))
|
|
6
6
|
end
|
|
7
7
|
end
|
|
8
8
|
|
|
@@ -26,8 +26,7 @@ class AlphaNumericValidator < ActiveModel::EachValidator
|
|
|
26
26
|
end
|
|
27
27
|
|
|
28
28
|
def valid?(value, options)
|
|
29
|
-
valid_length?(value) &&
|
|
30
|
-
valid_format?(value, options)
|
|
29
|
+
valid_length?(value) && valid_format?(value, options)
|
|
31
30
|
end
|
|
32
31
|
|
|
33
|
-
end
|
|
32
|
+
end
|
|
@@ -2,7 +2,7 @@ class AlphaValidator < ActiveModel::EachValidator
|
|
|
2
2
|
|
|
3
3
|
def validate_each(record, attribute, value)
|
|
4
4
|
unless valid?(value.to_s, options)
|
|
5
|
-
record.errors[attribute] <<
|
|
5
|
+
record.errors[attribute] << options.fetch(:message, I18n.t("active_validation.errors.messages.alpha"))
|
|
6
6
|
end
|
|
7
7
|
end
|
|
8
8
|
|
|
@@ -26,8 +26,7 @@ class AlphaValidator < ActiveModel::EachValidator
|
|
|
26
26
|
end
|
|
27
27
|
|
|
28
28
|
def valid?(value, options)
|
|
29
|
-
valid_length?(value) &&
|
|
30
|
-
valid_format?(value, options)
|
|
29
|
+
valid_length?(value) && valid_format?(value, options)
|
|
31
30
|
end
|
|
32
31
|
|
|
33
|
-
end
|
|
32
|
+
end
|
|
@@ -2,7 +2,7 @@ class Base64Validator < ActiveModel::EachValidator
|
|
|
2
2
|
|
|
3
3
|
def validate_each(record, attribute, value)
|
|
4
4
|
unless valid?(value.to_s)
|
|
5
|
-
record.errors[attribute] <<
|
|
5
|
+
record.errors[attribute] << options.fetch(:message, I18n.t("active_validation.errors.messages.base64"))
|
|
6
6
|
end
|
|
7
7
|
end
|
|
8
8
|
|
|
@@ -17,8 +17,7 @@ class Base64Validator < ActiveModel::EachValidator
|
|
|
17
17
|
end
|
|
18
18
|
|
|
19
19
|
def valid?(value)
|
|
20
|
-
valid_length?(value) &&
|
|
21
|
-
valid_format?(value)
|
|
20
|
+
valid_length?(value) && valid_format?(value)
|
|
22
21
|
end
|
|
23
22
|
|
|
24
|
-
end
|
|
23
|
+
end
|
|
@@ -2,13 +2,13 @@ class BooleanValidator < ActiveModel::EachValidator
|
|
|
2
2
|
|
|
3
3
|
def validate_each(record, attribute, value)
|
|
4
4
|
unless TRUE_VALUES.include?(value) || FALSE_VALUES.include?(value)
|
|
5
|
-
record.errors[attribute] <<
|
|
5
|
+
record.errors[attribute] << options.fetch(:message, I18n.t("active_validation.errors.messages.boolean"))
|
|
6
6
|
end
|
|
7
7
|
end
|
|
8
8
|
|
|
9
9
|
private
|
|
10
10
|
|
|
11
|
-
FALSE_VALUES = [false, 0,
|
|
12
|
-
TRUE_VALUES = [true, 1,
|
|
11
|
+
FALSE_VALUES = [false, 0, "0", "f", "F", "false", "FALSE"]
|
|
12
|
+
TRUE_VALUES = [true, 1, "1", "t", "T", "true", "TRUE"]
|
|
13
13
|
|
|
14
|
-
end
|
|
14
|
+
end
|
|
@@ -1,35 +1,35 @@
|
|
|
1
1
|
class CoordinateValidator < ActiveModel::EachValidator
|
|
2
2
|
|
|
3
3
|
def validate_each(record, attribute, value)
|
|
4
|
-
boundary = options.fetch(:boundary, :coordinate)
|
|
4
|
+
boundary = options.fetch(:boundary, :coordinate)
|
|
5
5
|
unless BOUNDARIES.include?(boundary)
|
|
6
6
|
raise ArgumentError,
|
|
7
|
-
"Unknown boundary: #{boundary.inspect}. Valid boundaries are: #{BOUNDARIES.map(&:inspect).join(', '
|
|
7
|
+
"Unknown boundary: #{boundary.inspect}. Valid boundaries are: #{BOUNDARIES.map(&:inspect).join(', ')}"
|
|
8
8
|
end
|
|
9
9
|
|
|
10
10
|
unless valid?(value, options)
|
|
11
|
-
record.errors[attribute] <<
|
|
11
|
+
record.errors[attribute] << options.fetch(:message, I18n.t("active_validation.errors.messages.coordinate.#{boundary}"))
|
|
12
12
|
end
|
|
13
13
|
end
|
|
14
14
|
|
|
15
15
|
private
|
|
16
16
|
|
|
17
|
-
BOUNDARIES = [:coordinate, :latitude, :longitude]
|
|
18
|
-
|
|
19
|
-
def valid_length?(value)
|
|
20
|
-
value.present?
|
|
21
|
-
end
|
|
17
|
+
BOUNDARIES = [:coordinate, :latitude, :longitude]
|
|
22
18
|
|
|
23
19
|
def valid_latitude?(value)
|
|
24
20
|
value >= -90 && value <= 90
|
|
25
21
|
end
|
|
26
22
|
|
|
23
|
+
def valid_length?(value)
|
|
24
|
+
value.present?
|
|
25
|
+
end
|
|
26
|
+
|
|
27
27
|
def valid_longitude?(value)
|
|
28
28
|
value >= -180 && value <= 180
|
|
29
29
|
end
|
|
30
30
|
|
|
31
31
|
def valid_boundary?(value, options)
|
|
32
|
-
case options.fetch(:boundary, :coordinate)
|
|
32
|
+
case options.fetch(:boundary, :coordinate)
|
|
33
33
|
when :latitude
|
|
34
34
|
valid_latitude?(value)
|
|
35
35
|
when :longitude
|
|
@@ -40,8 +40,7 @@ class CoordinateValidator < ActiveModel::EachValidator
|
|
|
40
40
|
end
|
|
41
41
|
|
|
42
42
|
def valid?(value, options)
|
|
43
|
-
valid_length?(value) &&
|
|
44
|
-
valid_boundary?(value, options)
|
|
43
|
+
valid_length?(value) && valid_boundary?(value, options)
|
|
45
44
|
end
|
|
46
45
|
|
|
47
|
-
end
|
|
46
|
+
end
|
|
@@ -2,52 +2,42 @@ class CreditCardValidator < ActiveModel::EachValidator
|
|
|
2
2
|
|
|
3
3
|
def validate_each(record, attribute, value)
|
|
4
4
|
unless valid?(value.to_s, options)
|
|
5
|
-
record.errors[attribute] <<
|
|
5
|
+
record.errors[attribute] << options.fetch(:message, I18n.t("active_validation.errors.messages.credit_card"))
|
|
6
6
|
end
|
|
7
7
|
end
|
|
8
8
|
|
|
9
9
|
private
|
|
10
10
|
|
|
11
11
|
DEFAULT_LENGTHS = {
|
|
12
|
-
american_express: [15],
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
laser: [16, 17, 18, 19],
|
|
17
|
-
maestro: [12, 13, 14, 15, 16, 17, 18, 19],
|
|
18
|
-
mastercard: [16],
|
|
19
|
-
solo: [16, 18, 19],
|
|
20
|
-
unionpay: [16, 17, 18, 19],
|
|
21
|
-
visa: [16]
|
|
22
|
-
}.freeze
|
|
12
|
+
american_express: [15], diners_club: [14, 16], discover: [16], jcb: [16],
|
|
13
|
+
laser: [16, 17, 18, 19], maestro: [12, 13, 14, 15, 16, 17, 18, 19],
|
|
14
|
+
mastercard: [16], solo: [16, 18, 19], unionpay: [16, 17, 18, 19], visa: [16]
|
|
15
|
+
}
|
|
23
16
|
|
|
24
17
|
DEFAULT_PREFIXES = {
|
|
25
|
-
american_express: [
|
|
26
|
-
diners_club: [
|
|
18
|
+
american_express: ["34", "37"],
|
|
19
|
+
diners_club: ["300", "301", "302", "303", "304", "305", "36", "54", "55"],
|
|
27
20
|
discover: [
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
'622924', '622925', '644', '645', '646', '647', '648',
|
|
33
|
-
'649', '65'
|
|
21
|
+
"6011", "622126", "622127", "622128", "622129", "62213", "62214", "62215",
|
|
22
|
+
"62216", "62217", "62218", "62219", "6222", "6223", "6224", "6225", "6226",
|
|
23
|
+
"6227", "6228", "62290", "62291", "622920", "622921", "622922", "622923",
|
|
24
|
+
"622924", "622925", "644", "645", "646", "647", "648", "649", "65"
|
|
34
25
|
],
|
|
35
|
-
jcb: [
|
|
36
|
-
laser: [
|
|
26
|
+
jcb: ["3528", "3529", "353", "354", "355", "356", "357", "358"],
|
|
27
|
+
laser: ["6304", "6706", "6771", "6709"],
|
|
37
28
|
maestro: [
|
|
38
|
-
|
|
39
|
-
|
|
29
|
+
"5018", "5020", "5038", "6304", "6759", "6761", "6762", "6763", "6764",
|
|
30
|
+
"6765", "6766"
|
|
40
31
|
],
|
|
41
|
-
mastercard: [
|
|
42
|
-
solo: [
|
|
32
|
+
mastercard: ["51", "52", "53", "54", "55"],
|
|
33
|
+
solo: ["6334", "6767"],
|
|
43
34
|
unionpay: [
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
'622920', '622921', '622922', '622923', '622924', '622925'
|
|
35
|
+
"622126", "622127", "622128", "622129", "62213", "62214", "62215", "62216",
|
|
36
|
+
"62217", "62218", "62219", "6222", "6223", "6224", "6225", "6226", "6227", "6228",
|
|
37
|
+
"62290", "62291", "622920", "622921", "622922", "622923", "622924", "622925"
|
|
48
38
|
],
|
|
49
|
-
visa: [
|
|
50
|
-
}
|
|
39
|
+
visa: ["4"]
|
|
40
|
+
}
|
|
51
41
|
|
|
52
42
|
def valid_format?(value, options)
|
|
53
43
|
value =~ (options.fetch(:strict, false) ? /^[0-9]+$/ : /^[0-9 -.]+$/)
|
|
@@ -56,8 +46,8 @@ class CreditCardValidator < ActiveModel::EachValidator
|
|
|
56
46
|
def valid_length?(value, options)
|
|
57
47
|
return(false) unless value.present?
|
|
58
48
|
|
|
59
|
-
current_card = options.fetch(:card, :all)
|
|
60
|
-
value_size
|
|
49
|
+
current_card = options.fetch(:card, :all)
|
|
50
|
+
value_size = value.size
|
|
61
51
|
|
|
62
52
|
case current_card
|
|
63
53
|
when :amex
|
|
@@ -71,15 +61,17 @@ class CreditCardValidator < ActiveModel::EachValidator
|
|
|
71
61
|
end
|
|
72
62
|
|
|
73
63
|
def valid_prefix?(value, options)
|
|
74
|
-
current_card = options.fetch(:card, :all)
|
|
64
|
+
current_card = options.fetch(:card, :all)
|
|
75
65
|
|
|
76
66
|
case current_card
|
|
77
67
|
when :amex
|
|
78
68
|
DEFAULT_PREFIXES.fetch(:american_express).any? { |p| value.start_with?(p) }
|
|
79
69
|
when :all
|
|
80
70
|
result = false
|
|
81
|
-
DEFAULT_LENGTHS.
|
|
82
|
-
|
|
71
|
+
DEFAULT_LENGTHS.each do |key, values|
|
|
72
|
+
if values.include?(value.size)
|
|
73
|
+
break if result = DEFAULT_PREFIXES.fetch(key).any? { |p| value.start_with?(p) }
|
|
74
|
+
end
|
|
83
75
|
end
|
|
84
76
|
result
|
|
85
77
|
else
|
|
@@ -88,11 +80,11 @@ class CreditCardValidator < ActiveModel::EachValidator
|
|
|
88
80
|
end
|
|
89
81
|
|
|
90
82
|
def valid?(value, options)
|
|
91
|
-
striped_value = value.gsub(/\D/,
|
|
83
|
+
striped_value = value.gsub(/\D/, "")
|
|
92
84
|
|
|
93
85
|
valid_format?(value, options) &&
|
|
94
86
|
valid_length?(striped_value, options) &&
|
|
95
87
|
valid_prefix?(striped_value, options)
|
|
96
88
|
end
|
|
97
89
|
|
|
98
|
-
end
|
|
90
|
+
end
|
|
@@ -2,7 +2,7 @@ class CurrencyValidator < ActiveModel::EachValidator
|
|
|
2
2
|
|
|
3
3
|
def validate_each(record, attribute, value)
|
|
4
4
|
unless valid?(value.to_s, options)
|
|
5
|
-
record.errors[attribute] <<
|
|
5
|
+
record.errors[attribute] << options.fetch(:message, I18n.t("active_validation.errors.messages.currency"))
|
|
6
6
|
end
|
|
7
7
|
end
|
|
8
8
|
|
|
@@ -17,8 +17,7 @@ class CurrencyValidator < ActiveModel::EachValidator
|
|
|
17
17
|
end
|
|
18
18
|
|
|
19
19
|
def valid?(value, options)
|
|
20
|
-
valid_length?(value) &&
|
|
21
|
-
valid_format?(value, options)
|
|
20
|
+
valid_length?(value) && valid_format?(value, options)
|
|
22
21
|
end
|
|
23
22
|
|
|
24
|
-
end
|
|
23
|
+
end
|
|
@@ -2,18 +2,18 @@ class CusipValidator < ActiveModel::EachValidator
|
|
|
2
2
|
|
|
3
3
|
def validate_each(record, attribute, value)
|
|
4
4
|
unless valid?(value.to_s)
|
|
5
|
-
record.errors[attribute] <<
|
|
5
|
+
record.errors[attribute] << options.fetch(:message, I18n.t("active_validation.errors.messages.cusip"))
|
|
6
6
|
end
|
|
7
7
|
end
|
|
8
8
|
|
|
9
9
|
private
|
|
10
10
|
|
|
11
11
|
def valid_checksum?(value)
|
|
12
|
-
digits
|
|
12
|
+
digits = value.chars.map { |c| c.match(/[A-Z]/) ? (c.ord - 55) : c.to_i }
|
|
13
13
|
even_values = digits.values_at(* digits.each_index.select { |i| i.even? })
|
|
14
|
-
odd_values
|
|
15
|
-
values
|
|
16
|
-
values
|
|
14
|
+
odd_values = digits.values_at(* digits.each_index.select { |i| i.odd? })
|
|
15
|
+
values = odd_values.map { |i| i * 2 }.zip(even_values).flatten
|
|
16
|
+
values = values.inject(0) { |s, i| s += (i / 10) + i % 10 }
|
|
17
17
|
|
|
18
18
|
((10 - values) % 10) % 10
|
|
19
19
|
end
|
|
@@ -27,9 +27,7 @@ class CusipValidator < ActiveModel::EachValidator
|
|
|
27
27
|
end
|
|
28
28
|
|
|
29
29
|
def valid?(value)
|
|
30
|
-
valid_length?(value) &&
|
|
31
|
-
valid_format?(value) &&
|
|
32
|
-
valid_checksum?(value)
|
|
30
|
+
valid_length?(value) && valid_format?(value) && valid_checksum?(value)
|
|
33
31
|
end
|
|
34
32
|
|
|
35
|
-
end
|
|
33
|
+
end
|