format_validator 0.0.4 → 0.0.5
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/.travis.yml +1 -0
- data/CHANGELOG +7 -0
- data/README.md +2 -0
- data/lib/format_validator/email_format_validator.rb +1 -1
- data/lib/format_validator/ssn_format_validator.rb +11 -0
- data/lib/format_validator/version.rb +1 -1
- data/lib/format_validator/zip_code_format_validator.rb +9 -1
- data/lib/format_validator.rb +1 -0
- data/lib/locales/en.yml +2 -1
- data/spec/format_validator/ssn_format_validator_spec.rb +34 -0
- data/spec/format_validator/zip_code_format_validator_spec.rb +35 -0
- metadata +6 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 85766a3287dd11e546adc7d1422e7351cc2abff2
|
4
|
+
data.tar.gz: d0f70f024444a0baca1efc91848277aa6b7acbd1
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 47de56e8c03ef94eedccb0224206de5f04ba966bd2aab49b2b60d73d6e45ec9be5da6bb553b817d22762cc98b3c2eb567dd247c63df435ee5e908a203d9cfded
|
7
|
+
data.tar.gz: a710d6418ec8d4a03e46972832e999de93b17ba8b62f550850ada005fee093ef53d607005a7bd8dba92538fbffc47dbcaba84f872f7a257c72358db717044a4e
|
data/.travis.yml
CHANGED
data/CHANGELOG
CHANGED
data/README.md
CHANGED
@@ -19,7 +19,9 @@ validates :email, email_format: true
|
|
19
19
|
validates :url, url_format: true
|
20
20
|
validates :hostname, hostname_format: true
|
21
21
|
validates :zip_code, zip_code_format: true
|
22
|
+
validates :zip_code, zip_code_short_format: true
|
22
23
|
validates :expiration_date, future_date: true
|
24
|
+
validates :ssn, ssn_format: true
|
23
25
|
```
|
24
26
|
|
25
27
|
## Contributing
|
@@ -2,7 +2,7 @@ module ActiveModel
|
|
2
2
|
module Validations
|
3
3
|
class EmailFormatValidator < ActiveModel::EachValidator
|
4
4
|
def validate_each(object, attribute, value)
|
5
|
-
unless value =~
|
5
|
+
unless value =~ /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\z/i
|
6
6
|
object.errors[attribute] << (options[:message] || I18n.t('form.errors.email'))
|
7
7
|
end
|
8
8
|
end
|
@@ -0,0 +1,11 @@
|
|
1
|
+
module ActiveModel
|
2
|
+
module Validations
|
3
|
+
class SsnFormatValidator < ActiveModel::EachValidator
|
4
|
+
def validate_each(object, attribute, value)
|
5
|
+
unless value =~ /\A\d{3}-\d{2}-\d{4}\z/
|
6
|
+
object.errors[attribute] << (options[:message] || I18n.t('form.errors.ssn'))
|
7
|
+
end
|
8
|
+
end
|
9
|
+
end
|
10
|
+
end
|
11
|
+
end
|
@@ -2,7 +2,15 @@ module ActiveModel
|
|
2
2
|
module Validations
|
3
3
|
class ZipCodeFormatValidator < ActiveModel::EachValidator
|
4
4
|
def validate_each(object, attribute, value)
|
5
|
-
unless value =~
|
5
|
+
unless value =~ /\A\d{5}(-\d{4})?\z/
|
6
|
+
object.errors[attribute] << (options[:message] || I18n.t('form.errors.zip_code'))
|
7
|
+
end
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
class ZipCodeShortFormatValidator < ActiveModel::EachValidator
|
12
|
+
def validate_each(object, attribute, value)
|
13
|
+
unless value =~ /\A\d{5}\z/
|
6
14
|
object.errors[attribute] << (options[:message] || I18n.t('form.errors.zip_code'))
|
7
15
|
end
|
8
16
|
end
|
data/lib/format_validator.rb
CHANGED
@@ -4,6 +4,7 @@ require 'format_validator/url_format_validator'
|
|
4
4
|
require 'format_validator/hostname_format_validator'
|
5
5
|
require 'format_validator/zip_code_format_validator'
|
6
6
|
require 'format_validator/future_date_validator'
|
7
|
+
require 'format_validator/ssn_format_validator'
|
7
8
|
|
8
9
|
module FormatValidator
|
9
10
|
I18n.load_path << File.expand_path('../locales/en.yml', __FILE__)
|
data/lib/locales/en.yml
CHANGED
@@ -0,0 +1,34 @@
|
|
1
|
+
require_relative '../spec_helper'
|
2
|
+
|
3
|
+
describe 'SsnFormatValidator' do
|
4
|
+
describe '#validate_each' do
|
5
|
+
let(:klass) do
|
6
|
+
Class.new do
|
7
|
+
include ActiveModel::Validations
|
8
|
+
attr_accessor :ssn
|
9
|
+
validates :ssn, ssn_format: true
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
subject { klass.new }
|
14
|
+
|
15
|
+
describe 'valid' do
|
16
|
+
it 'should not add any error' do
|
17
|
+
subject.ssn = '123-45-6789'
|
18
|
+
subject.valid?.must_equal true
|
19
|
+
subject.errors.messages.must_be_empty
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
describe 'invalid' do
|
24
|
+
['123456789', '123 45 6789', '123', '1234567890'].each do |invalid_ssn|
|
25
|
+
it "should add error when ssn is #{invalid_ssn}" do
|
26
|
+
subject.ssn = invalid_ssn
|
27
|
+
subject.valid?.must_equal false
|
28
|
+
subject.errors.messages.size.must_equal 1
|
29
|
+
subject.errors.messages[:ssn].must_equal ['is invalid']
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
@@ -34,3 +34,38 @@ describe 'ZipCodeFormatValidator' do
|
|
34
34
|
end
|
35
35
|
end
|
36
36
|
end
|
37
|
+
|
38
|
+
describe 'ZipCodeShortFormatValidator' do
|
39
|
+
describe '#validate_each' do
|
40
|
+
let(:klass) do
|
41
|
+
Class.new do
|
42
|
+
include ActiveModel::Validations
|
43
|
+
attr_accessor :zip_code
|
44
|
+
validates :zip_code, zip_code_short_format: true
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
subject { klass.new }
|
49
|
+
|
50
|
+
describe 'valid' do
|
51
|
+
%w[12345].each do |zip|
|
52
|
+
it 'should not add any error' do
|
53
|
+
subject.zip_code = zip
|
54
|
+
subject.valid?.must_equal true
|
55
|
+
subject.errors.messages.must_be_empty
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
describe 'invalid' do
|
61
|
+
['123', '123456', '123 45', '1234X', '12345-1234', '12345-234X', '12345 6789'].each do |invalid_zip_code|
|
62
|
+
it "should add error when zip code is #{invalid_zip_code}" do
|
63
|
+
subject.zip_code = invalid_zip_code
|
64
|
+
subject.valid?.must_equal false
|
65
|
+
subject.errors.messages.size.must_equal 1
|
66
|
+
subject.errors.messages[:zip_code].must_equal ['is invalid']
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|
70
|
+
end
|
71
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: format_validator
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.5
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jamal El Milahi
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2014-03-18 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activemodel
|
@@ -71,6 +71,7 @@ files:
|
|
71
71
|
- lib/format_validator/email_format_validator.rb
|
72
72
|
- lib/format_validator/future_date_validator.rb
|
73
73
|
- lib/format_validator/hostname_format_validator.rb
|
74
|
+
- lib/format_validator/ssn_format_validator.rb
|
74
75
|
- lib/format_validator/url_format_validator.rb
|
75
76
|
- lib/format_validator/version.rb
|
76
77
|
- lib/format_validator/zip_code_format_validator.rb
|
@@ -78,6 +79,7 @@ files:
|
|
78
79
|
- spec/format_validator/email_format_validator_spec.rb
|
79
80
|
- spec/format_validator/future_date_validator_spec.rb
|
80
81
|
- spec/format_validator/hostname_format_validator_spec.rb
|
82
|
+
- spec/format_validator/ssn_format_validator_spec.rb
|
81
83
|
- spec/format_validator/url_format_validator_spec.rb
|
82
84
|
- spec/format_validator/zip_code_format_validator_spec.rb
|
83
85
|
- spec/spec_helper.rb
|
@@ -101,7 +103,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
101
103
|
version: '0'
|
102
104
|
requirements: []
|
103
105
|
rubyforge_project:
|
104
|
-
rubygems_version: 2.0.
|
106
|
+
rubygems_version: 2.0.3
|
105
107
|
signing_key:
|
106
108
|
specification_version: 4
|
107
109
|
summary: format_validator is a gem that adds the missing format validators to Active
|
@@ -110,6 +112,7 @@ test_files:
|
|
110
112
|
- spec/format_validator/email_format_validator_spec.rb
|
111
113
|
- spec/format_validator/future_date_validator_spec.rb
|
112
114
|
- spec/format_validator/hostname_format_validator_spec.rb
|
115
|
+
- spec/format_validator/ssn_format_validator_spec.rb
|
113
116
|
- spec/format_validator/url_format_validator_spec.rb
|
114
117
|
- spec/format_validator/zip_code_format_validator_spec.rb
|
115
118
|
- spec/spec_helper.rb
|