validation_rules 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/LICENSE +20 -0
- data/README.md +3 -0
- data/lib/validation_rules/validation_rules.rb +129 -0
- data/lib/validation_rules/version.rb +3 -0
- data/lib/validation_rules.rb +5 -0
- data/spec/spec_helper.rb +3 -0
- data/spec/validation_rules_spec.rb +297 -0
- metadata +90 -0
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2013 Adam Gotterer
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,129 @@
|
|
1
|
+
require 'json'
|
2
|
+
require 'date'
|
3
|
+
require 'time'
|
4
|
+
|
5
|
+
module ValidationRules
|
6
|
+
EMAIL_REGEX = /^(?!(?:(?:\x22?\x5C[\x00-\x7E]\x22?)|(?:\x22?[^\x5C\x22]\x22?)){255,})(?!(?:(?:\x22?\x5C[\x00-\x7E]\x22?)|(?:\x22?[^\x5C\x22]\x22?)){65,}@)(?:(?:[\x21\x23-\x27\x2A\x2B\x2D\x2F-\x39\x3D\x3F\x5E-\x7E]+)|(?:\x22(?:[\x01-\x08\x0B\x0C\x0E-\x1F\x21\x23-\x5B\x5D-\x7F]|(?:\x5C[\x00-\x7F]))*\x22))(?:\.(?:(?:[\x21\x23-\x27\x2A\x2B\x2D\x2F-\x39\x3D\x3F\x5E-\x7E]+)|(?:\x22(?:[\x01-\x08\x0B\x0C\x0E-\x1F\x21\x23-\x5B\x5D-\x7F]|(?:\x5C[\x00-\x7F]))*\x22)))*@(?:(?:(?!.*[^.]{64,})(?:(?:(?:xn--)?[a-z0-9]+(?:-[a-z0-9]+)*\.){1,126}){1,}(?:(?:[a-z][a-z0-9]*)|(?:(?:xn--)[a-z0-9]+))(?:-[a-z0-9]+)*)|(?:\[(?:(?:IPv6:(?:(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){7})|(?:(?!(?:.*[a-f0-9][:\]]){7,})(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){0,5})?::(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){0,5})?)))|(?:(?:IPv6:(?:(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){5}:)|(?:(?!(?:.*[a-f0-9]:){5,})(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){0,3})?::(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){0,3}:)?)))?(?:(?:25[0-5])|(?:2[0-4][0-9])|(?:1[0-9]{2})|(?:[1-9]?[0-9]))(?:\.(?:(?:25[0-5])|(?:2[0-4][0-9])|(?:1[0-9]{2})|(?:[1-9]?[0-9]))){3}))\]))$/i
|
7
|
+
ALPHA_REGEX = /^[[:alpha:]]+$/
|
8
|
+
ALPHA_DASH_REGEX = /^[a-z\-_]+$/i
|
9
|
+
ALPHA_DASH_DIGIT_REGEX = /^[a-z\-_0-9]+$/i
|
10
|
+
ALPHA_DASH_SLASH_REGEX = /^[a-z\-_\/]+$/i
|
11
|
+
ALPHA_NUMERIC_REGEX = /^[[:alnum:]]+$/
|
12
|
+
ALPHA_NUMERIC_DASH_REGEX = /^[[:alnum:]\-_]+$/i
|
13
|
+
NUMERIC_REGEX = /^[0-9]+$/
|
14
|
+
DATE_REGEX = /^((?:[1-9][0-9]*)?[0-9]{4})-(1[0-2]|0[1-9])-(3[0-1]|0[1-9]|[1-2][0-9])$/
|
15
|
+
ISO8601_REGEX = /^(-?(?:[1-9][0-9]*)?[0-9]{4})-(1[0-2]|0[1-9])-(3[0-1]|0[1-9]|[1-2][0-9])(T(2[0-3]|[0-1][0-9]):([0-5][0-9]):([0-5][0-9])(\.[0-9]+)?(Z|[+-](?:2[0-3]|[0-1][0-9]):[0-5][0-9])?)?$/
|
16
|
+
|
17
|
+
def self.alpha(value)
|
18
|
+
value =~ ALPHA_REGEX
|
19
|
+
end
|
20
|
+
|
21
|
+
# Validates for letters, underscores and dashes
|
22
|
+
def self.alpha_dash(value)
|
23
|
+
value =~ ALPHA_DASH_REGEX
|
24
|
+
end
|
25
|
+
|
26
|
+
# Validates for letters, numbers, underscores and dashes
|
27
|
+
def self.alpha_dash_digit(value)
|
28
|
+
value =~ ALPHA_DASH_DIGIT_REGEX
|
29
|
+
end
|
30
|
+
|
31
|
+
# Validates for letters, numbers, forward-slashes, underscores and dashes
|
32
|
+
def self.alpha_dash_slash(value)
|
33
|
+
value =~ ALPHA_DASH_SLASH_REGEX
|
34
|
+
end
|
35
|
+
|
36
|
+
def self.alpha_numeric(value)
|
37
|
+
value =~ ALPHA_NUMERIC_REGEX
|
38
|
+
end
|
39
|
+
|
40
|
+
# Validates alphanumeric, dashes and underscores
|
41
|
+
def self.alpha_numeric_dash(value)
|
42
|
+
value =~ ALPHA_NUMERIC_DASH_REGEX
|
43
|
+
end
|
44
|
+
|
45
|
+
def self.date(value)
|
46
|
+
value =~ DATE_REGEX
|
47
|
+
end
|
48
|
+
|
49
|
+
def self.iso8601(value)
|
50
|
+
value =~ ISO8601_REGEX
|
51
|
+
end
|
52
|
+
|
53
|
+
def self.decimal(value, precision = 5, scale = 2)
|
54
|
+
before, after = precision - scale, scale
|
55
|
+
value.to_s =~ /^[-+]?\d{0,#{before}}?(?:\.\d{0,#{after}})?$/
|
56
|
+
end
|
57
|
+
|
58
|
+
def self.email(value)
|
59
|
+
value =~ EMAIL_REGEX
|
60
|
+
end
|
61
|
+
|
62
|
+
def self.bson_object_id(value)
|
63
|
+
::BSON::ObjectId.legal? value
|
64
|
+
end
|
65
|
+
|
66
|
+
def self.integer(value)
|
67
|
+
value.is_a? Integer
|
68
|
+
end
|
69
|
+
|
70
|
+
def self.future_date(value)
|
71
|
+
value = Time.parse(value) if value.is_a? String
|
72
|
+
value.to_i >= Time.now.to_i
|
73
|
+
end
|
74
|
+
|
75
|
+
def self.string(value)
|
76
|
+
value.is_a? String
|
77
|
+
end
|
78
|
+
|
79
|
+
def self.json(value)
|
80
|
+
return false unless value
|
81
|
+
begin
|
82
|
+
JSON.parse(value)
|
83
|
+
rescue JSON::ParserError
|
84
|
+
return false
|
85
|
+
end
|
86
|
+
|
87
|
+
return true
|
88
|
+
end
|
89
|
+
|
90
|
+
def self.money(value, decimals = 2)
|
91
|
+
value.to_s =~ /^[-+]?([0-9]+)?(?:\.[0-9]{0,#{decimals}})?$/
|
92
|
+
end
|
93
|
+
|
94
|
+
def self.length(value, length)
|
95
|
+
value.to_s.length == length
|
96
|
+
end
|
97
|
+
|
98
|
+
def self.matches(value, value1)
|
99
|
+
value == value1
|
100
|
+
end
|
101
|
+
|
102
|
+
def self.max_length(value, length)
|
103
|
+
value.to_s.length <= length
|
104
|
+
end
|
105
|
+
|
106
|
+
def self.min_length(value, length)
|
107
|
+
value.to_s.length >= length
|
108
|
+
end
|
109
|
+
|
110
|
+
def self.numeric(value)
|
111
|
+
value =~ NUMERIC_REGEX
|
112
|
+
end
|
113
|
+
|
114
|
+
def self.numeric_min(value, min)
|
115
|
+
value.to_f >= min.to_f
|
116
|
+
end
|
117
|
+
|
118
|
+
def self.numeric_max(value, max)
|
119
|
+
value.to_f <= max.to_f
|
120
|
+
end
|
121
|
+
|
122
|
+
def self.positive(value)
|
123
|
+
value.to_f > 0
|
124
|
+
end
|
125
|
+
|
126
|
+
def self.range(value, min, max)
|
127
|
+
value.to_f >= min.to_f and value.to_f <= max.to_f
|
128
|
+
end
|
129
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,297 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe ValidationRules do
|
4
|
+
context 'alpha' do
|
5
|
+
it 'passes for valid characters' do
|
6
|
+
ValidationRules.alpha('A').should be_true
|
7
|
+
ValidationRules.alpha('ABCDEFGHIJKLMNOPQRSTUVWXYZ').should be_true
|
8
|
+
ValidationRules.alpha('abcdefghijklmnopqrstuvwxyz').should be_true
|
9
|
+
end
|
10
|
+
|
11
|
+
it 'fails for invalid characters' do
|
12
|
+
ValidationRules.alpha('1234567890').should be_false
|
13
|
+
ValidationRules.alpha('ABC123').should be_false
|
14
|
+
ValidationRules.alpha('AB_').should be_false
|
15
|
+
ValidationRules.alpha('&^%@#').should be_false
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
context 'alpha_dash' do
|
20
|
+
it 'passes for valid characters' do
|
21
|
+
ValidationRules.alpha_dash('ABCDEFGHIJKLMNOPQRSTUVWXYZ').should be_true
|
22
|
+
ValidationRules.alpha_dash('abcdefghijklmnopqrstuvwxyz').should be_true
|
23
|
+
ValidationRules.alpha_dash('ABC-_').should be_true
|
24
|
+
ValidationRules.alpha_dash('ABCabc_-').should be_true
|
25
|
+
end
|
26
|
+
|
27
|
+
it 'fails for invalid characters' do
|
28
|
+
ValidationRules.alpha_dash('1234567890').should be_false
|
29
|
+
ValidationRules.alpha_dash('12345_-').should be_false
|
30
|
+
ValidationRules.alpha_dash('ABC_-%^&#').should be_false
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
context 'alpha_dash_digit' do
|
35
|
+
it 'passes for valid characters' do
|
36
|
+
ValidationRules.alpha_dash_digit('Aa-_0').should be_true
|
37
|
+
ValidationRules.alpha_dash_digit('_-_').should be_true
|
38
|
+
end
|
39
|
+
|
40
|
+
it 'fails for invalid characters' do
|
41
|
+
ValidationRules.alpha_dash_digit('ABCa-&%$').should be_false
|
42
|
+
ValidationRules.alpha_dash_digit('%^&*&(*\$#').should be_false
|
43
|
+
ValidationRules.alpha_dash_digit('ASbVN\_').should be_false
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
context 'alpha_dash_slash' do
|
48
|
+
it 'passes for valid characters' do
|
49
|
+
ValidationRules.alpha_dash_slash('ABCDEFGHIJKLMNOPQRSTUVWXYZ').should be_true
|
50
|
+
ValidationRules.alpha_dash_slash('abcdefghijklmnopqrstuvwxyz').should be_true
|
51
|
+
ValidationRules.alpha_dash_slash('ABC-_/').should be_true
|
52
|
+
ValidationRules.alpha_dash_slash('ABCabc_-/').should be_true
|
53
|
+
end
|
54
|
+
|
55
|
+
it 'fails for invalid characters' do
|
56
|
+
ValidationRules.alpha_dash_slash('1234567890').should be_false
|
57
|
+
ValidationRules.alpha_dash_slash('12345_-/').should be_false
|
58
|
+
ValidationRules.alpha_dash_slash('ABC_-%^&#/').should be_false
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
context 'alpha_numeric' do
|
63
|
+
it 'passes for valid characters' do
|
64
|
+
ValidationRules.alpha_numeric('ABCDEFGHIJKLMNOPQRSTUVWXYZ').should be_true
|
65
|
+
ValidationRules.alpha_numeric('abcdefghijklmnopqrstuvwxyz').should be_true
|
66
|
+
ValidationRules.alpha_numeric('1234567890').should be_true
|
67
|
+
ValidationRules.alpha_numeric('ABCDabc1234').should be_true
|
68
|
+
end
|
69
|
+
|
70
|
+
it 'fails for invalid characters' do
|
71
|
+
ValidationRules.alpha_numeric('ABC_-/').should be_false
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
context 'alpha_numeric_dash_regex' do
|
76
|
+
it 'passes for valid characters' do
|
77
|
+
ValidationRules.alpha_numeric_dash('ABCDEFGHIJKLMNOPQRSTUVWXYZ').should be_true
|
78
|
+
ValidationRules.alpha_numeric_dash('abcdefghijklmnopqrstuvwxyz').should be_true
|
79
|
+
ValidationRules.alpha_numeric_dash('A-B-C-0-9').should be_true
|
80
|
+
ValidationRules.alpha_numeric_dash('A_B_C_0_9').should be_true
|
81
|
+
ValidationRules.alpha_numeric_dash('A_-B_-C_-0_-9').should be_true
|
82
|
+
end
|
83
|
+
|
84
|
+
it 'fails for invalid characters' do
|
85
|
+
ValidationRules.alpha_numeric_dash('ABC/').should be_false
|
86
|
+
ValidationRules.alpha_numeric_dash('ABC&*^$#').should be_false
|
87
|
+
end
|
88
|
+
end
|
89
|
+
|
90
|
+
context 'decimal' do
|
91
|
+
it 'passes for valid numbers' do
|
92
|
+
ValidationRules.decimal(1).should be_true
|
93
|
+
ValidationRules.decimal('1').should be_true
|
94
|
+
ValidationRules.decimal('-1.0').should be_true
|
95
|
+
ValidationRules.decimal(-1.0).should be_true
|
96
|
+
ValidationRules.decimal(+1.0).should be_true
|
97
|
+
ValidationRules.decimal('+99.99').should be_true
|
98
|
+
ValidationRules.decimal('99.99').should be_true
|
99
|
+
ValidationRules.decimal(99.99).should be_true
|
100
|
+
ValidationRules.decimal('999.99').should be_true # check for precision
|
101
|
+
ValidationRules.decimal('9.99', 3, 2).should be_true # check for precision
|
102
|
+
ValidationRules.decimal('99.99', 4, 2).should be_true # check for precision
|
103
|
+
ValidationRules.decimal('9.999', 4, 3).should be_true # check for precision
|
104
|
+
ValidationRules.decimal('9.9', 4, 3).should be_true # check for precision
|
105
|
+
ValidationRules.decimal('.99').should be_true
|
106
|
+
end
|
107
|
+
|
108
|
+
it 'failes for invalid numbers' do
|
109
|
+
ValidationRules.decimal('sfdsf').should be_false
|
110
|
+
ValidationRules.decimal('aa.bb').should be_false
|
111
|
+
ValidationRules.decimal('99.99.99').should be_false
|
112
|
+
ValidationRules.decimal('9999.9').should be_false
|
113
|
+
ValidationRules.decimal('9.999', 4, 2).should be_false
|
114
|
+
end
|
115
|
+
end
|
116
|
+
|
117
|
+
context 'email' do
|
118
|
+
it 'passes for valid addresses' do
|
119
|
+
ValidationRules.email('name@domain.com').should be_true
|
120
|
+
ValidationRules.email('name2@domain.com').should be_true
|
121
|
+
ValidationRules.email('l3tt3rsAndNumb3rs@domain.com').should be_true
|
122
|
+
ValidationRules.email('has-dash@domain.com').should be_true
|
123
|
+
ValidationRules.email('hasApostrophe.o\'leary@domain.org').should be_true
|
124
|
+
ValidationRules.email('uncommonTLD@domain.museum').should be_true
|
125
|
+
ValidationRules.email('uncommonTLD@domain.travel').should be_true
|
126
|
+
ValidationRules.email('uncommonTLD@domain.mobi').should be_true
|
127
|
+
ValidationRules.email('countryCodeTLD@domain.uk').should be_true
|
128
|
+
ValidationRules.email('countryCodeTLD@domain.rw').should be_true
|
129
|
+
ValidationRules.email('lettersInDomain@911.com').should be_true
|
130
|
+
ValidationRules.email('underscore_inLocal@domain.net').should be_true
|
131
|
+
ValidationRules.email('subdomain@sub.domain.com').should be_true
|
132
|
+
ValidationRules.email('local@dash-inDomain.com').should be_true
|
133
|
+
ValidationRules.email('dot.inLocal@foo.com').should be_true
|
134
|
+
ValidationRules.email('a@singleLetterLocal.org').should be_true
|
135
|
+
ValidationRules.email('singleLetterDomain@x.org').should be_true
|
136
|
+
ValidationRules.email('&*=?^+{}\'~@validCharsInLocal.net').should be_true
|
137
|
+
ValidationRules.email('TLDDoesntExist@domain.moc').should be_true
|
138
|
+
ValidationRules.email('numbersInTLD@domain.c0m').should be_true
|
139
|
+
end
|
140
|
+
|
141
|
+
it 'fails for invalid addresses' do
|
142
|
+
ValidationRules.email('IPInsteadOfDomain@127.0.0.1').should be_false
|
143
|
+
ValidationRules.email('IPAndPort@127.0.0.1:25').should be_false
|
144
|
+
ValidationRules.email('missingDomain@.com').should be_false
|
145
|
+
ValidationRules.email('@missingLocal.org').should be_false
|
146
|
+
ValidationRules.email('missingatSign.net').should be_false
|
147
|
+
ValidationRules.email('missingDot@com').should be_false
|
148
|
+
ValidationRules.email('two@@signs.com').should be_false
|
149
|
+
ValidationRules.email('colonButNoPort@127.0.0.1:').should be_false
|
150
|
+
ValidationRules.email('someone-else@127.0.0.1.26').should be_false
|
151
|
+
ValidationRules.email('.localStartsWithDot@domain.com').should be_false
|
152
|
+
ValidationRules.email('localEndsWithDot.@domain.com').should be_false
|
153
|
+
ValidationRules.email('two..consecutiveDots@domain.com').should be_false
|
154
|
+
ValidationRules.email('domainStartsWithDash@-domain.com').should be_false
|
155
|
+
ValidationRules.email('domainEndsWithDash@domain-.com').should be_false
|
156
|
+
ValidationRules.email('missingTLD@domain.').should be_false
|
157
|
+
ValidationRules.email('! "#$%(),/;<>[]`|@invalidCharsInLocal.org').should be_false
|
158
|
+
ValidationRules.email('invalidCharsInDomain@! "#$%(),/;<>_[]`|.org').should be_false
|
159
|
+
ValidationRules.email('local@SecondLevelDomainNamesAreInvalidIfTheyAreLongerThan64Charactersss.org').should be_false
|
160
|
+
end
|
161
|
+
end
|
162
|
+
|
163
|
+
context 'money' do
|
164
|
+
it 'passes for valid values' do
|
165
|
+
ValidationRules.money('5.55').should be_true
|
166
|
+
ValidationRules.money('55.55').should be_true
|
167
|
+
ValidationRules.money(5.55).should be_true
|
168
|
+
ValidationRules.money('5.55', 4).should be_true
|
169
|
+
ValidationRules.money('5.555', 4).should be_true
|
170
|
+
ValidationRules.money('5.5555', 4).should be_true
|
171
|
+
ValidationRules.money('-5').should be_true
|
172
|
+
ValidationRules.money('+5').should be_true
|
173
|
+
ValidationRules.money(-5).should be_true
|
174
|
+
ValidationRules.money(+5).should be_true
|
175
|
+
ValidationRules.money('.98').should be_true
|
176
|
+
end
|
177
|
+
|
178
|
+
it 'fails for invalid values' do
|
179
|
+
ValidationRules.money('asdf').should be_false
|
180
|
+
ValidationRules.money('55.555', 2).should be_false
|
181
|
+
end
|
182
|
+
end
|
183
|
+
|
184
|
+
it 'validates length' do
|
185
|
+
ValidationRules.length('123456', 6).should be_true
|
186
|
+
ValidationRules.length(123456, 6).should be_true
|
187
|
+
|
188
|
+
ValidationRules.length('12345', 6).should be_false
|
189
|
+
ValidationRules.length('12345', 4).should be_false
|
190
|
+
end
|
191
|
+
|
192
|
+
it 'validates matches' do
|
193
|
+
ValidationRules.matches('ABC', 'ABC').should be_true
|
194
|
+
ValidationRules.matches('abc', 'abc').should be_true
|
195
|
+
|
196
|
+
ValidationRules.matches('abc', 'ABC').should be_false
|
197
|
+
ValidationRules.matches('abc', 'fgh').should be_false
|
198
|
+
end
|
199
|
+
|
200
|
+
it 'validates max_length' do
|
201
|
+
ValidationRules.max_length('abcd', 4).should be_true
|
202
|
+
ValidationRules.max_length('abcd', 10).should be_true
|
203
|
+
|
204
|
+
ValidationRules.max_length('abcd', 3).should be_false
|
205
|
+
end
|
206
|
+
|
207
|
+
it 'validates min_length' do
|
208
|
+
ValidationRules.min_length('abcd', 4).should be_true
|
209
|
+
ValidationRules.min_length('abcde', 3).should be_true
|
210
|
+
|
211
|
+
ValidationRules.min_length('abcde', 10).should be_false
|
212
|
+
end
|
213
|
+
|
214
|
+
it 'validates numeric values' do
|
215
|
+
ValidationRules.numeric('123456').should be_true
|
216
|
+
|
217
|
+
ValidationRules.numeric('ABCDS').should be_false
|
218
|
+
ValidationRules.numeric('10.99').should be_false
|
219
|
+
ValidationRules.numeric('ab123.99').should be_false
|
220
|
+
end
|
221
|
+
|
222
|
+
it 'validates numeric_min' do
|
223
|
+
ValidationRules.numeric_min('11', '10').should be_true
|
224
|
+
ValidationRules.numeric_min(11, 10).should be_true
|
225
|
+
ValidationRules.numeric_min(11, 11).should be_true
|
226
|
+
|
227
|
+
ValidationRules.numeric_min(40, 50).should be_false
|
228
|
+
end
|
229
|
+
|
230
|
+
it 'validates numeric_max' do
|
231
|
+
ValidationRules.numeric_max('50', '69').should be_true
|
232
|
+
ValidationRules.numeric_max(50, 69).should be_true
|
233
|
+
ValidationRules.numeric_max(50, 50).should be_true
|
234
|
+
|
235
|
+
ValidationRules.numeric_max(50, 40).should be_false
|
236
|
+
end
|
237
|
+
|
238
|
+
it 'validates positive' do
|
239
|
+
ValidationRules.positive(10).should be_true
|
240
|
+
ValidationRules.positive(10.99).should be_true
|
241
|
+
ValidationRules.positive('10.99').should be_true
|
242
|
+
|
243
|
+
ValidationRules.positive('-10.99').should be_false
|
244
|
+
ValidationRules.positive(-10.99).should be_false
|
245
|
+
ValidationRules.positive(0).should be_false
|
246
|
+
end
|
247
|
+
|
248
|
+
it 'validates range' do
|
249
|
+
ValidationRules.range(5, 1, 10).should be_true
|
250
|
+
ValidationRules.range(5.5, 1, 10).should be_true
|
251
|
+
ValidationRules.range('5', '1', '10').should be_true
|
252
|
+
ValidationRules.range('5.5', '1', '10').should be_true
|
253
|
+
ValidationRules.range(5, 5, 10).should be_true
|
254
|
+
ValidationRules.range(10, 1, 10).should be_true
|
255
|
+
|
256
|
+
ValidationRules.range(20, 5, 10).should be_false
|
257
|
+
ValidationRules.range(1, 5, 10).should be_false
|
258
|
+
end
|
259
|
+
|
260
|
+
it 'validates json' do
|
261
|
+
ValidationRules.json('{ "key": "value" }').should be_true
|
262
|
+
|
263
|
+
ValidationRules.json('{ "key: "value" }').should be_false
|
264
|
+
ValidationRules.json('no good').should be_false
|
265
|
+
end
|
266
|
+
|
267
|
+
it 'validates date' do
|
268
|
+
ValidationRules.date('2010-10-25').should be_true
|
269
|
+
|
270
|
+
ValidationRules.date('2010/10/25').should be_false
|
271
|
+
ValidationRules.date('10-25-83').should be_false
|
272
|
+
ValidationRules.date('10-25-1983').should be_false
|
273
|
+
ValidationRules.date('2010-10-25 01:11:59').should be_false
|
274
|
+
ValidationRules.date('2010-99-99').should be_false
|
275
|
+
end
|
276
|
+
|
277
|
+
it 'validates dates in iso8601' do
|
278
|
+
ValidationRules.iso8601('2010-10-25').should be_true
|
279
|
+
ValidationRules.iso8601('2010-10-25T00:00:00').should be_true
|
280
|
+
ValidationRules.iso8601('2010-10-25T10:00:00.12').should be_true
|
281
|
+
ValidationRules.iso8601('2010-10-25T10:00:00+07:00').should be_true
|
282
|
+
ValidationRules.iso8601('2010-10-25T10:00:00.12+07:00').should be_true
|
283
|
+
ValidationRules.iso8601('2010-10-25T00:00:00Z').should be_true
|
284
|
+
|
285
|
+
ValidationRules.iso8601('2010-10-25 00:00:00').should be_false
|
286
|
+
ValidationRules.iso8601('2010-10-25 00:99:00').should be_false
|
287
|
+
ValidationRules.iso8601('2010/10/05').should be_false
|
288
|
+
ValidationRules.iso8601('2010-99-99').should be_false
|
289
|
+
end
|
290
|
+
|
291
|
+
it 'validates future dates' do
|
292
|
+
ValidationRules.future_date('2060-10-25').should be_true
|
293
|
+
ValidationRules.future_date(Time.now).should be_true
|
294
|
+
|
295
|
+
ValidationRules.future_date('2010-10-25').should be_false
|
296
|
+
end
|
297
|
+
end
|
metadata
ADDED
@@ -0,0 +1,90 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: validation_rules
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 23
|
5
|
+
prerelease:
|
6
|
+
segments:
|
7
|
+
- 1
|
8
|
+
- 0
|
9
|
+
- 0
|
10
|
+
version: 1.0.0
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Adam Gotterer
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2013-04-05 00:00:00 Z
|
19
|
+
dependencies:
|
20
|
+
- !ruby/object:Gem::Dependency
|
21
|
+
name: rspec
|
22
|
+
prerelease: false
|
23
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
24
|
+
none: false
|
25
|
+
requirements:
|
26
|
+
- - ">="
|
27
|
+
- !ruby/object:Gem::Version
|
28
|
+
hash: 13
|
29
|
+
segments:
|
30
|
+
- 1
|
31
|
+
- 2
|
32
|
+
- 9
|
33
|
+
version: 1.2.9
|
34
|
+
type: :development
|
35
|
+
version_requirements: *id001
|
36
|
+
description: A collection of common validation rules
|
37
|
+
email: agotterer+gem@gmail.com
|
38
|
+
executables: []
|
39
|
+
|
40
|
+
extensions: []
|
41
|
+
|
42
|
+
extra_rdoc_files: []
|
43
|
+
|
44
|
+
files:
|
45
|
+
- lib/validation_rules/validation_rules.rb
|
46
|
+
- lib/validation_rules/version.rb
|
47
|
+
- lib/validation_rules.rb
|
48
|
+
- LICENSE
|
49
|
+
- README.md
|
50
|
+
- spec/spec_helper.rb
|
51
|
+
- spec/validation_rules_spec.rb
|
52
|
+
homepage: https://github.com/adamgotterer/validation_rules
|
53
|
+
licenses: []
|
54
|
+
|
55
|
+
post_install_message:
|
56
|
+
rdoc_options: []
|
57
|
+
|
58
|
+
require_paths:
|
59
|
+
- lib
|
60
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
61
|
+
none: false
|
62
|
+
requirements:
|
63
|
+
- - ">="
|
64
|
+
- !ruby/object:Gem::Version
|
65
|
+
hash: 55
|
66
|
+
segments:
|
67
|
+
- 1
|
68
|
+
- 9
|
69
|
+
- 2
|
70
|
+
version: 1.9.2
|
71
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
72
|
+
none: false
|
73
|
+
requirements:
|
74
|
+
- - ">="
|
75
|
+
- !ruby/object:Gem::Version
|
76
|
+
hash: 3
|
77
|
+
segments:
|
78
|
+
- 0
|
79
|
+
version: "0"
|
80
|
+
requirements: []
|
81
|
+
|
82
|
+
rubyforge_project:
|
83
|
+
rubygems_version: 1.8.24
|
84
|
+
signing_key:
|
85
|
+
specification_version: 3
|
86
|
+
summary: A collection of common validation rules
|
87
|
+
test_files:
|
88
|
+
- spec/spec_helper.rb
|
89
|
+
- spec/validation_rules_spec.rb
|
90
|
+
has_rdoc:
|