mask_validator 0.2 → 0.2.1
Sign up to get free protection for your applications and to get access to all the features.
- data/README.markdown +7 -1
- data/lib/mask_validator.rb +30 -19
- data/mask_validator.gemspec +1 -1
- data/spec/mask_validator_spec.rb +110 -83
- data/spec/models/person.rb +19 -9
- data/spec/spec_helper.rb +16 -0
- data/spec/support/db/schema.rb +2 -0
- data/spec/support/models/person.rb +19 -9
- metadata +9 -9
data/README.markdown
CHANGED
@@ -8,7 +8,9 @@ The gem works getting the value before type cast and comparing with a regexp fro
|
|
8
8
|
|
9
9
|
## Installation
|
10
10
|
|
11
|
-
|
11
|
+
Use the github version until a really stable version be released.
|
12
|
+
|
13
|
+
`gem "mask_validator", :git => "git://github.com/MarceloCajueiro/mask_validator.git"`
|
12
14
|
|
13
15
|
## Usage:
|
14
16
|
|
@@ -16,6 +18,10 @@ The gem works getting the value before type cast and comparing with a regexp fro
|
|
16
18
|
|
17
19
|
`validates :acronym, mask: "***"`
|
18
20
|
|
21
|
+
`validates :acronym, mask: :custom_method`
|
22
|
+
|
23
|
+
`validates :acronym, mask: Proc.new{|o| o.custom_method}`
|
24
|
+
|
19
25
|
* a - Represents an alpha character (A-Z,a-z)
|
20
26
|
* 9 - Represents a numeric character (0-9)
|
21
27
|
* * - Represents an alphanumeric character (A-Z,a-z,0-9)
|
data/lib/mask_validator.rb
CHANGED
@@ -13,47 +13,58 @@ class MaskValidator < ActiveModel::EachValidator
|
|
13
13
|
# 9 - to numbers (0-9)
|
14
14
|
# * - to alphanumerics (A-Z, a-z, 0-9)
|
15
15
|
#
|
16
|
+
def initialize(options)
|
17
|
+
@allow_nil, @allow_blank = options.delete(:allow_nil), options.delete(:allow_blank)
|
18
|
+
|
19
|
+
super
|
20
|
+
end
|
21
|
+
|
16
22
|
def validate_each(record, attribute, value)
|
17
23
|
value = record.send("#{attribute.to_s}_before_type_cast")
|
18
24
|
|
19
|
-
if value.nil?
|
20
|
-
record.errors.add(attribute, :blank) unless allow_nil?
|
21
|
-
end
|
25
|
+
return if (@allow_nil && value.nil?) || (@allow_blank && value.blank?)
|
22
26
|
|
23
|
-
|
24
|
-
record.errors.add(attribute, :empty) unless allow_blank?
|
25
|
-
else
|
26
|
-
record.errors.add(attribute, message, options) unless value.match(regexp)
|
27
|
-
end
|
27
|
+
record.errors.add(attribute, message, options) unless value.to_s.match regexp(record)
|
28
28
|
end
|
29
29
|
|
30
30
|
#
|
31
31
|
# Transform the string in a regular expression:
|
32
32
|
#
|
33
|
-
#
|
33
|
+
# mask_value_for(record) => "9a"
|
34
34
|
#
|
35
35
|
# regexp #=> /[0-9][a-zA-Z]/
|
36
36
|
#
|
37
37
|
# TODO: improve this
|
38
|
-
def regexp
|
39
|
-
/\A#{(
|
38
|
+
def regexp(record=nil)
|
39
|
+
/\A#{(mask_value_for(record).to_s.each_char.collect { |char| character_map[char] || "\\#{char}" }).join}\z/
|
40
40
|
end
|
41
41
|
|
42
42
|
def character_map
|
43
43
|
{ "9" => "[0-9]", "a" => "[a-zA-Z]", "*" => "[a-zA-Z0-9]" }
|
44
44
|
end
|
45
45
|
|
46
|
+
#
|
47
|
+
# Evaluate the options[:with] according with its class:
|
48
|
+
#
|
49
|
+
# options[:with] = :custom_mask
|
50
|
+
# options[:with] = Proc.new {|o| o.custom_mask}
|
51
|
+
# options[:with] = "9a"
|
52
|
+
#
|
53
|
+
# TODO: improve this
|
54
|
+
def mask_value_for(record=nil)
|
55
|
+
case options[:with]
|
56
|
+
when String
|
57
|
+
options[:with]
|
58
|
+
when Proc
|
59
|
+
options[:with].call(record)
|
60
|
+
when Symbol
|
61
|
+
record.send(options[:with])
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
46
65
|
private
|
47
66
|
|
48
67
|
def message
|
49
68
|
options[:message]
|
50
69
|
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
|
59
70
|
end
|
data/mask_validator.gemspec
CHANGED
data/spec/mask_validator_spec.rb
CHANGED
@@ -2,124 +2,147 @@ require 'spec_helper'
|
|
2
2
|
|
3
3
|
describe MaskValidator do
|
4
4
|
subject do
|
5
|
-
Person.new
|
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
|
5
|
+
Person.new
|
18
6
|
end
|
19
7
|
|
20
8
|
# validates :phone, :mask => "(99) 9999-9999", :allow_blank => true
|
21
|
-
context "mask validation to phone
|
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
|
-
|
9
|
+
context "mask validation to phone" do
|
32
10
|
it "should be valid with a phone in according of the mask" do
|
33
11
|
subject.phone = '(48) 9874-4569'
|
34
|
-
subject.should
|
12
|
+
subject.should have_valid_value_to(:phone)
|
35
13
|
end
|
36
14
|
|
37
15
|
it "should not be valid with a phone with a wrong pattern" do
|
38
16
|
subject.phone = '4852458787'
|
39
|
-
subject.should_not
|
17
|
+
subject.should_not have_valid_value_to(:phone)
|
40
18
|
|
41
19
|
subject.phone = '48 9865 4879'
|
42
|
-
subject.should_not
|
20
|
+
subject.should_not have_valid_value_to(:phone)
|
43
21
|
|
44
22
|
subject.phone = '(48) 9874-45169'
|
45
|
-
subject.should_not
|
23
|
+
subject.should_not have_valid_value_to(:phone)
|
46
24
|
|
47
25
|
subject.phone = '(48)98956698'
|
48
|
-
subject.should_not
|
26
|
+
subject.should_not have_valid_value_to(:phone)
|
49
27
|
end
|
50
28
|
end
|
51
29
|
|
52
30
|
# validates :acronym, :mask => "***", :allow_nil => true
|
53
|
-
context "mask validation to acronym
|
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
|
-
|
31
|
+
context "mask validation to acronym" do
|
64
32
|
it "should be valid with a acronym in according of the mask" do
|
65
33
|
subject.acronym = '1gd'
|
66
|
-
subject.should
|
34
|
+
subject.should have_valid_value_to(:acronym)
|
67
35
|
|
68
36
|
subject.acronym = '666'
|
69
|
-
subject.should
|
37
|
+
subject.should have_valid_value_to(:acronym)
|
70
38
|
|
71
39
|
subject.acronym = 'zzz'
|
72
|
-
subject.should
|
40
|
+
subject.should have_valid_value_to(:acronym)
|
73
41
|
end
|
74
42
|
|
75
43
|
it "should not be valid with a acronym with a wrong pattern" do
|
76
44
|
subject.acronym = '1qw1'
|
77
|
-
subject.should_not
|
45
|
+
subject.should_not have_valid_value_to(:acronym)
|
78
46
|
end
|
79
47
|
end
|
80
48
|
|
81
49
|
# validates :alphanumeric, :mask => "aaa999"
|
82
|
-
context "mask validation to alphanumeric
|
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
|
-
|
50
|
+
context "mask validation to alphanumeric" do
|
93
51
|
it "should be valid with a alphanumeric in according of the mask" do
|
94
52
|
subject.alphanumeric = 'awe987'
|
95
|
-
subject.should
|
53
|
+
subject.should have_valid_value_to(:alphanumeric)
|
96
54
|
end
|
97
55
|
|
98
56
|
it "should not be valid with a alphanumeric with a wrong pattern" do
|
99
57
|
subject.alphanumeric = '999999'
|
100
|
-
subject.should_not
|
58
|
+
subject.should_not have_valid_value_to(:alphanumeric)
|
101
59
|
|
102
60
|
subject.alphanumeric = 'QQQQQQ'
|
103
|
-
subject.should_not
|
61
|
+
subject.should_not have_valid_value_to(:alphanumeric)
|
104
62
|
|
105
63
|
subject.alphanumeric = '666aaaa'
|
106
|
-
subject.should_not
|
64
|
+
subject.should_not have_valid_value_to(:alphanumeric)
|
107
65
|
|
108
66
|
subject.alphanumeric = '666AAA'
|
109
|
-
subject.should_not
|
67
|
+
subject.should_not have_valid_value_to(:alphanumeric)
|
110
68
|
end
|
111
69
|
end
|
112
70
|
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
subject.
|
117
|
-
subject.should_not be_valid
|
71
|
+
context "when the attributes type is not a string" do
|
72
|
+
it "should not be valid with a wrong date format" do
|
73
|
+
subject.birth_date = "15-12-2009"
|
74
|
+
subject.should_not have_valid_value_to(:birth_date)
|
118
75
|
end
|
119
76
|
|
120
|
-
it "should not be valid with
|
121
|
-
subject.
|
122
|
-
subject.should_not
|
77
|
+
it "should not be valid with a wrong birth year" do
|
78
|
+
subject.birth_year = 20110
|
79
|
+
subject.should_not have_valid_value_to(:birth_year)
|
80
|
+
end
|
81
|
+
|
82
|
+
it "should not be valid with a wrong birth time" do
|
83
|
+
subject.birth_time = "333:20"
|
84
|
+
subject.should_not have_valid_value_to(:birth_time)
|
85
|
+
end
|
86
|
+
|
87
|
+
it "should not be valid with a wrong body fat" do
|
88
|
+
subject.body_fat = 333.00
|
89
|
+
subject.should_not have_valid_value_to(:body_fat)
|
90
|
+
end
|
91
|
+
end
|
92
|
+
|
93
|
+
# validates :custom, :mask => :custom_mask, :allow_blank => false
|
94
|
+
context "mask validation to custom field using custom_mask method" do
|
95
|
+
it "should be valid with an correct custom value" do
|
96
|
+
subject.stub!(:custom_mask => "99999-999")
|
97
|
+
subject.custom = '32632-567'
|
98
|
+
subject.should have_valid_value_to(:custom)
|
99
|
+
end
|
100
|
+
|
101
|
+
it "should not be valid with an empty custom value" do
|
102
|
+
subject.stub!(:custom_mask => "9.9.9")
|
103
|
+
subject.custom = ''
|
104
|
+
subject.should_not have_valid_value_to(:custom)
|
105
|
+
end
|
106
|
+
end
|
107
|
+
|
108
|
+
# validates :identification, :mask => Proc.new{|o| o.proc_mask}, :allow_blank => false
|
109
|
+
context "mask validation to proc field using a Proc" do
|
110
|
+
it "should be valid with an correct value" do
|
111
|
+
subject.stub!(:proc_mask => "999.9.99")
|
112
|
+
subject.identification = '326.3.67'
|
113
|
+
subject.should have_valid_value_to(:identification)
|
114
|
+
end
|
115
|
+
|
116
|
+
it "should not be valid with an empty value" do
|
117
|
+
subject.stub!(:prock_mask => "9.9.9")
|
118
|
+
subject.identification = ''
|
119
|
+
subject.should_not have_valid_value_to(:identification)
|
120
|
+
end
|
121
|
+
end
|
122
|
+
|
123
|
+
# validates :phone, :mask => "99999-999", :allow_blank => true
|
124
|
+
context "mask validation to phone with explicit ':allow_blank => true'" do
|
125
|
+
it "should not be valid with an nil phone" do
|
126
|
+
subject.phone = nil
|
127
|
+
subject.should have_valid_value_to(:phone)
|
128
|
+
end
|
129
|
+
|
130
|
+
it "should be valid with an empty phone" do
|
131
|
+
subject.phone = ''
|
132
|
+
subject.should have_valid_value_to(:phone)
|
133
|
+
end
|
134
|
+
end
|
135
|
+
|
136
|
+
# validates :acronym, :mask => "***", :allow_nil => true
|
137
|
+
context "mask validation to acronym with explicit ':allow_nil => true'" do
|
138
|
+
it "should be valid with an nil acronym" do
|
139
|
+
subject.acronym = nil
|
140
|
+
subject.should have_valid_value_to(:acronym)
|
141
|
+
end
|
142
|
+
|
143
|
+
it "should be valid with an empty acronym" do
|
144
|
+
subject.acronym = ''
|
145
|
+
subject.should_not have_valid_value_to(:acronym)
|
123
146
|
end
|
124
147
|
end
|
125
148
|
|
@@ -127,34 +150,38 @@ describe MaskValidator do
|
|
127
150
|
context "mask validation to fax with explicit ':allow_nil => false'" do
|
128
151
|
it "should not be valid with an nil fax" do
|
129
152
|
subject.fax = nil
|
130
|
-
subject.should_not
|
153
|
+
subject.should_not have_valid_value_to(:fax)
|
131
154
|
end
|
132
155
|
|
133
156
|
it "should be valid with an empty fax" do
|
134
157
|
subject.fax = ''
|
135
|
-
subject.
|
158
|
+
subject.should_not have_valid_value_to(:fax)
|
136
159
|
end
|
137
160
|
end
|
138
161
|
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
subject.
|
162
|
+
# validates :zip_code, :mask => "99999-999", :allow_blank => false
|
163
|
+
context "mask validation to zip_code with explicit ':allow_blank => false'" do
|
164
|
+
it "should not be valid with an nil zip_code" do
|
165
|
+
subject.zip_code = nil
|
166
|
+
subject.should_not have_valid_value_to(:zip_code)
|
143
167
|
end
|
144
168
|
|
145
|
-
it "should
|
146
|
-
subject.
|
147
|
-
subject.
|
169
|
+
it "should be valid with an empty zip_code" do
|
170
|
+
subject.zip_code = ''
|
171
|
+
subject.should_not have_valid_value_to(:zip_code)
|
148
172
|
end
|
173
|
+
end
|
149
174
|
|
150
|
-
|
151
|
-
|
152
|
-
|
175
|
+
# validates :birth_date, :mask => '99/99/9999'
|
176
|
+
context "mask validation to birth_date with implicit ':allow_blank => false'" do
|
177
|
+
it "should not be valid with an nil birth_date" do
|
178
|
+
subject.birth_date = nil
|
179
|
+
subject.should_not have_valid_value_to(:birth_date)
|
153
180
|
end
|
154
181
|
|
155
|
-
it "should
|
156
|
-
subject.
|
157
|
-
subject.
|
182
|
+
it "should be valid with an empty birth_date" do
|
183
|
+
subject.birth_date = ''
|
184
|
+
subject.should_not have_valid_value_to(:birth_date)
|
158
185
|
end
|
159
186
|
end
|
160
187
|
end
|
data/spec/models/person.rb
CHANGED
@@ -1,11 +1,21 @@
|
|
1
1
|
class Person < ActiveRecord::Base
|
2
|
-
validates :phone,
|
3
|
-
validates :fax,
|
4
|
-
validates :acronym,
|
5
|
-
validates :alphanumeric,
|
6
|
-
validates :zip_code,
|
7
|
-
validates :birth_date,
|
8
|
-
validates :birth_time,
|
9
|
-
validates :birth_year,
|
10
|
-
validates :body_fat,
|
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
|
+
validates :custom, :mask => :custom_mask, :allow_blank => false
|
12
|
+
validates :identification, :mask => Proc.new{|o| o.proc_mask}, :allow_blank => false
|
13
|
+
|
14
|
+
def custom_mask
|
15
|
+
"999.99"
|
16
|
+
end
|
17
|
+
|
18
|
+
def proc_mask
|
19
|
+
"99.99"
|
20
|
+
end
|
11
21
|
end
|
data/spec/spec_helper.rb
CHANGED
@@ -5,3 +5,19 @@ require 'mask_validator'
|
|
5
5
|
|
6
6
|
require "support/db/schema"
|
7
7
|
require "support/models/person"
|
8
|
+
|
9
|
+
RSpec::Matchers.define :have_valid_value_to do |attribute|
|
10
|
+
match do |actual|
|
11
|
+
actual.valid?
|
12
|
+
|
13
|
+
!actual.errors[attribute].include?("is invalid")
|
14
|
+
end
|
15
|
+
|
16
|
+
failure_message_for_should do |actual|
|
17
|
+
"expected that the #{attribute} attribute would be valid"
|
18
|
+
end
|
19
|
+
|
20
|
+
failure_message_for_should_not do |actual|
|
21
|
+
"expected that the #{attribute} attribute would be invalid"
|
22
|
+
end
|
23
|
+
end
|
data/spec/support/db/schema.rb
CHANGED
@@ -1,11 +1,21 @@
|
|
1
1
|
class Person < ActiveRecord::Base
|
2
|
-
validates :phone,
|
3
|
-
validates :fax,
|
4
|
-
validates :acronym,
|
5
|
-
validates :alphanumeric,
|
6
|
-
validates :zip_code,
|
7
|
-
validates :birth_date,
|
8
|
-
validates :birth_time,
|
9
|
-
validates :birth_year,
|
10
|
-
validates :body_fat,
|
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
|
+
validates :custom, :mask => :custom_mask, :allow_blank => false
|
12
|
+
validates :identification, :mask => Proc.new{|o| o.proc_mask}, :allow_blank => false
|
13
|
+
|
14
|
+
def custom_mask
|
15
|
+
"999.99"
|
16
|
+
end
|
17
|
+
|
18
|
+
def proc_mask
|
19
|
+
"99.99"
|
20
|
+
end
|
11
21
|
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:
|
4
|
+
version: 0.2.1
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,11 +9,11 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-
|
12
|
+
date: 2012-04-19 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: activemodel
|
16
|
-
requirement: &
|
16
|
+
requirement: &70113695766940 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ! '>='
|
@@ -21,10 +21,10 @@ dependencies:
|
|
21
21
|
version: '3.0'
|
22
22
|
type: :runtime
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
24
|
+
version_requirements: *70113695766940
|
25
25
|
- !ruby/object:Gem::Dependency
|
26
26
|
name: activerecord
|
27
|
-
requirement: &
|
27
|
+
requirement: &70113695766380 !ruby/object:Gem::Requirement
|
28
28
|
none: false
|
29
29
|
requirements:
|
30
30
|
- - ! '>='
|
@@ -32,10 +32,10 @@ dependencies:
|
|
32
32
|
version: '3.0'
|
33
33
|
type: :runtime
|
34
34
|
prerelease: false
|
35
|
-
version_requirements: *
|
35
|
+
version_requirements: *70113695766380
|
36
36
|
- !ruby/object:Gem::Dependency
|
37
37
|
name: rake
|
38
|
-
requirement: &
|
38
|
+
requirement: &70113695765640 !ruby/object:Gem::Requirement
|
39
39
|
none: false
|
40
40
|
requirements:
|
41
41
|
- - ! '>='
|
@@ -43,7 +43,7 @@ dependencies:
|
|
43
43
|
version: 0.8.7
|
44
44
|
type: :development
|
45
45
|
prerelease: false
|
46
|
-
version_requirements: *
|
46
|
+
version_requirements: *70113695765640
|
47
47
|
description:
|
48
48
|
email:
|
49
49
|
- marcelocajueiro@gmail.com
|
@@ -84,7 +84,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
84
84
|
version: '0'
|
85
85
|
requirements: []
|
86
86
|
rubyforge_project: mask_validator
|
87
|
-
rubygems_version: 1.8.
|
87
|
+
rubygems_version: 1.8.17
|
88
88
|
signing_key:
|
89
89
|
specification_version: 3
|
90
90
|
summary: Input Mask validation for ActiveModel
|