validation_kit 1.0.0 → 1.0.1
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.
- data/lib/validation_kit/{validates_as_email → email_validator}/README +0 -0
- data/lib/validation_kit/{validates_as_email/validates_as_email.rb → email_validator/email_validator.rb} +0 -0
- data/lib/validation_kit/{validates_mixed_case_of → mixed_case_validator}/README +0 -0
- data/lib/validation_kit/{validates_mixed_case_of → mixed_case_validator}/config/locales/en.yml +0 -0
- data/lib/validation_kit/{validates_mixed_case_of → mixed_case_validator}/config/locales/fr.yml +0 -0
- data/lib/validation_kit/{validates_mixed_case_of/validates_mixed_case_of.rb → mixed_case_validator/mixed_case_validator.rb} +3 -3
- data/lib/validation_kit/{validates_as_phone → phone_validator}/README +0 -0
- data/lib/validation_kit/{validates_as_phone/validates_as_phone.rb → phone_validator/phone_validator.rb} +2 -2
- data/lib/validation_kit/{validates_as_postal_code → postal_code_validator}/README +0 -0
- data/lib/validation_kit/postal_code_validator/postal_code_validator.rb +87 -0
- data/lib/validation_kit/version.rb +1 -1
- data/lib/validation_kit.rb +5 -3
- metadata +12 -12
- data/lib/validation_kit/validates_as_postal_code/validates_as_postal_code.rb +0 -89
File without changes
|
File without changes
|
File without changes
|
data/lib/validation_kit/{validates_mixed_case_of → mixed_case_validator}/config/locales/en.yml
RENAMED
File without changes
|
data/lib/validation_kit/{validates_mixed_case_of → mixed_case_validator}/config/locales/fr.yml
RENAMED
File without changes
|
@@ -4,8 +4,8 @@ module ValidationKit
|
|
4
4
|
ALL_LOWERCASE = -1
|
5
5
|
|
6
6
|
def validate_each(record, attribute, value)
|
7
|
-
|
8
|
-
|
7
|
+
return if value.nil?
|
8
|
+
return if value.gsub(/\W/, "").size < 3 # skip very short words
|
9
9
|
error = nil
|
10
10
|
|
11
11
|
if (value.upcase == value)
|
@@ -14,7 +14,7 @@ module ValidationKit
|
|
14
14
|
error = ALL_LOWERCASE
|
15
15
|
end
|
16
16
|
|
17
|
-
|
17
|
+
return if error.nil?
|
18
18
|
|
19
19
|
item_name = I18n.t("activerecord.models.attributes.#{name.underscore}.#{attribute}",
|
20
20
|
:default => nil) or options[:attribute_name] or attribute
|
File without changes
|
@@ -23,9 +23,9 @@ module ValidationKit
|
|
23
23
|
country = false
|
24
24
|
end
|
25
25
|
|
26
|
-
|
26
|
+
return unless country
|
27
27
|
current_regex = regex_for_country(country)
|
28
|
-
|
28
|
+
return unless current_regex
|
29
29
|
|
30
30
|
new_value = value.to_s.gsub(/[^0-9]/, '')
|
31
31
|
new_value ||= ''
|
File without changes
|
@@ -0,0 +1,87 @@
|
|
1
|
+
module ValidationKit
|
2
|
+
class PostalCodeValidator < ActiveModel::EachValidator
|
3
|
+
def postal_code_regex_for_country(country_code)
|
4
|
+
if country_code.blank?
|
5
|
+
nil
|
6
|
+
elsif ["AU", "NZ"].include?(country_code)
|
7
|
+
/\d{4}/
|
8
|
+
elsif ["US"].include?(country_code)
|
9
|
+
/\d{5}(-\d{4})?/
|
10
|
+
elsif ["CA"].include?(country_code)
|
11
|
+
/[ABCEGHJKLMNPRSTVXY]\d[ABCEGHJKLMNPRSTWVXYZ]\d[ABCEGHJKLMNPRSTWVXYZ]\d/
|
12
|
+
else
|
13
|
+
nil
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
def disallowed_characters_for_country(country_code)
|
18
|
+
if country_code.blank?
|
19
|
+
nil
|
20
|
+
elsif ["US", "AU", "NZ"].include?(country_code)
|
21
|
+
/[^0-9]/
|
22
|
+
elsif ["CA", "UK"].include?(country_code)
|
23
|
+
/[^0-9A-Z]/
|
24
|
+
else
|
25
|
+
nil
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
def validate_each(record, attribute, value)
|
30
|
+
if options[:country].is_a?(String)
|
31
|
+
country = options[:country]
|
32
|
+
elsif options[:country].is_a?(Symbol) and record.respond_to?(options[:country])
|
33
|
+
country = record.send(options[:country])
|
34
|
+
elsif record.respond_to?(:country)
|
35
|
+
country = record.send(:country)
|
36
|
+
else
|
37
|
+
country = false
|
38
|
+
end
|
39
|
+
|
40
|
+
return unless country
|
41
|
+
current_regex = postal_code_regex_for_country(country)
|
42
|
+
return unless current_regex
|
43
|
+
disallowed_characters = disallowed_characters_for_country(country)
|
44
|
+
|
45
|
+
new_value = value.nil? ? "" : value.upcase.gsub(disallowed_characters, '')
|
46
|
+
|
47
|
+
unless (options[:allow_blank] && new_value.blank?) || new_value =~ current_regex
|
48
|
+
message = I18n.t("activerecord.errors.models.#{name.underscore}.attributes.#{attribute}.invalid",
|
49
|
+
:default => [:"activerecord.errors.models.#{name.underscore}.invalid",
|
50
|
+
options[:message],
|
51
|
+
:'activerecord.errors.messages.invalid'])
|
52
|
+
record.errors[attribute] << message
|
53
|
+
else
|
54
|
+
record.send(attr_name.to_s + '=',
|
55
|
+
format_as_postal_code(new_value, country, disallowed_characters)
|
56
|
+
) if options[:set]
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
def format_as_postal_code(arg, country_code, disallowed_characters)
|
61
|
+
return nil if (arg.blank? or country_code.blank? or !postal_code_regex_for_country(country_code))
|
62
|
+
|
63
|
+
postal_code = arg.gsub(disallowed_characters, '')
|
64
|
+
|
65
|
+
if ["US"].include?(country_code)
|
66
|
+
digit_count = postal_code.length
|
67
|
+
if digit_count == 5
|
68
|
+
return postal_code
|
69
|
+
elsif digit_count == 9
|
70
|
+
return "%s-%s" % [postal_code[0..4], postal_code[5..8]]
|
71
|
+
else
|
72
|
+
return nil
|
73
|
+
end
|
74
|
+
|
75
|
+
elsif ["AU", "NZ"].include?(country_code)
|
76
|
+
postal_code
|
77
|
+
|
78
|
+
elsif ["CA"].include?(country_code)
|
79
|
+
fsa = postal_code[0..2]
|
80
|
+
lda = postal_code[3..5]
|
81
|
+
|
82
|
+
postal_code = "%s %s" % [fsa, lda]
|
83
|
+
postal_code.upcase
|
84
|
+
end
|
85
|
+
end
|
86
|
+
end
|
87
|
+
end
|
data/lib/validation_kit.rb
CHANGED
@@ -1,8 +1,10 @@
|
|
1
1
|
require "validation_kit/version"
|
2
2
|
|
3
|
-
|
3
|
+
lib_path = File.dirname(__FILE__)
|
4
|
+
validators = Dir[File.join(lib_path, '**', '*_validator.rb')]
|
4
5
|
validators.each do |v|
|
5
6
|
require v
|
7
|
+
validator_class = File.basename(v, '.rb').camelize
|
8
|
+
validator = "ValidationKit::#{validator_class}".constantize
|
9
|
+
ActiveModel::Validations.const_set(validator_class, validator)
|
6
10
|
end
|
7
|
-
|
8
|
-
module ValidationKit; end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: validation_kit
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.1
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -10,7 +10,7 @@ authors:
|
|
10
10
|
autorequire:
|
11
11
|
bindir: bin
|
12
12
|
cert_chain: []
|
13
|
-
date: 2012-03-
|
13
|
+
date: 2012-03-12 00:00:00.000000000 Z
|
14
14
|
dependencies: []
|
15
15
|
description: A collection of various validators for Rails forms
|
16
16
|
email:
|
@@ -24,16 +24,16 @@ files:
|
|
24
24
|
- Gemfile
|
25
25
|
- Rakefile
|
26
26
|
- lib/validation_kit.rb
|
27
|
-
- lib/validation_kit/
|
28
|
-
- lib/validation_kit/
|
29
|
-
- lib/validation_kit/
|
30
|
-
- lib/validation_kit/
|
31
|
-
- lib/validation_kit/
|
32
|
-
- lib/validation_kit/
|
33
|
-
- lib/validation_kit/
|
34
|
-
- lib/validation_kit/
|
35
|
-
- lib/validation_kit/
|
36
|
-
- lib/validation_kit/
|
27
|
+
- lib/validation_kit/email_validator/README
|
28
|
+
- lib/validation_kit/email_validator/email_validator.rb
|
29
|
+
- lib/validation_kit/mixed_case_validator/README
|
30
|
+
- lib/validation_kit/mixed_case_validator/config/locales/en.yml
|
31
|
+
- lib/validation_kit/mixed_case_validator/config/locales/fr.yml
|
32
|
+
- lib/validation_kit/mixed_case_validator/mixed_case_validator.rb
|
33
|
+
- lib/validation_kit/phone_validator/README
|
34
|
+
- lib/validation_kit/phone_validator/phone_validator.rb
|
35
|
+
- lib/validation_kit/postal_code_validator/README
|
36
|
+
- lib/validation_kit/postal_code_validator/postal_code_validator.rb
|
37
37
|
- lib/validation_kit/version.rb
|
38
38
|
- validation_kit.gemspec
|
39
39
|
homepage: https://github.com/turbovote/validation_kit
|
@@ -1,89 +0,0 @@
|
|
1
|
-
module ValidationKit
|
2
|
-
class PostalCodeValidator < ActiveModel::EachValidator
|
3
|
-
|
4
|
-
def postal_code_regex_for_country(country_code)
|
5
|
-
if country_code.blank?
|
6
|
-
nil
|
7
|
-
elsif ["AU", "NZ"].include?(country_code)
|
8
|
-
/\d{4}/
|
9
|
-
elsif ["US"].include?(country_code)
|
10
|
-
/\d{5}(-\d{4})?/
|
11
|
-
elsif ["CA"].include?(country_code)
|
12
|
-
/[ABCEGHJKLMNPRSTVXY]\d[ABCEGHJKLMNPRSTWVXYZ]\d[ABCEGHJKLMNPRSTWVXYZ]\d/
|
13
|
-
else
|
14
|
-
nil
|
15
|
-
end
|
16
|
-
end
|
17
|
-
|
18
|
-
def disallowed_characters_for_country(country_code)
|
19
|
-
if country_code.blank?
|
20
|
-
nil
|
21
|
-
elsif ["US", "AU", "NZ"].include?(country_code)
|
22
|
-
/[^0-9]/
|
23
|
-
elsif ["CA", "UK"].include?(country_code)
|
24
|
-
/[^0-9A-Z]/
|
25
|
-
else
|
26
|
-
nil
|
27
|
-
end
|
28
|
-
end
|
29
|
-
|
30
|
-
def validate_each(record, attribute, value)
|
31
|
-
if options[:country].is_a?(String)
|
32
|
-
country = options[:country]
|
33
|
-
elsif options[:country].is_a?(Symbol) and record.respond_to?(options[:country])
|
34
|
-
country = record.send(options[:country])
|
35
|
-
elsif record.respond_to?(:country)
|
36
|
-
country = record.send(:country)
|
37
|
-
else
|
38
|
-
country = false
|
39
|
-
end
|
40
|
-
|
41
|
-
next unless country
|
42
|
-
current_regex = postal_code_regex_for_country(country)
|
43
|
-
next unless current_regex
|
44
|
-
disallowed_characters = disallowed_characters_for_country(country)
|
45
|
-
|
46
|
-
new_value = value.nil? ? "" : value.upcase.gsub(disallowed_characters, '')
|
47
|
-
|
48
|
-
unless (options[:allow_blank] && new_value.blank?) || new_value =~ current_regex
|
49
|
-
message = I18n.t("activerecord.errors.models.#{name.underscore}.attributes.#{attribute}.invalid",
|
50
|
-
:default => [:"activerecord.errors.models.#{name.underscore}.invalid",
|
51
|
-
options[:message],
|
52
|
-
:'activerecord.errors.messages.invalid'])
|
53
|
-
record.errors[attribute] << message
|
54
|
-
else
|
55
|
-
record.send(attr_name.to_s + '=',
|
56
|
-
format_as_postal_code(new_value, country, disallowed_characters)
|
57
|
-
) if options[:set]
|
58
|
-
end
|
59
|
-
end
|
60
|
-
|
61
|
-
def format_as_postal_code(arg, country_code, disallowed_characters)
|
62
|
-
return nil if (arg.blank? or country_code.blank? or !postal_code_regex_for_country(country_code))
|
63
|
-
|
64
|
-
postal_code = arg.gsub(disallowed_characters, '')
|
65
|
-
|
66
|
-
if ["US"].include?(country_code)
|
67
|
-
digit_count = postal_code.length
|
68
|
-
if digit_count == 5
|
69
|
-
return postal_code
|
70
|
-
elsif digit_count == 9
|
71
|
-
return "%s-%s" % [postal_code[0..4], postal_code[5..8]]
|
72
|
-
else
|
73
|
-
return nil
|
74
|
-
end
|
75
|
-
|
76
|
-
elsif ["AU", "NZ"].include?(country_code)
|
77
|
-
postal_code
|
78
|
-
|
79
|
-
elsif ["CA"].include?(country_code)
|
80
|
-
fsa = postal_code[0..2]
|
81
|
-
lda = postal_code[3..5]
|
82
|
-
|
83
|
-
postal_code = "%s %s" % [fsa, lda]
|
84
|
-
postal_code.upcase
|
85
|
-
end
|
86
|
-
end
|
87
|
-
end
|
88
|
-
end
|
89
|
-
end
|