mask_validator 0.1 → 0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +1 -0
- data/.rspec +1 -0
- data/Gemfile +5 -1
- data/README.markdown +3 -1
- data/Rakefile +1 -10
- data/lib/mask_validator.rb +44 -13
- data/mask_validator.gemspec +2 -2
- data/spec/mask_validator_spec.rb +160 -0
- data/spec/models/person.rb +11 -0
- data/spec/spec_helper.rb +7 -0
- data/spec/support/db/schema.rb +20 -0
- data/spec/support/models/person.rb +11 -0
- metadata +31 -12
- data/test/mask_validator_test.rb +0 -50
- data/test/test_helper.rb +0 -12
data/.gitignore
CHANGED
data/.rspec
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--color
|
data/Gemfile
CHANGED
data/README.markdown
CHANGED
@@ -2,7 +2,9 @@
|
|
2
2
|
|
3
3
|
This gem was inspired in the Sobrinho's gems to validate simple things inside of ActiveModel.
|
4
4
|
|
5
|
-
Use mask in inputs and validate
|
5
|
+
Use mask in inputs and validate in the models.
|
6
|
+
|
7
|
+
The gem works getting the value before type cast and comparing with a regexp from the mask pattern.
|
6
8
|
|
7
9
|
## Installation
|
8
10
|
|
data/Rakefile
CHANGED
data/lib/mask_validator.rb
CHANGED
@@ -2,27 +2,58 @@ class MaskValidator < ActiveModel::EachValidator
|
|
2
2
|
|
3
3
|
#
|
4
4
|
# This validator use the characters 'a', '9' and '*'
|
5
|
-
# to validate the
|
5
|
+
# to validate the format with regular expression
|
6
6
|
#
|
7
7
|
# Example using in models:
|
8
|
-
# validates :phone, mask
|
9
|
-
# validates :acronym, mask
|
8
|
+
# validates :phone, :mask => "(99) 9999-9999"
|
9
|
+
# validates :acronym, :mask => "***"
|
10
10
|
#
|
11
11
|
# Where:
|
12
|
-
# a -
|
13
|
-
# 9 -
|
14
|
-
# * -
|
12
|
+
# a - to letters (A-Z, a-z)
|
13
|
+
# 9 - to numbers (0-9)
|
14
|
+
# * - to alphanumerics (A-Z, a-z, 0-9)
|
15
15
|
#
|
16
|
-
# TODO: refactoring
|
17
16
|
def validate_each(record, attribute, value)
|
18
|
-
|
17
|
+
value = record.send("#{attribute.to_s}_before_type_cast")
|
19
18
|
|
20
|
-
|
21
|
-
|
19
|
+
if value.nil?
|
20
|
+
record.errors.add(attribute, :blank) unless allow_nil?
|
21
|
+
end
|
22
22
|
|
23
|
-
|
24
|
-
|
25
|
-
|
23
|
+
if value.blank?
|
24
|
+
record.errors.add(attribute, :empty) unless allow_blank?
|
25
|
+
else
|
26
|
+
record.errors.add(attribute, message, options) unless value.match(regexp)
|
26
27
|
end
|
27
28
|
end
|
29
|
+
|
30
|
+
#
|
31
|
+
# Transform the string in a regular expression:
|
32
|
+
#
|
33
|
+
# options[:with] = "9a"
|
34
|
+
#
|
35
|
+
# regexp #=> /[0-9][a-zA-Z]/
|
36
|
+
#
|
37
|
+
# TODO: improve this
|
38
|
+
def regexp
|
39
|
+
/\A#{(options[:with].to_s.each_char.collect { |char| character_map[char] || "\\#{char}" }).join}\z/
|
40
|
+
end
|
41
|
+
|
42
|
+
def character_map
|
43
|
+
{ "9" => "[0-9]", "a" => "[a-zA-Z]", "*" => "[a-zA-Z0-9]" }
|
44
|
+
end
|
45
|
+
|
46
|
+
private
|
47
|
+
|
48
|
+
def message
|
49
|
+
options[:message]
|
50
|
+
end
|
51
|
+
|
52
|
+
def allow_nil?
|
53
|
+
options.include?(:allow_nil) && options[:allow_nil] == false ? false : true
|
54
|
+
end
|
55
|
+
|
56
|
+
def allow_blank?
|
57
|
+
options.include?(:allow_blank) && options[:allow_blank] == false ? false : true
|
58
|
+
end
|
28
59
|
end
|
data/mask_validator.gemspec
CHANGED
@@ -3,7 +3,7 @@ $:.push File.expand_path("../lib", __FILE__)
|
|
3
3
|
|
4
4
|
Gem::Specification.new do |s|
|
5
5
|
s.name = "mask_validator"
|
6
|
-
s.version = "0.
|
6
|
+
s.version = "0.2"
|
7
7
|
s.platform = Gem::Platform::RUBY
|
8
8
|
s.authors = ["Marcelo Cajueiro"]
|
9
9
|
s.email = ["marcelocajueiro@gmail.com"]
|
@@ -18,6 +18,6 @@ Gem::Specification.new do |s|
|
|
18
18
|
s.require_paths = ["lib"]
|
19
19
|
|
20
20
|
s.add_dependency 'activemodel', ">= 3.0"
|
21
|
+
s.add_dependency 'activerecord', ">= 3.0"
|
21
22
|
s.add_development_dependency 'rake', '>= 0.8.7'
|
22
|
-
|
23
23
|
end
|
@@ -0,0 +1,160 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe MaskValidator do
|
4
|
+
subject do
|
5
|
+
Person.new :phone => '(12) 3456-7890',
|
6
|
+
:fax => '(12) 3456-7890',
|
7
|
+
:acronym => 'ABC',
|
8
|
+
:alphanumeric => 'AAA666',
|
9
|
+
:zip_code => '88900-000',
|
10
|
+
:birth_date => '13/10/1989',
|
11
|
+
:birth_time => '10:20',
|
12
|
+
:birth_year => '2011',
|
13
|
+
:body_fat => '23,44'
|
14
|
+
end
|
15
|
+
|
16
|
+
it "Person should be valid" do
|
17
|
+
subject.should be_valid
|
18
|
+
end
|
19
|
+
|
20
|
+
# validates :phone, :mask => "(99) 9999-9999", :allow_blank => true
|
21
|
+
context "mask validation to phone with explicit ':allow_blank => true'" do
|
22
|
+
it "should be valid with a nil phone" do
|
23
|
+
subject.phone = nil
|
24
|
+
subject.should be_valid
|
25
|
+
end
|
26
|
+
|
27
|
+
it "should be valid with an empty phone" do
|
28
|
+
subject.phone = ''
|
29
|
+
subject.should be_valid
|
30
|
+
end
|
31
|
+
|
32
|
+
it "should be valid with a phone in according of the mask" do
|
33
|
+
subject.phone = '(48) 9874-4569'
|
34
|
+
subject.should be_valid
|
35
|
+
end
|
36
|
+
|
37
|
+
it "should not be valid with a phone with a wrong pattern" do
|
38
|
+
subject.phone = '4852458787'
|
39
|
+
subject.should_not be_valid
|
40
|
+
|
41
|
+
subject.phone = '48 9865 4879'
|
42
|
+
subject.should_not be_valid
|
43
|
+
|
44
|
+
subject.phone = '(48) 9874-45169'
|
45
|
+
subject.should_not be_valid
|
46
|
+
|
47
|
+
subject.phone = '(48)98956698'
|
48
|
+
subject.should_not be_valid
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
# validates :acronym, :mask => "***", :allow_nil => true
|
53
|
+
context "mask validation to acronym with explicit ':allow_nil => true'" do
|
54
|
+
it "should be valid with an nil acronym" do
|
55
|
+
subject.acronym = nil
|
56
|
+
subject.should be_valid
|
57
|
+
end
|
58
|
+
|
59
|
+
it "should be valid with an empty acronym" do
|
60
|
+
subject.acronym = ''
|
61
|
+
subject.should be_valid
|
62
|
+
end
|
63
|
+
|
64
|
+
it "should be valid with a acronym in according of the mask" do
|
65
|
+
subject.acronym = '1gd'
|
66
|
+
subject.should be_valid
|
67
|
+
|
68
|
+
subject.acronym = '666'
|
69
|
+
subject.should be_valid
|
70
|
+
|
71
|
+
subject.acronym = 'zzz'
|
72
|
+
subject.should be_valid
|
73
|
+
end
|
74
|
+
|
75
|
+
it "should not be valid with a acronym with a wrong pattern" do
|
76
|
+
subject.acronym = '1qw1'
|
77
|
+
subject.should_not be_valid
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
81
|
+
# validates :alphanumeric, :mask => "aaa999"
|
82
|
+
context "mask validation to alphanumeric with implicit ':allow_nil => true' and ':allow_blank => true'" do
|
83
|
+
it "should be valid with an nil alphanumeric" do
|
84
|
+
subject.alphanumeric = nil
|
85
|
+
subject.should be_valid
|
86
|
+
end
|
87
|
+
|
88
|
+
it "should be valid with an empty alphanumeric" do
|
89
|
+
subject.alphanumeric = ''
|
90
|
+
subject.should be_valid
|
91
|
+
end
|
92
|
+
|
93
|
+
it "should be valid with a alphanumeric in according of the mask" do
|
94
|
+
subject.alphanumeric = 'awe987'
|
95
|
+
subject.should be_valid
|
96
|
+
end
|
97
|
+
|
98
|
+
it "should not be valid with a alphanumeric with a wrong pattern" do
|
99
|
+
subject.alphanumeric = '999999'
|
100
|
+
subject.should_not be_valid
|
101
|
+
|
102
|
+
subject.alphanumeric = 'QQQQQQ'
|
103
|
+
subject.should_not be_valid
|
104
|
+
|
105
|
+
subject.alphanumeric = '666aaaa'
|
106
|
+
subject.should_not be_valid
|
107
|
+
|
108
|
+
subject.alphanumeric = '666AAA'
|
109
|
+
subject.should_not be_valid
|
110
|
+
end
|
111
|
+
end
|
112
|
+
|
113
|
+
# validates :zip_code, :mask => "99999-999", :allow_blank => false
|
114
|
+
context "mask validation to zip code with explicit ':allow_blank => false'" do
|
115
|
+
it "should not be valid with an nil zip code" do
|
116
|
+
subject.zip_code = nil
|
117
|
+
subject.should_not be_valid
|
118
|
+
end
|
119
|
+
|
120
|
+
it "should not be valid with an empty zip code" do
|
121
|
+
subject.zip_code = ''
|
122
|
+
subject.should_not be_valid
|
123
|
+
end
|
124
|
+
end
|
125
|
+
|
126
|
+
# validates :fax, :mask => "(99) 9999-9999", :allow_nil => false
|
127
|
+
context "mask validation to fax with explicit ':allow_nil => false'" do
|
128
|
+
it "should not be valid with an nil fax" do
|
129
|
+
subject.fax = nil
|
130
|
+
subject.should_not be_valid
|
131
|
+
end
|
132
|
+
|
133
|
+
it "should be valid with an empty fax" do
|
134
|
+
subject.fax = ''
|
135
|
+
subject.should be_valid
|
136
|
+
end
|
137
|
+
end
|
138
|
+
|
139
|
+
context "when the attributes type is not a string" do
|
140
|
+
it "should not be valid with a wrong date format" do
|
141
|
+
subject.birth_date = "15-12-2009"
|
142
|
+
subject.should be_invalid
|
143
|
+
end
|
144
|
+
|
145
|
+
it "should not be valid with a wrong birth year" do
|
146
|
+
subject.birth_year = "20110"
|
147
|
+
subject.should be_invalid
|
148
|
+
end
|
149
|
+
|
150
|
+
it "should not be valid with a wrong birth time" do
|
151
|
+
subject.birth_time = "333:20"
|
152
|
+
subject.should be_invalid
|
153
|
+
end
|
154
|
+
|
155
|
+
it "should not be valid with a wrong body fat" do
|
156
|
+
subject.birth_time = "333,00%"
|
157
|
+
subject.should be_invalid
|
158
|
+
end
|
159
|
+
end
|
160
|
+
end
|
@@ -0,0 +1,11 @@
|
|
1
|
+
class Person < ActiveRecord::Base
|
2
|
+
validates :phone, :mask => "(99) 9999-9999", :allow_blank => true
|
3
|
+
validates :fax, :mask => "(99) 9999-9999", :allow_nil => false
|
4
|
+
validates :acronym, :mask => "***", :allow_nil => true
|
5
|
+
validates :alphanumeric, :mask => "aaa999"
|
6
|
+
validates :zip_code, :mask => "99999-999", :allow_blank => false
|
7
|
+
validates :birth_date, :mask => '99/99/9999'
|
8
|
+
validates :birth_time, :mask => '99:99'
|
9
|
+
validates :birth_year, :mask => '9999'
|
10
|
+
validates :body_fat, :mask => "99,99"
|
11
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
ActiveRecord::Base.establish_connection(
|
2
|
+
:adapter => "sqlite3",
|
3
|
+
:database => ":memory:"
|
4
|
+
)
|
5
|
+
|
6
|
+
ActiveRecord::Schema.define do
|
7
|
+
self.verbose = false
|
8
|
+
|
9
|
+
create_table :people do |t|
|
10
|
+
t.string :phone
|
11
|
+
t.string :acronym
|
12
|
+
t.string :alphanumeric
|
13
|
+
t.string :zip_code
|
14
|
+
t.string :fax
|
15
|
+
t.decimal :body_fat
|
16
|
+
t.integer :birth_year
|
17
|
+
t.date :birth_date
|
18
|
+
t.datetime :birth_time
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,11 @@
|
|
1
|
+
class Person < ActiveRecord::Base
|
2
|
+
validates :phone, :mask => "(99) 9999-9999", :allow_blank => true
|
3
|
+
validates :fax, :mask => "(99) 9999-9999", :allow_nil => false
|
4
|
+
validates :acronym, :mask => "***", :allow_nil => true
|
5
|
+
validates :alphanumeric, :mask => "aaa999"
|
6
|
+
validates :zip_code, :mask => "99999-999", :allow_blank => false
|
7
|
+
validates :birth_date, :mask => '99/99/9999'
|
8
|
+
validates :birth_time, :mask => '99:99'
|
9
|
+
validates :birth_year, :mask => '9999'
|
10
|
+
validates :body_fat, :mask => "99,99"
|
11
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: mask_validator
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: '0.
|
4
|
+
version: '0.2'
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,12 +9,11 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date:
|
13
|
-
default_executable:
|
12
|
+
date: 2012-01-20 00:00:00.000000000 Z
|
14
13
|
dependencies:
|
15
14
|
- !ruby/object:Gem::Dependency
|
16
15
|
name: activemodel
|
17
|
-
requirement: &
|
16
|
+
requirement: &70361744263680 !ruby/object:Gem::Requirement
|
18
17
|
none: false
|
19
18
|
requirements:
|
20
19
|
- - ! '>='
|
@@ -22,10 +21,21 @@ dependencies:
|
|
22
21
|
version: '3.0'
|
23
22
|
type: :runtime
|
24
23
|
prerelease: false
|
25
|
-
version_requirements: *
|
24
|
+
version_requirements: *70361744263680
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: activerecord
|
27
|
+
requirement: &70361744262500 !ruby/object:Gem::Requirement
|
28
|
+
none: false
|
29
|
+
requirements:
|
30
|
+
- - ! '>='
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '3.0'
|
33
|
+
type: :runtime
|
34
|
+
prerelease: false
|
35
|
+
version_requirements: *70361744262500
|
26
36
|
- !ruby/object:Gem::Dependency
|
27
37
|
name: rake
|
28
|
-
requirement: &
|
38
|
+
requirement: &70361744260860 !ruby/object:Gem::Requirement
|
29
39
|
none: false
|
30
40
|
requirements:
|
31
41
|
- - ! '>='
|
@@ -33,7 +43,7 @@ dependencies:
|
|
33
43
|
version: 0.8.7
|
34
44
|
type: :development
|
35
45
|
prerelease: false
|
36
|
-
version_requirements: *
|
46
|
+
version_requirements: *70361744260860
|
37
47
|
description:
|
38
48
|
email:
|
39
49
|
- marcelocajueiro@gmail.com
|
@@ -42,15 +52,18 @@ extensions: []
|
|
42
52
|
extra_rdoc_files: []
|
43
53
|
files:
|
44
54
|
- .gitignore
|
55
|
+
- .rspec
|
45
56
|
- Gemfile
|
46
57
|
- MIT-LICENSE
|
47
58
|
- README.markdown
|
48
59
|
- Rakefile
|
49
60
|
- lib/mask_validator.rb
|
50
61
|
- mask_validator.gemspec
|
51
|
-
-
|
52
|
-
-
|
53
|
-
|
62
|
+
- spec/mask_validator_spec.rb
|
63
|
+
- spec/models/person.rb
|
64
|
+
- spec/spec_helper.rb
|
65
|
+
- spec/support/db/schema.rb
|
66
|
+
- spec/support/models/person.rb
|
54
67
|
homepage: https://github.com/marcelocajueiro/mask_validator
|
55
68
|
licenses: []
|
56
69
|
post_install_message:
|
@@ -71,8 +84,14 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
71
84
|
version: '0'
|
72
85
|
requirements: []
|
73
86
|
rubyforge_project: mask_validator
|
74
|
-
rubygems_version: 1.
|
87
|
+
rubygems_version: 1.8.10
|
75
88
|
signing_key:
|
76
89
|
specification_version: 3
|
77
90
|
summary: Input Mask validation for ActiveModel
|
78
|
-
test_files:
|
91
|
+
test_files:
|
92
|
+
- spec/mask_validator_spec.rb
|
93
|
+
- spec/models/person.rb
|
94
|
+
- spec/spec_helper.rb
|
95
|
+
- spec/support/db/schema.rb
|
96
|
+
- spec/support/models/person.rb
|
97
|
+
has_rdoc:
|
data/test/mask_validator_test.rb
DELETED
@@ -1,50 +0,0 @@
|
|
1
|
-
require 'test_helper'
|
2
|
-
|
3
|
-
class MaskValidatorTest < Test::Unit::TestCase
|
4
|
-
def test_phones
|
5
|
-
create_asserts('phone')
|
6
|
-
|
7
|
-
assert_invalid '4852458787'
|
8
|
-
assert_invalid '48 9865 4879'
|
9
|
-
assert_invalid "(48)98956698"
|
10
|
-
assert_invalid "(48) 9874-45169"
|
11
|
-
assert_valid "(48) 9874-4569"
|
12
|
-
end
|
13
|
-
|
14
|
-
def test_acronyms
|
15
|
-
create_asserts('acronym')
|
16
|
-
|
17
|
-
assert_invalid '1qw1'
|
18
|
-
assert_valid '1gd'
|
19
|
-
assert_valid '666'
|
20
|
-
assert_valid 'zzz'
|
21
|
-
end
|
22
|
-
|
23
|
-
def test_alphanumerics
|
24
|
-
create_asserts('alphanumeric')
|
25
|
-
|
26
|
-
assert_invalid '999999'
|
27
|
-
assert_invalid 'QQQQQQ'
|
28
|
-
assert_invalid '666AAAA'
|
29
|
-
assert_invalid '666AAA'
|
30
|
-
assert_valid 'AAA666'
|
31
|
-
end
|
32
|
-
|
33
|
-
protected
|
34
|
-
|
35
|
-
def create_asserts(attr)
|
36
|
-
class_eval <<-RUBY
|
37
|
-
def assert_valid(value)
|
38
|
-
assert person(:#{attr} => value).valid?
|
39
|
-
end
|
40
|
-
|
41
|
-
def assert_invalid(value)
|
42
|
-
assert person(:#{attr} => value).invalid?
|
43
|
-
end
|
44
|
-
RUBY
|
45
|
-
end
|
46
|
-
|
47
|
-
def person(attributes = {})
|
48
|
-
Person.new(attributes)
|
49
|
-
end
|
50
|
-
end
|
data/test/test_helper.rb
DELETED
@@ -1,12 +0,0 @@
|
|
1
|
-
require 'rubygems'
|
2
|
-
require 'test/unit'
|
3
|
-
require 'active_model'
|
4
|
-
require 'mask_validator'
|
5
|
-
require 'ostruct'
|
6
|
-
|
7
|
-
class Person < OpenStruct
|
8
|
-
include ActiveModel::Validations
|
9
|
-
validates :phone, mask: "(99) 9999-9999"
|
10
|
-
validates :acronym, mask: "***"
|
11
|
-
validates :alphanumeric, mask: "aaa999"
|
12
|
-
end
|