active_validation 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (93) hide show
  1. checksums.yaml +7 -0
  2. data/.coveralls.yml +1 -0
  3. data/.gitignore +14 -0
  4. data/.rspec +4 -0
  5. data/.travis.yml +13 -0
  6. data/Gemfile +4 -0
  7. data/LICENSE.txt +22 -0
  8. data/README.md +1099 -0
  9. data/Rakefile +1 -0
  10. data/active_validation.gemspec +29 -0
  11. data/config/locales/en.yml +109 -0
  12. data/lib/active_validation/matchers/ensure_valid_alpha_format_of.rb +26 -0
  13. data/lib/active_validation/matchers/ensure_valid_alpha_numeric_format_of.rb +26 -0
  14. data/lib/active_validation/matchers/ensure_valid_base64_format_of.rb +26 -0
  15. data/lib/active_validation/matchers/ensure_valid_boolean_format_of.rb +26 -0
  16. data/lib/active_validation/matchers/ensure_valid_credit_card_format_of.rb +26 -0
  17. data/lib/active_validation/matchers/ensure_valid_currency_format_of.rb +26 -0
  18. data/lib/active_validation/matchers/ensure_valid_cusip_format_of.rb +26 -0
  19. data/lib/active_validation/matchers/ensure_valid_email_format_of.rb +26 -0
  20. data/lib/active_validation/matchers/ensure_valid_equality_matcher_of.rb +40 -0
  21. data/lib/active_validation/matchers/ensure_valid_hex_format_of.rb +26 -0
  22. data/lib/active_validation/matchers/ensure_valid_imei_format_of.rb +26 -0
  23. data/lib/active_validation/matchers/ensure_valid_ip_format_of.rb +26 -0
  24. data/lib/active_validation/matchers/ensure_valid_isbn_format_of.rb +26 -0
  25. data/lib/active_validation/matchers/ensure_valid_isin_format_of.rb +26 -0
  26. data/lib/active_validation/matchers/ensure_valid_latitude_format_of.rb +26 -0
  27. data/lib/active_validation/matchers/ensure_valid_longitude_format_of.rb +26 -0
  28. data/lib/active_validation/matchers/ensure_valid_mac_address_format_of.rb +26 -0
  29. data/lib/active_validation/matchers/ensure_valid_name_format_of.rb +26 -0
  30. data/lib/active_validation/matchers/ensure_valid_password_format_of.rb +26 -0
  31. data/lib/active_validation/matchers/ensure_valid_phone_format_of.rb +26 -0
  32. data/lib/active_validation/matchers/ensure_valid_sedol_format_of.rb +26 -0
  33. data/lib/active_validation/matchers/ensure_valid_slug_format_of.rb +26 -0
  34. data/lib/active_validation/matchers/ensure_valid_ssn_format_of.rb +26 -0
  35. data/lib/active_validation/matchers/ensure_valid_url_format_of.rb +26 -0
  36. data/lib/active_validation/matchers/ensure_valid_username_format_of.rb +26 -0
  37. data/lib/active_validation/matchers/ensure_valid_uuid_format_of.rb +26 -0
  38. data/lib/active_validation/validators/alpha_numeric_validator.rb +30 -0
  39. data/lib/active_validation/validators/alpha_validator.rb +31 -0
  40. data/lib/active_validation/validators/base64_validator.rb +9 -0
  41. data/lib/active_validation/validators/boolean_validator.rb +9 -0
  42. data/lib/active_validation/validators/credit_card_validator.rb +133 -0
  43. data/lib/active_validation/validators/currency_validator.rb +23 -0
  44. data/lib/active_validation/validators/cusip_validator.rb +33 -0
  45. data/lib/active_validation/validators/email_validator.rb +25 -0
  46. data/lib/active_validation/validators/equality_validator.rb +27 -0
  47. data/lib/active_validation/validators/hex_validator.rb +9 -0
  48. data/lib/active_validation/validators/imei_validator.rb +37 -0
  49. data/lib/active_validation/validators/ip_validator.rb +9 -0
  50. data/lib/active_validation/validators/isbn_validator.rb +24 -0
  51. data/lib/active_validation/validators/isin_validator.rb +38 -0
  52. data/lib/active_validation/validators/latitude_validator.rb +9 -0
  53. data/lib/active_validation/validators/longitude_validator.rb +9 -0
  54. data/lib/active_validation/validators/mac_address_validator.rb +24 -0
  55. data/lib/active_validation/validators/name_validator.rb +9 -0
  56. data/lib/active_validation/validators/password_validator.rb +23 -0
  57. data/lib/active_validation/validators/phone_validator.rb +9 -0
  58. data/lib/active_validation/validators/sedol_validator.rb +32 -0
  59. data/lib/active_validation/validators/slug_validator.rb +9 -0
  60. data/lib/active_validation/validators/ssn_validator.rb +9 -0
  61. data/lib/active_validation/validators/url_validator.rb +36 -0
  62. data/lib/active_validation/validators/username_validator.rb +9 -0
  63. data/lib/active_validation/validators/uuid_validator.rb +28 -0
  64. data/lib/active_validation/version.rb +3 -0
  65. data/lib/active_validation.rb +91 -0
  66. data/spec/lib/alpha_numeric_validator_spec.rb +91 -0
  67. data/spec/lib/alpha_validator_spec.rb +182 -0
  68. data/spec/lib/base64_validator_spec.rb +33 -0
  69. data/spec/lib/boolean_validator_spec.rb +35 -0
  70. data/spec/lib/credit_card_validator_spec.rb +686 -0
  71. data/spec/lib/currency_validator_spec.rb +63 -0
  72. data/spec/lib/cusip_validator_spec.rb +27 -0
  73. data/spec/lib/email_validator_spec.rb +109 -0
  74. data/spec/lib/equality_validator_spec.rb +334 -0
  75. data/spec/lib/hex_validator_spec.rb +73 -0
  76. data/spec/lib/imei_validator_spec.rb +41 -0
  77. data/spec/lib/ip_validator_spec.rb +33 -0
  78. data/spec/lib/isbn_validator_spec.rb +41 -0
  79. data/spec/lib/isin_validator_spec.rb +35 -0
  80. data/spec/lib/latitude_validator_spec.rb +31 -0
  81. data/spec/lib/longitude_validator_spec.rb +31 -0
  82. data/spec/lib/mac_address_validator_spec.rb +54 -0
  83. data/spec/lib/name_validator_spec.rb +39 -0
  84. data/spec/lib/password_validator_spec.rb +85 -0
  85. data/spec/lib/phone_validator_spec.rb +42 -0
  86. data/spec/lib/sedol_validator_spec.rb +31 -0
  87. data/spec/lib/slug_validator_spec.rb +41 -0
  88. data/spec/lib/ssn_validator_spec.rb +36 -0
  89. data/spec/lib/url_validator_spec.rb +106 -0
  90. data/spec/lib/username_validator_spec.rb +37 -0
  91. data/spec/lib/uuid_validator_spec.rb +157 -0
  92. data/spec/spec_helper.rb +12 -0
  93. metadata +260 -0
@@ -0,0 +1,63 @@
1
+ require 'spec_helper'
2
+
3
+ describe CurrencyValidator do
4
+
5
+ context "has a valid value" do
6
+ let(:klass) do
7
+ Class.new do
8
+ include ActiveModel::Validations
9
+ attr_accessor :price, :name
10
+ validates :price, currency: true
11
+ end
12
+ end
13
+
14
+ subject { klass.new }
15
+
16
+ it { should allow_value(".00").for(:price) }
17
+ it { should allow_value("1.0").for(:price) }
18
+ it { should allow_value("1.00").for(:price) }
19
+ it { should allow_value("12345678.00").for(:price) }
20
+
21
+ it { should_not allow_value('').for(:price) }
22
+ it { should_not allow_value(' ').for(:price) }
23
+ it { should_not allow_value(nil).for(:price) }
24
+ it { should_not allow_value("1").for(:price) }
25
+ it { should_not allow_value("1.000").for(:price) }
26
+ it { should_not allow_value("$1.00").for(:price) }
27
+ it { should_not allow_value("! \#$%\`|").for(:price) }
28
+ it { should_not allow_value("<>@[]\`|").for(:price) }
29
+
30
+ it { should ensure_valid_currency_format_of(:price) }
31
+ it { should_not ensure_valid_currency_format_of(:name) }
32
+ end
33
+
34
+ context "with :strict option has a valid value" do
35
+ let(:klass) do
36
+ Class.new do
37
+ include ActiveModel::Validations
38
+ attr_accessor :price, :name
39
+ validates :price, currency: { strict: true }
40
+ end
41
+ end
42
+
43
+ subject { klass.new }
44
+
45
+ it { should allow_value("1.00").for(:price) }
46
+ it { should allow_value("12345678.00").for(:price) }
47
+
48
+ it { should_not allow_value('').for(:price) }
49
+ it { should_not allow_value(' ').for(:price) }
50
+ it { should_not allow_value(nil).for(:price) }
51
+ it { should_not allow_value(".00").for(:price) }
52
+ it { should_not allow_value("1").for(:price) }
53
+ it { should_not allow_value("1.0").for(:price) }
54
+ it { should_not allow_value("1.000").for(:price) }
55
+ it { should_not allow_value("$1.00").for(:price) }
56
+ it { should_not allow_value("! \#$%\`|").for(:price) }
57
+ it { should_not allow_value("<>@[]\`|").for(:price) }
58
+
59
+ it { should ensure_valid_currency_format_of(:price) }
60
+ it { should_not ensure_valid_currency_format_of(:name) }
61
+ end
62
+
63
+ end
@@ -0,0 +1,27 @@
1
+ require 'spec_helper'
2
+
3
+ describe CusipValidator do
4
+
5
+ context "has a valid value" do
6
+ let(:klass) do
7
+ Class.new do
8
+ include ActiveModel::Validations
9
+ attr_accessor :code, :name
10
+ validates :code, cusip: true
11
+ end
12
+ end
13
+
14
+ subject { klass.new }
15
+
16
+ it { should allow_value("125509BG3").for(:code) }
17
+
18
+ it { should_not allow_value('').for(:code) }
19
+ it { should_not allow_value(' ').for(:code) }
20
+ it { should_not allow_value(nil).for(:code) }
21
+ it { should_not allow_value("12345678AB").for(:code) }
22
+
23
+ it { should ensure_valid_cusip_format_of(:code) }
24
+ it { should_not ensure_valid_cusip_format_of(:name) }
25
+ end
26
+
27
+ end
@@ -0,0 +1,109 @@
1
+ require 'spec_helper'
2
+
3
+ describe EmailValidator do
4
+
5
+ subject { klass.new }
6
+
7
+ context "with valid format" do
8
+ let(:klass) do
9
+ Class.new do
10
+ include ActiveModel::Validations
11
+ attr_accessor :email, :name
12
+ validates :email, email: true
13
+ end
14
+ end
15
+
16
+ [
17
+ "s_u@example.com",
18
+ "super.user@example.com",
19
+ "super+user@example.com",
20
+ "super-user@example.com",
21
+ "super+user@example-site.com",
22
+ "user@example.com",
23
+ "user@example-site.com",
24
+ "user@en.example.com",
25
+ "user@example.museum",
26
+ "user@123.com",
27
+ "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ@example.com",
28
+ "01234567890@example.com"
29
+ ].each do |email|
30
+ it "#{email.inspect} should be valid" do
31
+ should allow_value(email).for(:email)
32
+ end
33
+ end
34
+
35
+ [
36
+ "",
37
+ " ",
38
+ "example.com",
39
+ "super user@example.com",
40
+ " user@example.com",
41
+ " user@example.com ",
42
+ "user@example.com ",
43
+ "user",
44
+ "user@com",
45
+ "user@.com",
46
+ "user@example",
47
+ "user@example.",
48
+ "user@example.c",
49
+ "user_example.com",
50
+ "user@example_site.com",
51
+ "user@example.com@example.com",
52
+ "user@",
53
+ "user@! \"\#$%(),/;<>_[]\`|.com",
54
+ "user@127.0.0.1",
55
+ "user@127.0.0.1:25",
56
+ "user@example.com\n<script>alert('hello')</script>",
57
+ "@example.com",
58
+ "@example",
59
+ "@",
60
+ "! \#$%\`|@example.com",
61
+ "<>@[]\`|@example.com"
62
+ ].each do |email|
63
+ it "#{email.inspect} should be invalid" do
64
+ should_not allow_value(email).for(:email)
65
+ end
66
+ end
67
+
68
+ it { should ensure_valid_email_format_of(:email) }
69
+ it { should_not ensure_valid_email_format_of(:name) }
70
+ end
71
+
72
+ context "is in the specific domain" do
73
+ context "domain specified as string" do
74
+ let(:klass) do
75
+ Class.new do
76
+ include ActiveModel::Validations
77
+ attr_accessor :email, :name
78
+ validates :email, email: { domain: "edu" }
79
+ end
80
+ end
81
+
82
+ it { should allow_value("user@example.edu").for(:email) }
83
+ it { should_not allow_value("user@example.com").for(:email) }
84
+
85
+ it { should ensure_valid_email_format_of(:email) }
86
+ it { should_not ensure_valid_email_format_of(:name) }
87
+ end
88
+
89
+ context "set as an array of strings and symbols" do
90
+ let(:klass) do
91
+ Class.new do
92
+ include ActiveModel::Validations
93
+ attr_accessor :email, :name
94
+ validates :email, email: { domain: ['com', :edu, 'Com.Au'] }
95
+ end
96
+ end
97
+
98
+ it { should allow_value("user@example.com").for(:email) }
99
+ it { should allow_value("user@example.edu").for(:email) }
100
+ it { should allow_value("user@example.com.au").for(:email) }
101
+ it { should allow_value("user@example.Com.Au").for(:email) }
102
+ it { should_not allow_value("user@example.org").for(:email) }
103
+
104
+ it { should ensure_valid_email_format_of(:email) }
105
+ it { should_not ensure_valid_email_format_of(:name) }
106
+ end
107
+ end
108
+
109
+ end
@@ -0,0 +1,334 @@
1
+ require 'spec_helper'
2
+
3
+ describe EqualityValidator do
4
+
5
+ context "value less than" do
6
+ let(:klass) do
7
+ Class.new do
8
+ include ActiveModel::Validations
9
+ attr_accessor :bid, :price, :name
10
+ validates :bid, equality: { operator: :less_than, to: :price }
11
+ end
12
+ end
13
+
14
+ subject(:model){ klass.new }
15
+
16
+ specify "first field less than the second" do
17
+ model.bid = 1
18
+ model.price = 2
19
+ expect(model).to be_valid
20
+ end
21
+
22
+ specify "first field equal to the second" do
23
+ model.bid = 1
24
+ model.price = 1
25
+ expect(model).to be_invalid
26
+ end
27
+
28
+ specify "first field greater than the second" do
29
+ model.bid = 1
30
+ model.price = 0
31
+ expect(model).to be_invalid
32
+ end
33
+
34
+ specify "first field is empty, the second is blank" do
35
+ model.bid = ""
36
+ model.price = " "
37
+ expect(model).to be_valid
38
+ end
39
+
40
+ specify "first field is blank, the second is empty" do
41
+ model.bid = " "
42
+ model.price = ""
43
+ expect(model).to be_invalid
44
+ end
45
+ end
46
+
47
+ context "value less than or equal to" do
48
+ let(:klass) do
49
+ Class.new do
50
+ include ActiveModel::Validations
51
+ attr_accessor :bid, :price, :name
52
+ validates :bid, equality: { operator: :less_than_or_equal_to, to: :price }
53
+ end
54
+ end
55
+
56
+ subject(:model){ klass.new }
57
+
58
+ specify "first field equal to the second" do
59
+ model.bid = 1
60
+ model.price = 1
61
+ expect(model).to be_valid
62
+ end
63
+
64
+ specify "first field less than the second" do
65
+ model.bid = 1
66
+ model.price = 2
67
+ expect(model).to be_valid
68
+ end
69
+
70
+ specify "first field greater than the second" do
71
+ model.bid = 1
72
+ model.price = 0
73
+ expect(model).to be_invalid
74
+ end
75
+
76
+ specify "first field is empty, the second is blank" do
77
+ model.bid = ""
78
+ model.price = " "
79
+ expect(model).to be_valid
80
+ end
81
+
82
+ specify "first field is blank, the second is empty" do
83
+ model.bid = " "
84
+ model.price = ""
85
+ expect(model).to be_invalid
86
+ end
87
+
88
+ specify "both fields are blank" do
89
+ model.bid = model.price = ""
90
+ expect(model).to be_valid
91
+ end
92
+
93
+ specify "both fields are empty" do
94
+ model.bid = model.price = " "
95
+ expect(model).to be_valid
96
+ end
97
+ end
98
+
99
+ context "value greater than" do
100
+ let(:klass) do
101
+ Class.new do
102
+ include ActiveModel::Validations
103
+ attr_accessor :bid, :price, :name
104
+ validates :bid, equality: { operator: :greater_than, to: :price }
105
+ end
106
+ end
107
+
108
+ subject(:model){ klass.new }
109
+
110
+ specify "first field greater than the second" do
111
+ model.bid = 1
112
+ model.price = 0
113
+ expect(model).to be_valid
114
+ end
115
+
116
+ specify "first field less than the second" do
117
+ model.bid = 1
118
+ model.price = 2
119
+ expect(model).to be_invalid
120
+ end
121
+
122
+ specify "first field equal to the second" do
123
+ model.bid = 1
124
+ model.price = 1
125
+ expect(model).to be_invalid
126
+ end
127
+
128
+ specify "first field is empty, the second is blank" do
129
+ model.bid = ""
130
+ model.price = " "
131
+ expect(model).to be_invalid
132
+ end
133
+
134
+ specify "first field is blank, the second is empty" do
135
+ model.bid = " "
136
+ model.price = ""
137
+ expect(model).to be_valid
138
+ end
139
+ end
140
+
141
+ context "value greater than or equal to" do
142
+ let(:klass) do
143
+ Class.new do
144
+ include ActiveModel::Validations
145
+ attr_accessor :bid, :price, :name
146
+ validates :bid, equality: { operator: :greater_than_or_equal_to, to: :price }
147
+ end
148
+ end
149
+
150
+ subject(:model){ klass.new }
151
+
152
+ specify "first field equal to the second" do
153
+ model.bid = 1
154
+ model.price = 1
155
+ expect(model).to be_valid
156
+ end
157
+
158
+ specify "first field greater than the second" do
159
+ model.bid = 1
160
+ model.price = 0
161
+ expect(model).to be_valid
162
+ end
163
+
164
+ specify "first field less than the second" do
165
+ model.bid = 1
166
+ model.price = 2
167
+ expect(model).to be_invalid
168
+ end
169
+
170
+ specify "first field is empty, the second is blank" do
171
+ model.bid = ""
172
+ model.price = " "
173
+ expect(model).to be_invalid
174
+ end
175
+
176
+ specify "first field is blank, the second is empty" do
177
+ model.bid = " "
178
+ model.price = ""
179
+ expect(model).to be_valid
180
+ end
181
+
182
+ specify "both fields are blank" do
183
+ model.bid = model.price = ""
184
+ expect(model).to be_valid
185
+ end
186
+
187
+ specify "both fields are empty" do
188
+ model.bid = model.price = " "
189
+ expect(model).to be_valid
190
+ end
191
+ end
192
+
193
+ context "value equal to" do
194
+ let(:klass) do
195
+ Class.new do
196
+ include ActiveModel::Validations
197
+ attr_accessor :bid, :price, :name
198
+ validates :bid, equality: { operator: :equal_to, to: :price }
199
+ end
200
+ end
201
+
202
+ subject(:model){ klass.new }
203
+
204
+ specify "first field equal to the second" do
205
+ model.bid = 1
206
+ model.price = 1
207
+ expect(model).to be_valid
208
+ end
209
+
210
+ specify "first field less than the second" do
211
+ model.bid = 1
212
+ model.price = 2
213
+ expect(model).to be_invalid
214
+ end
215
+
216
+ specify "first field greater than the second" do
217
+ model.bid = 1
218
+ model.price = 0
219
+ expect(model).to be_invalid
220
+ end
221
+
222
+ specify "first field has value, the second is nil" do
223
+ model.bid = 1
224
+ model.price = nil
225
+ expect(model).to be_invalid
226
+ end
227
+
228
+ specify "first field is nil, the second has value" do
229
+ model.bid = nil
230
+ model.price = 1
231
+ expect(model).to be_invalid
232
+ end
233
+
234
+ specify "both fields are nil" do
235
+ model.bid = model.price = nil
236
+ expect(model).to be_valid
237
+ end
238
+
239
+ specify "first field is empty, the second is blank" do
240
+ model.bid = ""
241
+ model.price = " "
242
+ expect(model).to be_invalid
243
+ end
244
+
245
+ specify "first field is blank, the second is empty" do
246
+ model.bid = " "
247
+ model.price = ""
248
+ expect(model).to be_invalid
249
+ end
250
+
251
+ specify "both fields are blank" do
252
+ model.bid = model.price = ""
253
+ expect(model).to be_valid
254
+ end
255
+
256
+ specify "both fields are empty" do
257
+ model.bid = model.price = " "
258
+ expect(model).to be_valid
259
+ end
260
+ end
261
+
262
+ context "value not equal to" do
263
+ let(:klass) do
264
+ Class.new do
265
+ include ActiveModel::Validations
266
+ attr_accessor :bid, :price, :name
267
+ validates :bid, equality: { operator: :not_equal_to, to: :price }
268
+ end
269
+ end
270
+
271
+ subject(:model){ klass.new }
272
+
273
+ specify "first field equal to the second" do
274
+ model.bid = 1
275
+ model.price = 1
276
+ expect(model).to be_invalid
277
+ end
278
+
279
+ specify "first field less than the second" do
280
+ model.bid = 1
281
+ model.price = 2
282
+ expect(model).to be_valid
283
+ end
284
+
285
+ specify "first field greater than the second" do
286
+ model.bid = 1
287
+ model.price = 0
288
+ expect(model).to be_valid
289
+ end
290
+
291
+ specify "first field has value, the second is nil" do
292
+ model.bid = 1
293
+ model.price = nil
294
+ expect(model).to be_valid
295
+ end
296
+
297
+ specify "first field is nil, the second has value" do
298
+ model.bid = nil
299
+ model.price = 1
300
+ expect(model).to be_valid
301
+ end
302
+
303
+ specify "both fields are nil" do
304
+ model.bid = model.price = nil
305
+ expect(model).to be_invalid
306
+ end
307
+
308
+ specify "first field is empty, the second is blank" do
309
+ model.bid = ""
310
+ model.price = " "
311
+ expect(model).to be_valid
312
+ end
313
+
314
+ specify "first field is blank, the second is empty" do
315
+ model.bid = " "
316
+ model.price = ""
317
+ expect(model).to be_valid
318
+ end
319
+
320
+ specify "both fields are blank" do
321
+ model.bid = model.price = ""
322
+ expect(model).to be_invalid
323
+ end
324
+
325
+ specify "both fields are empty" do
326
+ model.bid = model.price = " "
327
+ expect(model).to be_invalid
328
+ end
329
+ end
330
+
331
+ #it { should ensure_equality_of(:bid).to(:price).operator(:less_than) }
332
+ #it { should_not ensure_equality_of(:bid).to(:name).operator(:less_than) }
333
+
334
+ end
@@ -0,0 +1,73 @@
1
+ require 'spec_helper'
2
+
3
+ describe HexValidator do
4
+
5
+ context "has a valid value" do
6
+ let(:klass) do
7
+ Class.new do
8
+ include ActiveModel::Validations
9
+ attr_accessor :color, :name
10
+ validates :color, hex: true
11
+ end
12
+ end
13
+
14
+ subject { klass.new }
15
+
16
+ it { should allow_value("#aaa").for(:color) }
17
+ it { should allow_value("#AAA").for(:color) }
18
+ it { should allow_value("#aaaaaa").for(:color) }
19
+ it { should allow_value("#AAAAAA").for(:color) }
20
+ it { should allow_value("#999").for(:color) }
21
+ it { should allow_value("#999999").for(:color) }
22
+ it { should allow_value("#a9a").for(:color) }
23
+ it { should allow_value("#A9A").for(:color) }
24
+ it { should allow_value("#a9a9a9").for(:color) }
25
+ it { should allow_value("#A9A9A9").for(:color) }
26
+ it { should allow_value("aaa").for(:color) }
27
+ it { should allow_value("AAA").for(:color) }
28
+ it { should allow_value("aaaaaa").for(:color) }
29
+ it { should allow_value("AAAAAA").for(:color) }
30
+ it { should allow_value("999").for(:color) }
31
+ it { should allow_value("999999").for(:color) }
32
+ it { should allow_value("a9a").for(:color) }
33
+ it { should allow_value("A9A").for(:color) }
34
+ it { should allow_value("a9a9a9").for(:color) }
35
+ it { should allow_value("A9A9A9").for(:color) }
36
+
37
+ it { should_not allow_value('').for(:color) }
38
+ it { should_not allow_value(' ').for(:color) }
39
+ it { should_not allow_value(nil).for(:color) }
40
+ it { should_not allow_value("#").for(:color) }
41
+ it { should_not allow_value("#9").for(:color) }
42
+ it { should_not allow_value("#9a").for(:color) }
43
+ it { should_not allow_value("#9A").for(:color) }
44
+ it { should_not allow_value("#hhh").for(:color) }
45
+ it { should_not allow_value("#HHH").for(:color) }
46
+ it { should_not allow_value("#9h9").for(:color) }
47
+ it { should_not allow_value("#9H9").for(:color) }
48
+ it { should_not allow_value("#9a9a").for(:color) }
49
+ it { should_not allow_value("#9A9A").for(:color) }
50
+ it { should_not allow_value("#9a9a9").for(:color) }
51
+ it { should_not allow_value("#9A9A9").for(:color) }
52
+ it { should_not allow_value("#9a9a9a9").for(:color) }
53
+ it { should_not allow_value("#9A9A9A9").for(:color) }
54
+ it { should_not allow_value(" #9a9").for(:color) }
55
+ it { should_not allow_value(" #9A9").for(:color) }
56
+ it { should_not allow_value(" #9a9 ").for(:color) }
57
+ it { should_not allow_value(" #9A9 ").for(:color) }
58
+ it { should_not allow_value("#9a9 ").for(:color) }
59
+ it { should_not allow_value("#9A9 ").for(:color) }
60
+ it { should_not allow_value(" 9a9").for(:color) }
61
+ it { should_not allow_value(" 9A9").for(:color) }
62
+ it { should_not allow_value(" 9a9 ").for(:color) }
63
+ it { should_not allow_value(" 9A9 ").for(:color) }
64
+ it { should_not allow_value("9a9 ").for(:color) }
65
+ it { should_not allow_value("9A9 ").for(:color) }
66
+ it { should_not allow_value("! \#$%\`|").for(:color) }
67
+ it { should_not allow_value("<>@[]\`|").for(:color) }
68
+
69
+ it { should ensure_valid_hex_format_of(:color) }
70
+ it { should_not ensure_valid_hex_format_of(:name) }
71
+ end
72
+
73
+ end
@@ -0,0 +1,41 @@
1
+ require 'spec_helper'
2
+
3
+ describe ImeiValidator do
4
+
5
+ context "has valid format" do
6
+ let(:klass) do
7
+ Class.new do
8
+ include ActiveModel::Validations
9
+ attr_accessor :imei, :name
10
+ validates :imei, imei: true
11
+ end
12
+ end
13
+
14
+ subject { klass.new }
15
+
16
+ it { should allow_value(356843052637512).for(:imei) }
17
+ it { should allow_value("356843052637512").for(:imei) }
18
+ it { should allow_value("35-684305-2637512").for(:imei) }
19
+ it { should allow_value("35-684305.263.7512").for(:imei) }
20
+
21
+ context "value too short" do
22
+ it { should_not allow_value("3568430537512").for(:imei) }
23
+ it { should_not allow_value("3").for(:imei) }
24
+ end
25
+
26
+ it "can't be too long" do
27
+ should_not allow_value("35684305263751233").for(:imei)
28
+ end
29
+
30
+ context "checksum doesn't match" do
31
+ it { should_not allow_value("356843052637513").for(:imei) }
32
+ it { should_not allow_value("156843052637512").for(:imei) }
33
+ end
34
+
35
+ it { should_not allow_value("invalid").for(:imei) }
36
+
37
+ it { should ensure_valid_imei_format_of(:imei) }
38
+ it { should_not ensure_valid_imei_format_of(:name) }
39
+ end
40
+
41
+ end
@@ -0,0 +1,33 @@
1
+ require 'spec_helper'
2
+
3
+ describe IpValidator do
4
+
5
+ context "has a valid value" do
6
+ let(:klass) do
7
+ Class.new do
8
+ include ActiveModel::Validations
9
+ attr_accessor :ip, :name
10
+ validates :ip, ip: true
11
+ end
12
+ end
13
+
14
+ subject { klass.new }
15
+
16
+ it { should allow_value("0.0.0.0").for(:ip) }
17
+ it { should allow_value("127.0.0.1").for(:ip) }
18
+ it { should allow_value("99.39.240.31").for(:ip) }
19
+
20
+ it { should_not allow_value('').for(:ip) }
21
+ it { should_not allow_value(' ').for(:ip) }
22
+ it { should_not allow_value(nil).for(:ip) }
23
+ it { should_not allow_value("0 0 0 0").for(:ip) }
24
+ it { should_not allow_value("0.0.0.0:3000").for(:ip) }
25
+ it { should_not allow_value("22.22.333.22").for(:ip) }
26
+ it { should_not allow_value("! \#$%\`|").for(:ip) }
27
+ it { should_not allow_value("<>@[]\`|").for(:ip) }
28
+
29
+ it { should ensure_valid_ip_format_of(:ip) }
30
+ it { should_not ensure_valid_ip_format_of(:name) }
31
+ end
32
+
33
+ end