format_validators 0.0.6 → 0.0.7
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG +4 -0
- data/README.rdoc +2 -1
- data/VERSION +1 -1
- data/app/validators/days_of_week_validator.rb +16 -3
- data/app/validators/florida_counties_validator.rb +22 -1
- data/app/validators/phone_format_validator.rb +9 -0
- data/app/validators/ssn_format_validator.rb +7 -1
- data/app/validators/zipcode_format_validator.rb +4 -0
- data/format_validators.gemspec +2 -2
- metadata +3 -3
data/CHANGELOG
CHANGED
data/README.rdoc
CHANGED
@@ -5,8 +5,9 @@ A consolidation of a few of the most common validations.
|
|
5
5
|
== What do we validate
|
6
6
|
|
7
7
|
* Currency
|
8
|
+
* Days of the Week
|
8
9
|
* Florida Counties (a little too custom but someone requested it so here it is)
|
9
|
-
* Phone
|
10
|
+
* North American Phone Number
|
10
11
|
* Social Security Number
|
11
12
|
* ZIP code
|
12
13
|
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.0.
|
1
|
+
0.0.7
|
@@ -1,10 +1,22 @@
|
|
1
|
+
# Checks validity of values supplied for an object's days of the week attribute
|
2
|
+
#
|
3
|
+
# example usage:
|
4
|
+
# validates :atrribute_to_check, :days_of_week => true
|
1
5
|
class DaysOfWeekValidator < ActiveModel::EachValidator
|
6
|
+
# Compares object's days of week attribute against days_of_week array for validity
|
7
|
+
# @param [BasicRecord] record This is the object needing validation
|
8
|
+
# @param [Symbol] attribute The attribute being validated
|
9
|
+
# @param [String] value The value of the attribute to compare aginst the days_of_week array
|
10
|
+
# @return [Boolean] True or false depending on if the value of the record's attribute is contained within the days_of_week array
|
2
11
|
def validate_each(record, attribute, value)
|
3
12
|
value = value.to_s.downcase
|
4
|
-
record.errors[attribute] << value + " is not a valid day of the week or abbreviation" unless
|
13
|
+
record.errors[attribute] << value + " is not a valid day of the week or abbreviation" unless self.days_of_week.include?(value)
|
5
14
|
end
|
6
|
-
|
7
|
-
|
15
|
+
|
16
|
+
# All available days of the week and their abbreviations to be compared against for validation
|
17
|
+
# @return [Array] Array containing all available abbreviations for days of the week
|
18
|
+
def self.days_of_week
|
19
|
+
["monday",
|
8
20
|
"mon",
|
9
21
|
"m",
|
10
22
|
"tuesday",
|
@@ -27,4 +39,5 @@ class DaysOfWeekValidator < ActiveModel::EachValidator
|
|
27
39
|
"sun",
|
28
40
|
"su"
|
29
41
|
]
|
42
|
+
end
|
30
43
|
end
|
@@ -1,19 +1,40 @@
|
|
1
|
-
#
|
1
|
+
#
|
2
|
+
# Counties use abbreviations and will not validate otherwise
|
3
|
+
#
|
4
|
+
# Note: counties with abbreviations do not account for the full name
|
2
5
|
# example: Saint Lucie must be St. Lucie
|
6
|
+
#
|
3
7
|
class FloridaCountiesValidator < ActiveModel::EachValidator
|
8
|
+
|
9
|
+
#
|
10
|
+
# Checks the value parameter against a list of all Florida Counties
|
11
|
+
# If the value contains the word county, it is considered invalid
|
12
|
+
#
|
13
|
+
# @param [ActiveRecord::Base] record the record containing the value
|
14
|
+
# @param [Symbol] attribute the symbol used to access the value on the record
|
15
|
+
# @param [String] value The value of the attribute being validated
|
16
|
+
#
|
4
17
|
def validate_each record, attribute, value
|
18
|
+
|
19
|
+
# normalize the value for later comparison
|
5
20
|
value = value.to_s.downcase.split(' ').map {|w| w.capitalize }.join(' ')
|
6
21
|
|
22
|
+
# Check the value does not contain the word county
|
7
23
|
if value.include?("County")
|
8
24
|
message = value + ' should not contain the word county.'
|
9
25
|
record.errors[attribute] << (options[:message] || message )
|
26
|
+
|
27
|
+
# strip county to further normalize the string. This allows
|
28
|
+
# the value to still be compared against the COUNTIES list
|
10
29
|
value = value.gsub("County", "").strip
|
11
30
|
end
|
12
31
|
|
13
32
|
message = value + ' is not a county in Florida'
|
33
|
+
# push message into errors if the value is not found in the COUNTIES list
|
14
34
|
record.errors[attribute] << (options[:message] || message ) unless COUNTIES.include? value
|
15
35
|
end
|
16
36
|
|
37
|
+
# List of known florida counties
|
17
38
|
COUNTIES = [
|
18
39
|
"Alachua",
|
19
40
|
"Baker",
|
@@ -16,6 +16,9 @@ class PhoneFormatValidator < ActiveModel::EachValidator
|
|
16
16
|
# checks for non digit characters in the number
|
17
17
|
# checks against regional format validator
|
18
18
|
# adds errors if necessary
|
19
|
+
# @param [ActiveRecord::Base] record The record to validate
|
20
|
+
# @param [Symbol] attribute The field on the record to validate
|
21
|
+
# @param [String] value The value of the attribute
|
19
22
|
def validate_each record, attribute, value
|
20
23
|
region = options[:region] || :north_america
|
21
24
|
|
@@ -27,6 +30,9 @@ class PhoneFormatValidator < ActiveModel::EachValidator
|
|
27
30
|
end
|
28
31
|
end
|
29
32
|
|
33
|
+
# Validates a phone number by region
|
34
|
+
#
|
35
|
+
# Currently only supports the north_america region
|
30
36
|
# @param [Symbol] region The phone number region to check against
|
31
37
|
# @param [String] formatted_value Formatted phone number, guaranteed to be all digits
|
32
38
|
# @return [Boolean] Whether the formatted value passes region validations
|
@@ -38,16 +44,19 @@ class PhoneFormatValidator < ActiveModel::EachValidator
|
|
38
44
|
end
|
39
45
|
end
|
40
46
|
|
47
|
+
# @param [String] input String representation of a phone number with non digit characters removed
|
41
48
|
# @return [Boolean] True if it is seven digits long and the first digit is not 1 or 0
|
42
49
|
def valid_seven_digit?(input)
|
43
50
|
input.length == 7 && input[0] != "0" && input[0] != "1"
|
44
51
|
end
|
45
52
|
|
53
|
+
# @param [String] input String representation of a phone number with non digit characters removed
|
46
54
|
# @return [Boolean] True if it is 10 digits long, first digit not 1 or 0 and the last 7 digits validate
|
47
55
|
def valid_ten_digit?(input)
|
48
56
|
input.length == 10 && input[0] != "0" && input[0] != "1" && valid_seven_digit?(input.slice(-7, 7))
|
49
57
|
end
|
50
58
|
|
59
|
+
# @param [String] input String representation of a phone number with non digit characters removed
|
51
60
|
# @return [Boolean] Same as the ten digit with a 1 in front
|
52
61
|
def valid_eleven_digit?(input)
|
53
62
|
input.length == 11 && input[0] == "1" && valid_ten_digit?(input.slice(-10, 10))
|
@@ -1,8 +1,14 @@
|
|
1
1
|
# Custom Validations for Social Security Number format
|
2
|
-
|
2
|
+
#
|
3
3
|
# e.g. validates :ssn, :ssn_format => true
|
4
4
|
# e.g. validates :ssn, :ssn_format => { :allow_blank => true, :if => :method_returns_true }
|
5
5
|
class SsnFormatValidator < ActiveModel::EachValidator
|
6
|
+
|
7
|
+
# Checks input against a social security regex that matches ###-##-#### with or without dashes
|
8
|
+
#
|
9
|
+
# @param [ActiveRecord::Base] record The record to validate
|
10
|
+
# @param [Symbol] attribute The field on the record to validate
|
11
|
+
# @param [String] value The value of the attribute
|
6
12
|
def validate_each record, attribute, value
|
7
13
|
format = /^\A([\d]{3}\-[\d]{2}\-[\d]{4}|[\d]{9})\Z$/
|
8
14
|
message = attribute.to_s.humanize + ' does not match the format ###-##-####.'
|
@@ -3,6 +3,10 @@
|
|
3
3
|
# e.g. validates :zip, :zipcode_format => true
|
4
4
|
# e.g. validates :zip, :zipcode_format => { :allow_blank => true, :if => :method_returns_true }
|
5
5
|
class ZipcodeFormatValidator < ActiveModel::EachValidator
|
6
|
+
# validates a US zipcode matching 12345, 12345 6789, or 12345-6789
|
7
|
+
# @param [ActiveRecord::Base] record The record to validate
|
8
|
+
# @param [Symbol] attribute The field on the record to validate
|
9
|
+
# @param [String] value The value of the attribute
|
6
10
|
def validate_each record, attribute, value
|
7
11
|
format = /^\A[\d]{5}(?:[-|\s][\d]{4})?\Z$/
|
8
12
|
message = attribute.to_s.humanize + ' doesn\'t match an acceptable format.'
|
data/format_validators.gemspec
CHANGED
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = "format_validators"
|
8
|
-
s.version = "0.0.
|
8
|
+
s.version = "0.0.7"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Jeremiah Hemphill"]
|
12
|
-
s.date = "2012-04-
|
12
|
+
s.date = "2012-04-11"
|
13
13
|
s.description = "Complex format validators"
|
14
14
|
s.email = "jeremiah@cloudspace.com"
|
15
15
|
s.extra_rdoc_files = [
|
metadata
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
name: format_validators
|
3
3
|
version: !ruby/object:Gem::Version
|
4
4
|
prerelease:
|
5
|
-
version: 0.0.
|
5
|
+
version: 0.0.7
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
8
8
|
- Jeremiah Hemphill
|
@@ -10,7 +10,7 @@ autorequire:
|
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
12
|
|
13
|
-
date: 2012-04-
|
13
|
+
date: 2012-04-11 00:00:00 Z
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
16
16
|
name: rails
|
@@ -234,7 +234,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
234
234
|
requirements:
|
235
235
|
- - ">="
|
236
236
|
- !ruby/object:Gem::Version
|
237
|
-
hash:
|
237
|
+
hash: -2572695772213769967
|
238
238
|
segments:
|
239
239
|
- 0
|
240
240
|
version: "0"
|