flash_validators 0.0.1 → 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.travis.yml +2 -3
- data/README.md +483 -232
- data/config/locales/en.yml +12 -0
- data/lib/flash_validators.rb +6 -0
- data/lib/flash_validators/matchers/ensure_valid_alpha_format_of.rb +18 -0
- data/lib/flash_validators/matchers/ensure_valid_alpha_numeric_format_of.rb +18 -0
- data/lib/flash_validators/matchers/ensure_valid_boolean_format_of.rb +2 -4
- data/lib/flash_validators/matchers/ensure_valid_credit_card_format_of.rb +18 -0
- data/lib/flash_validators/matchers/ensure_valid_currency_format_of.rb +2 -4
- data/lib/flash_validators/matchers/ensure_valid_email_format_of.rb +2 -4
- data/lib/flash_validators/matchers/ensure_valid_equality_matcher_of.rb +2 -4
- data/lib/flash_validators/matchers/ensure_valid_hex_format_of.rb +2 -4
- data/lib/flash_validators/matchers/ensure_valid_imei_format_of.rb +2 -4
- data/lib/flash_validators/matchers/ensure_valid_ip_format_of.rb +2 -4
- data/lib/flash_validators/matchers/ensure_valid_latitude_format_of.rb +2 -4
- data/lib/flash_validators/matchers/ensure_valid_longitude_format_of.rb +2 -4
- data/lib/flash_validators/matchers/ensure_valid_mac_address_format_of.rb +2 -4
- data/lib/flash_validators/matchers/ensure_valid_name_format_of.rb +2 -4
- data/lib/flash_validators/matchers/ensure_valid_password_format_of.rb +2 -4
- data/lib/flash_validators/matchers/ensure_valid_phone_format_of.rb +2 -4
- data/lib/flash_validators/matchers/ensure_valid_slug_format_of.rb +2 -4
- data/lib/flash_validators/matchers/ensure_valid_ssn_format_of.rb +2 -4
- data/lib/flash_validators/matchers/ensure_valid_url_format_of.rb +2 -4
- data/lib/flash_validators/matchers/ensure_valid_username_format_of.rb +2 -4
- data/lib/flash_validators/validators/alpha_numeric_validator.rb +17 -0
- data/lib/flash_validators/validators/alpha_validator.rb +19 -0
- data/lib/flash_validators/validators/credit_card_validator.rb +25 -0
- data/lib/flash_validators/validators/password_validator.rb +9 -1
- data/lib/flash_validators/version.rb +2 -2
- data/spec/lib/alpha_numeric_validator_spec.rb +61 -0
- data/spec/lib/alpha_validator_spec.rb +120 -0
- data/spec/lib/credit_card_validator_spec.rb +316 -0
- data/spec/lib/currency_validator_spec.rb +1 -1
- data/spec/lib/password_validator_spec.rb +42 -0
- metadata +15 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b467f38532ba8c73ce3756b5f63b4990b545b06a
|
4
|
+
data.tar.gz: 5c2b50e04ea7e771b4a98cc2b783320fccad5f74
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 16fd40d55f1dc9e8a420ce88a7e4225608946894dccf85b665d50ed284382b46215c1892ce39ded908a31699ca4c71eca356444d6583c82c15505c0247b0ccbf
|
7
|
+
data.tar.gz: 5f1099472b2a4b7c651143b8bc643cbbc18bc961846f68c2b45fd107a9be832bf2c256b6581aefbc67ee71bd336551f691a618d7684027357de3b455a6df1400
|
data/.travis.yml
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
before_install:
|
2
|
+
- sudo apt-get install libxml2-dev
|
1
3
|
cache: bundler
|
2
4
|
language: ruby
|
3
5
|
notifications:
|
@@ -7,8 +9,5 @@ notifications:
|
|
7
9
|
on_failure: change
|
8
10
|
on_success: never
|
9
11
|
rvm:
|
10
|
-
- '2.1.0'
|
11
|
-
- '2.0.0'
|
12
|
-
- '1.9.3'
|
13
12
|
- ruby-head
|
14
13
|
script: 'bundle exec rake'
|
data/README.md
CHANGED
@@ -6,7 +6,7 @@
|
|
6
6
|
|
7
7
|
Flash Validators is a collection of custom validators that are often required in Rails applications plus shoulda-style RSpec matchers to test the validation rules.
|
8
8
|
|
9
|
-
Currently supported validators: boolean, currency, email, equality, hex, IMEI, IP, latitude, longitude, mac address, name, password, phone, slug, ssn, url, and username.
|
9
|
+
Currently supported validators: alpha, alpha-numeric, boolean, credit card, currency, email, equality, hex, IMEI, IP, latitude, longitude, mac address, name, password, phone, slug, ssn, url, and username.
|
10
10
|
|
11
11
|
Highly recommended validators:
|
12
12
|
* **DateTime:** Validates Timeliness - https://github.com/adzap/validates_timeliness
|
@@ -30,6 +30,92 @@ Or install it yourself as:
|
|
30
30
|
|
31
31
|
## Usage
|
32
32
|
|
33
|
+
### AlphaValidator
|
34
|
+
|
35
|
+
**Ex:** Example or Example Title
|
36
|
+
|
37
|
+
**Rules:**
|
38
|
+
* Characters: A-Z a-z
|
39
|
+
* Must include: A-Z a-z
|
40
|
+
|
41
|
+
With an ActiveRecord model:
|
42
|
+
|
43
|
+
```ruby
|
44
|
+
class Book < ActiveRecord::Base
|
45
|
+
attr_accessor :title, :name
|
46
|
+
validates :title, alpha: true
|
47
|
+
end
|
48
|
+
```
|
49
|
+
|
50
|
+
Or any ruby class:
|
51
|
+
|
52
|
+
```ruby
|
53
|
+
class Book
|
54
|
+
include ActiveModel::Validations
|
55
|
+
attr_accessor :title, :name
|
56
|
+
validates :title, alpha: true
|
57
|
+
end
|
58
|
+
```
|
59
|
+
|
60
|
+
Options: :strict, :lowercase, :uppercase
|
61
|
+
|
62
|
+
```ruby
|
63
|
+
validates :title, alpha: { strict: true }
|
64
|
+
validates :title, alpha: { lowercase: true }
|
65
|
+
validates :title, alpha: { uppsercase: true }
|
66
|
+
```
|
67
|
+
|
68
|
+
RSpec matcher is also available for your convenience:
|
69
|
+
|
70
|
+
```ruby
|
71
|
+
describe Product do
|
72
|
+
it { should ensure_valid_alpha_format_of(:title) }
|
73
|
+
it { should_not ensure_valid_alpha_format_of(:name) }
|
74
|
+
end
|
75
|
+
```
|
76
|
+
|
77
|
+
### AlphaNumericValidator
|
78
|
+
|
79
|
+
**Ex:** Example1 or Example Title 1
|
80
|
+
|
81
|
+
**Rules:**
|
82
|
+
* Characters: A-Z a-z 0-9
|
83
|
+
* Must include: A-Z a-z 0-9
|
84
|
+
|
85
|
+
With an ActiveRecord model:
|
86
|
+
|
87
|
+
```ruby
|
88
|
+
class Book < ActiveRecord::Base
|
89
|
+
attr_accessor :title, :name
|
90
|
+
validates :title, alpha_numeric: true
|
91
|
+
end
|
92
|
+
```
|
93
|
+
|
94
|
+
Or any ruby class:
|
95
|
+
|
96
|
+
```ruby
|
97
|
+
class Book
|
98
|
+
include ActiveModel::Validations
|
99
|
+
attr_accessor :title, :name
|
100
|
+
validates :title, alpha_numeric: true
|
101
|
+
end
|
102
|
+
```
|
103
|
+
|
104
|
+
Strict: requires not including spaces
|
105
|
+
|
106
|
+
```ruby
|
107
|
+
validates :title, alpha_numeric: { strict: true }
|
108
|
+
```
|
109
|
+
|
110
|
+
RSpec matcher is also available for your convenience:
|
111
|
+
|
112
|
+
```ruby
|
113
|
+
describe Product do
|
114
|
+
it { should ensure_valid_alpha_numeric_format_of(:title) }
|
115
|
+
it { should_not ensure_valid_alpha_numeric_format_of(:name) }
|
116
|
+
end
|
117
|
+
```
|
118
|
+
|
33
119
|
### BooleanValidator
|
34
120
|
|
35
121
|
**Ex:** true or false or 1 or 0
|
@@ -39,25 +125,78 @@ Or install it yourself as:
|
|
39
125
|
|
40
126
|
With an ActiveRecord model:
|
41
127
|
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
128
|
+
```ruby
|
129
|
+
class User < ActiveRecord::Base
|
130
|
+
attr_accessor :active, :name
|
131
|
+
validates :active, boolean: true
|
132
|
+
end
|
133
|
+
```
|
134
|
+
|
135
|
+
Or any ruby class:
|
136
|
+
|
137
|
+
```ruby
|
138
|
+
class User
|
139
|
+
include ActiveModel::Validations
|
140
|
+
attr_accessor :active, :name
|
141
|
+
validates :active, boolean: true
|
142
|
+
end
|
143
|
+
```
|
144
|
+
|
145
|
+
RSpec matcher is also available for your convenience:
|
146
|
+
|
147
|
+
```ruby
|
148
|
+
describe Product do
|
149
|
+
it { should ensure_valid_boolean_format_of(:active) }
|
150
|
+
it { should_not ensure_valid_boolean_format_of(:name) }
|
151
|
+
end
|
152
|
+
```
|
153
|
+
### CreditCardValidator
|
154
|
+
|
155
|
+
**Ex:** 370000000000002
|
156
|
+
|
157
|
+
**Rules:**
|
158
|
+
* Characters: 0-9
|
159
|
+
* Must include: 0-9
|
160
|
+
* Range for card digits: 15-16
|
161
|
+
|
162
|
+
With an ActiveRecord model:
|
163
|
+
|
164
|
+
```ruby
|
165
|
+
class Invoice < ActiveRecord::Base
|
166
|
+
attr_accessor :cc_number, :name
|
167
|
+
validates :cc_number, credit_card: true
|
168
|
+
end
|
169
|
+
```
|
46
170
|
|
47
171
|
Or any ruby class:
|
48
172
|
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
173
|
+
```ruby
|
174
|
+
class Invoice
|
175
|
+
include ActiveModel::Validations
|
176
|
+
attr_accessor :cc_number, :name
|
177
|
+
validates :cc_number, credit_card: true
|
178
|
+
end
|
179
|
+
```
|
180
|
+
|
181
|
+
Options: :american_express, :diners_club, :discover, :jbc, :master_card, :visa
|
182
|
+
|
183
|
+
```ruby
|
184
|
+
validates :cc_number, currency: { american_express: true }
|
185
|
+
validates :cc_number, currency: { diners_club: true }
|
186
|
+
validates :cc_number, currency: { discover: true }
|
187
|
+
validates :cc_number, currency: { jbc: true }
|
188
|
+
validates :cc_number, currency: { master_card: true }
|
189
|
+
validates :cc_number, currency: { visa: true }
|
190
|
+
```
|
54
191
|
|
55
192
|
RSpec matcher is also available for your convenience:
|
56
193
|
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
194
|
+
```ruby
|
195
|
+
describe Invoice do
|
196
|
+
it { should ensure_valid_credit_card_format_of(:cc_number) }
|
197
|
+
it { should_not ensure_valid_credit_card_format_of(:name) }
|
198
|
+
end
|
199
|
+
```
|
61
200
|
|
62
201
|
### CurrencyValidator
|
63
202
|
|
@@ -70,29 +209,37 @@ RSpec matcher is also available for your convenience:
|
|
70
209
|
|
71
210
|
With an ActiveRecord model:
|
72
211
|
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
212
|
+
```ruby
|
213
|
+
class Product < ActiveRecord::Base
|
214
|
+
attr_accessor :price, :name
|
215
|
+
validates :price, currency: true
|
216
|
+
end
|
217
|
+
```
|
77
218
|
|
78
219
|
Or any ruby class:
|
79
220
|
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
221
|
+
```ruby
|
222
|
+
class Product
|
223
|
+
include ActiveModel::Validations
|
224
|
+
attr_accessor :price, :name
|
225
|
+
validates :price, currency: true
|
226
|
+
end
|
227
|
+
```
|
85
228
|
|
86
229
|
Strict: requires leading number and exactly two decimals, 1.45
|
87
230
|
|
88
|
-
|
231
|
+
```ruby
|
232
|
+
validates :price, currency: { strict: true }
|
233
|
+
```
|
89
234
|
|
90
235
|
RSpec matcher is also available for your convenience:
|
91
236
|
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
237
|
+
```ruby
|
238
|
+
describe Product do
|
239
|
+
it { should ensure_valid_currency_format_of(:price) }
|
240
|
+
it { should_not ensure_valid_currency_format_of(:name) }
|
241
|
+
end
|
242
|
+
```
|
96
243
|
|
97
244
|
### EmailValidator
|
98
245
|
|
@@ -106,31 +253,39 @@ RSpec matcher is also available for your convenience:
|
|
106
253
|
|
107
254
|
With an ActiveRecord model:
|
108
255
|
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
256
|
+
```ruby
|
257
|
+
class User < ActiveRecord::Base
|
258
|
+
attr_accessor :email, :name
|
259
|
+
validates :email, email: true
|
260
|
+
end
|
261
|
+
```
|
113
262
|
|
114
263
|
Or any ruby class:
|
115
264
|
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
265
|
+
```ruby
|
266
|
+
class User
|
267
|
+
include ActiveModel::Validations
|
268
|
+
attr_accessor :email, :name
|
269
|
+
validates :email, email: true
|
270
|
+
end
|
271
|
+
```
|
121
272
|
|
122
273
|
You can specify domains to which the email domain should belong in one of the folowing ways:
|
123
274
|
|
124
|
-
|
125
|
-
|
126
|
-
|
275
|
+
```ruby
|
276
|
+
validates :email, email: { domains: 'com' }
|
277
|
+
validates :email, email: { domains: :com }
|
278
|
+
validates :email, email: { domains: [:com, 'edu'] }
|
279
|
+
```
|
127
280
|
|
128
281
|
RSpec matcher is also available for your convenience:
|
129
282
|
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
283
|
+
```ruby
|
284
|
+
describe User do
|
285
|
+
it { should ensure_valid_email_format_of(:email) }
|
286
|
+
it { should_not ensure_valid_email_format_of(:name) }
|
287
|
+
end
|
288
|
+
```
|
134
289
|
|
135
290
|
### EqualityValidator
|
136
291
|
|
@@ -148,25 +303,31 @@ RSpec matcher is also available for your convenience:
|
|
148
303
|
|
149
304
|
With an ActiveRecord model:
|
150
305
|
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
306
|
+
```ruby
|
307
|
+
class Auction < ActiveRecord::Base
|
308
|
+
attr_accessor :bid, :price, :product
|
309
|
+
validates :bid, equality: { operator: :greater_than_or_equal_to, to: :price }
|
310
|
+
end
|
311
|
+
```
|
155
312
|
|
156
313
|
Or any ruby class:
|
157
314
|
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
315
|
+
```ruby
|
316
|
+
class Auction
|
317
|
+
include ActiveModel::Validations
|
318
|
+
attr_accessor :bid, :price, :product
|
319
|
+
validates :bid, equality: { operator: :greater_than_or_equal_to, to: :price }
|
320
|
+
end
|
321
|
+
```
|
163
322
|
|
164
323
|
RSpec matcher is also available for your convenience:
|
165
324
|
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
325
|
+
```ruby
|
326
|
+
describe Auction do
|
327
|
+
it { should ensure_equality_of(:bid).to(:price) }
|
328
|
+
it { should_not ensure_equality_of(:bid).to(:product) }
|
329
|
+
end
|
330
|
+
```
|
170
331
|
|
171
332
|
### HexValidator
|
172
333
|
|
@@ -179,25 +340,31 @@ RSpec matcher is also available for your convenience:
|
|
179
340
|
|
180
341
|
With an ActiveRecord model:
|
181
342
|
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
|
343
|
+
```ruby
|
344
|
+
class Profile < ActiveRecord::Base
|
345
|
+
attr_accessor :color, :trim
|
346
|
+
validates :color, hex: true
|
347
|
+
end
|
348
|
+
```
|
186
349
|
|
187
350
|
Or any ruby class:
|
188
351
|
|
189
|
-
|
190
|
-
|
191
|
-
|
192
|
-
|
193
|
-
|
352
|
+
```ruby
|
353
|
+
class Profile
|
354
|
+
include ActiveModel::Validations
|
355
|
+
attr_accessor :color, :trim
|
356
|
+
validates :color, hex: true
|
357
|
+
end
|
358
|
+
```
|
194
359
|
|
195
360
|
RSpec matcher is also available for your convenience:
|
196
361
|
|
197
|
-
|
198
|
-
|
199
|
-
|
200
|
-
|
362
|
+
```ruby
|
363
|
+
describe Color do
|
364
|
+
it { should ensure_valid_hex_format_of(:color) }
|
365
|
+
it { should_not ensure_valid_hex_format_of(:trim) }
|
366
|
+
end
|
367
|
+
```
|
201
368
|
|
202
369
|
### ImeiValidator
|
203
370
|
|
@@ -209,25 +376,31 @@ RSpec matcher is also available for your convenience:
|
|
209
376
|
|
210
377
|
With an ActiveRecord model:
|
211
378
|
|
212
|
-
|
213
|
-
|
214
|
-
|
215
|
-
|
379
|
+
```ruby
|
380
|
+
class User < ActiveRecord::Base
|
381
|
+
attr_accessor :imei, :name
|
382
|
+
validates :imei, imei: true
|
383
|
+
end
|
384
|
+
```
|
216
385
|
|
217
386
|
Or any ruby class:
|
218
387
|
|
219
|
-
|
220
|
-
|
221
|
-
|
222
|
-
|
223
|
-
|
388
|
+
```ruby
|
389
|
+
class User
|
390
|
+
include ActiveModel::Validations
|
391
|
+
attr_accessor :imei, :name
|
392
|
+
validates :imei, imei: true
|
393
|
+
end
|
394
|
+
```
|
224
395
|
|
225
396
|
RSpec matcher is also available for your convenience:
|
226
397
|
|
227
|
-
|
228
|
-
|
229
|
-
|
230
|
-
|
398
|
+
```ruby
|
399
|
+
describe User do
|
400
|
+
it { should ensure_valid_imei_format_of(:imei) }
|
401
|
+
it { should_not ensure_valid_imei_format_of(:name) }
|
402
|
+
end
|
403
|
+
```
|
231
404
|
|
232
405
|
### IpValidator
|
233
406
|
|
@@ -239,25 +412,31 @@ RSpec matcher is also available for your convenience:
|
|
239
412
|
|
240
413
|
With an ActiveRecord model:
|
241
414
|
|
242
|
-
|
243
|
-
|
244
|
-
|
245
|
-
|
415
|
+
```ruby
|
416
|
+
class User < ActiveRecord::Base
|
417
|
+
attr_accessor :ip, :name
|
418
|
+
validates :ip, ip: true
|
419
|
+
end
|
420
|
+
```
|
246
421
|
|
247
422
|
Or any ruby class:
|
248
423
|
|
249
|
-
|
250
|
-
|
251
|
-
|
252
|
-
|
253
|
-
|
424
|
+
```ruby
|
425
|
+
class User
|
426
|
+
include ActiveModel::Validations
|
427
|
+
attr_accessor :ip, :name
|
428
|
+
validates :ip, ip: true
|
429
|
+
end
|
430
|
+
```
|
254
431
|
|
255
432
|
RSpec matcher is also available for your convenience:
|
256
433
|
|
257
|
-
|
258
|
-
|
259
|
-
|
260
|
-
|
434
|
+
```ruby
|
435
|
+
describe User do
|
436
|
+
it { should ensure_valid_ip_format_of(:ip) }
|
437
|
+
it { should_not ensure_valid_ip_format_of(:name) }
|
438
|
+
end
|
439
|
+
```
|
261
440
|
|
262
441
|
### LatitudeValidator
|
263
442
|
|
@@ -269,25 +448,31 @@ RSpec matcher is also available for your convenience:
|
|
269
448
|
|
270
449
|
With an ActiveRecord model:
|
271
450
|
|
272
|
-
|
273
|
-
|
274
|
-
|
275
|
-
|
451
|
+
```ruby
|
452
|
+
class Location < ActiveRecord::Base
|
453
|
+
attr_accessor :lat, :name
|
454
|
+
validates :lat, latitude: true
|
455
|
+
end
|
456
|
+
```
|
276
457
|
|
277
458
|
Or any ruby class:
|
278
459
|
|
279
|
-
|
280
|
-
|
281
|
-
|
282
|
-
|
283
|
-
|
460
|
+
```ruby
|
461
|
+
class Location
|
462
|
+
include ActiveModel::Validations
|
463
|
+
attr_accessor :lat, :name
|
464
|
+
validates :lat, latitude: true
|
465
|
+
end
|
466
|
+
```
|
284
467
|
|
285
468
|
RSpec matcher is also available for your convenience:
|
286
469
|
|
287
|
-
|
288
|
-
|
289
|
-
|
290
|
-
|
470
|
+
```ruby
|
471
|
+
describe Location do
|
472
|
+
it { should ensure_valid_latitude_format_of(:lat) }
|
473
|
+
it { should_not ensure_valid_latitude_format_of(:name) }
|
474
|
+
end
|
475
|
+
```
|
291
476
|
|
292
477
|
### LongitudeValidator
|
293
478
|
|
@@ -299,25 +484,31 @@ RSpec matcher is also available for your convenience:
|
|
299
484
|
|
300
485
|
With an ActiveRecord model:
|
301
486
|
|
302
|
-
|
303
|
-
|
304
|
-
|
305
|
-
|
487
|
+
```ruby
|
488
|
+
class Location < ActiveRecord::Base
|
489
|
+
attr_accessor :lon, :name
|
490
|
+
validates :lon, longitude: true
|
491
|
+
end
|
492
|
+
```
|
306
493
|
|
307
494
|
Or any ruby class:
|
308
495
|
|
309
|
-
|
310
|
-
|
311
|
-
|
312
|
-
|
313
|
-
|
496
|
+
```ruby
|
497
|
+
class Location
|
498
|
+
include ActiveModel::Validations
|
499
|
+
attr_accessor :lon, :name
|
500
|
+
validates :lon, longitude: true
|
501
|
+
end
|
502
|
+
```
|
314
503
|
|
315
504
|
RSpec matcher is also available for your convenience:
|
316
505
|
|
317
|
-
|
318
|
-
|
319
|
-
|
320
|
-
|
506
|
+
```ruby
|
507
|
+
describe Location do
|
508
|
+
it { should ensure_valid_longitude_format_of(:lon) }
|
509
|
+
it { should_not ensure_valid_longitude_format_of(:name) }
|
510
|
+
end
|
511
|
+
```
|
321
512
|
|
322
513
|
### MacAddressValidator
|
323
514
|
|
@@ -334,25 +525,31 @@ RSpec matcher is also available for your convenience:
|
|
334
525
|
|
335
526
|
With an ActiveRecord model:
|
336
527
|
|
337
|
-
|
338
|
-
|
339
|
-
|
340
|
-
|
528
|
+
```ruby
|
529
|
+
class Device < ActiveRecord::Base
|
530
|
+
attr_accessor :mac, :name
|
531
|
+
validates :mac, mac_address: true
|
532
|
+
end
|
533
|
+
```
|
341
534
|
|
342
535
|
Or any ruby class:
|
343
536
|
|
344
|
-
|
345
|
-
|
346
|
-
|
347
|
-
|
348
|
-
|
537
|
+
```ruby
|
538
|
+
class Device
|
539
|
+
include ActiveModel::Validations
|
540
|
+
attr_accessor :mac, :name
|
541
|
+
validates :mac, mac_address: true
|
542
|
+
end
|
543
|
+
```
|
349
544
|
|
350
545
|
RSpec matcher is also available for your convenience:
|
351
546
|
|
352
|
-
|
353
|
-
|
354
|
-
|
355
|
-
|
547
|
+
```ruby
|
548
|
+
describe Device do
|
549
|
+
it { should ensure_valid_mac_address_format_of }
|
550
|
+
it { should_not ensure_valid_mac_address_format_of(:name) }
|
551
|
+
end
|
552
|
+
```
|
356
553
|
|
357
554
|
### NameValidator
|
358
555
|
|
@@ -365,25 +562,31 @@ RSpec matcher is also available for your convenience:
|
|
365
562
|
|
366
563
|
With an ActiveRecord model:
|
367
564
|
|
368
|
-
|
369
|
-
|
370
|
-
|
371
|
-
|
565
|
+
```ruby
|
566
|
+
class User < ActiveRecord::Base
|
567
|
+
attr_accessor :name, :email
|
568
|
+
validates :name, name: true
|
569
|
+
end
|
570
|
+
```
|
372
571
|
|
373
572
|
Or any ruby class:
|
374
573
|
|
375
|
-
|
376
|
-
|
377
|
-
|
378
|
-
|
379
|
-
|
574
|
+
```ruby
|
575
|
+
class User
|
576
|
+
include ActiveModel::Validations
|
577
|
+
attr_accessor :name, :email
|
578
|
+
validates :name, name: true
|
579
|
+
end
|
580
|
+
```
|
380
581
|
|
381
582
|
RSpec matcher is also available for your convenience:
|
382
583
|
|
383
|
-
|
384
|
-
|
385
|
-
|
386
|
-
|
584
|
+
```ruby
|
585
|
+
describe User do
|
586
|
+
it { should ensure_valid_name_format_of(:name) }
|
587
|
+
it { should_not ensure_valid_name_format_of(:email) }
|
588
|
+
end
|
589
|
+
```
|
387
590
|
|
388
591
|
### PasswordValidator
|
389
592
|
|
@@ -395,25 +598,37 @@ RSpec matcher is also available for your convenience:
|
|
395
598
|
|
396
599
|
With an ActiveRecord model:
|
397
600
|
|
398
|
-
|
399
|
-
|
400
|
-
|
401
|
-
|
601
|
+
```ruby
|
602
|
+
class User < ActiveRecord::Base
|
603
|
+
attr_accessor :password, :name
|
604
|
+
validates :password, password: true
|
605
|
+
end
|
606
|
+
```
|
402
607
|
|
403
608
|
Or any ruby class:
|
404
609
|
|
405
|
-
|
406
|
-
|
407
|
-
|
408
|
-
|
409
|
-
|
610
|
+
```ruby
|
611
|
+
class User
|
612
|
+
include ActiveModel::Validations
|
613
|
+
attr_accessor :password, :name
|
614
|
+
validates :password, name: true
|
615
|
+
end
|
616
|
+
```
|
617
|
+
|
618
|
+
Strict: requires length between 6 and 18, one number, lowercase, upcase letter
|
619
|
+
|
620
|
+
```ruby
|
621
|
+
validates :password, password: { strict: true }
|
622
|
+
```
|
410
623
|
|
411
624
|
RSpec matcher is also available for your convenience:
|
412
625
|
|
413
|
-
|
414
|
-
|
415
|
-
|
416
|
-
|
626
|
+
```ruby
|
627
|
+
describe User do
|
628
|
+
it { should ensure_valid_password_format_of(:password) }
|
629
|
+
it { should_not ensure_valid_password_format_of(:name) }
|
630
|
+
end
|
631
|
+
```
|
417
632
|
|
418
633
|
### PhoneValidator
|
419
634
|
|
@@ -424,25 +639,31 @@ RSpec matcher is also available for your convenience:
|
|
424
639
|
|
425
640
|
With an ActiveRecord model:
|
426
641
|
|
427
|
-
|
428
|
-
|
429
|
-
|
430
|
-
|
642
|
+
```ruby
|
643
|
+
class User < ActiveRecord::Base
|
644
|
+
attr_accessor :phone, :name
|
645
|
+
validates :phone, phone: true
|
646
|
+
end
|
647
|
+
```
|
431
648
|
|
432
649
|
Or any ruby class:
|
433
650
|
|
434
|
-
|
435
|
-
|
436
|
-
|
437
|
-
|
438
|
-
|
651
|
+
```ruby
|
652
|
+
class User
|
653
|
+
include ActiveModel::Validations
|
654
|
+
attr_accessor :phone, :name
|
655
|
+
validates :phone, phone: true
|
656
|
+
end
|
657
|
+
```
|
439
658
|
|
440
659
|
RSpec matcher is also available for your convenience:
|
441
660
|
|
442
|
-
|
443
|
-
|
444
|
-
|
445
|
-
|
661
|
+
```ruby
|
662
|
+
describe User do
|
663
|
+
it { should ensure_valid_phone_format_of(:phone) }
|
664
|
+
it { should_not ensure_valid_phone_format_of(:name) }
|
665
|
+
end
|
666
|
+
```
|
446
667
|
|
447
668
|
### SlugValidator
|
448
669
|
|
@@ -453,25 +674,31 @@ RSpec matcher is also available for your convenience:
|
|
453
674
|
|
454
675
|
With an ActiveRecord model:
|
455
676
|
|
456
|
-
|
457
|
-
|
458
|
-
|
459
|
-
|
677
|
+
```ruby
|
678
|
+
class User < ActiveRecord::Base
|
679
|
+
attr_accessor :slug, :name
|
680
|
+
validates :slug, slug: true
|
681
|
+
end
|
682
|
+
```
|
460
683
|
|
461
684
|
Or any ruby class:
|
462
685
|
|
463
|
-
|
464
|
-
|
465
|
-
|
466
|
-
|
467
|
-
|
686
|
+
```ruby
|
687
|
+
class User
|
688
|
+
include ActiveModel::Validations
|
689
|
+
attr_accessor :slug, :name
|
690
|
+
validates :slug, slug: true
|
691
|
+
end
|
692
|
+
```
|
468
693
|
|
469
694
|
RSpec matcher is also available for your convenience:
|
470
695
|
|
471
|
-
|
472
|
-
|
473
|
-
|
474
|
-
|
696
|
+
```ruby
|
697
|
+
describe User do
|
698
|
+
it { should ensure_valid_slug_format_of(:slug) }
|
699
|
+
it { should_not ensure_valid_slug_format_of(:name) }
|
700
|
+
end
|
701
|
+
```
|
475
702
|
|
476
703
|
### SsnValidator
|
477
704
|
|
@@ -482,25 +709,31 @@ RSpec matcher is also available for your convenience:
|
|
482
709
|
|
483
710
|
With an ActiveRecord model:
|
484
711
|
|
485
|
-
|
486
|
-
|
487
|
-
|
488
|
-
|
712
|
+
```ruby
|
713
|
+
class User < ActiveRecord::Base
|
714
|
+
attr_accessor :ssn, :name
|
715
|
+
validates :ssn, ssn: true
|
716
|
+
end
|
717
|
+
```
|
489
718
|
|
490
719
|
Or any ruby class:
|
491
720
|
|
492
|
-
|
493
|
-
|
494
|
-
|
495
|
-
|
496
|
-
|
721
|
+
```ruby
|
722
|
+
class User
|
723
|
+
include ActiveModel::Validations
|
724
|
+
attr_accessor :ssn, :name
|
725
|
+
validates :ssn, ssn: true
|
726
|
+
end
|
727
|
+
```
|
497
728
|
|
498
729
|
RSpec matcher is also available for your convenience:
|
499
730
|
|
500
|
-
|
501
|
-
|
502
|
-
|
503
|
-
|
731
|
+
```ruby
|
732
|
+
describe User do
|
733
|
+
it { should ensure_valid_ssn_format_of(:ssn) }
|
734
|
+
it { should_not ensure_valid_ssn_format_of(:name) }
|
735
|
+
end
|
736
|
+
```
|
504
737
|
|
505
738
|
### UrlValidator
|
506
739
|
|
@@ -513,40 +746,52 @@ RSpec matcher is also available for your convenience:
|
|
513
746
|
|
514
747
|
With an ActiveRecord model:
|
515
748
|
|
516
|
-
|
517
|
-
|
518
|
-
|
519
|
-
|
749
|
+
```ruby
|
750
|
+
class User < ActiveRecord::Base
|
751
|
+
attr_accessor :url, :name
|
752
|
+
validates :url, url: true
|
753
|
+
end
|
754
|
+
```
|
520
755
|
|
521
756
|
Or any ruby class:
|
522
757
|
|
523
|
-
|
524
|
-
|
525
|
-
|
526
|
-
|
527
|
-
|
758
|
+
```ruby
|
759
|
+
class User
|
760
|
+
include ActiveModel::Validations
|
761
|
+
attr_accessor :url, :name
|
762
|
+
validates :url, url: true
|
763
|
+
end
|
764
|
+
```
|
528
765
|
|
529
766
|
You can specify domains to which the URL domain should belong in one of the folowing ways:
|
530
767
|
|
531
|
-
|
532
|
-
|
533
|
-
|
768
|
+
```ruby
|
769
|
+
validates :url, url: { domains: 'com' }
|
770
|
+
validates :url, url: { domains: :com }
|
771
|
+
validates :url, url: { domains: [:com, 'edu'] }
|
772
|
+
```
|
534
773
|
|
535
774
|
You can specify if the URL should the site root:
|
536
775
|
|
537
|
-
|
776
|
+
```ruby
|
777
|
+
validates :url, url: { root: true }
|
778
|
+
```
|
538
779
|
|
539
780
|
You can specify the URL scheme:
|
540
781
|
|
541
|
-
|
542
|
-
|
782
|
+
```ruby
|
783
|
+
validates :url, url: { scheme: :http }
|
784
|
+
validates :url, url: { scheme: [:http, 'https'] }
|
785
|
+
```
|
543
786
|
|
544
787
|
RSpec matcher is also available for your convenience:
|
545
788
|
|
546
|
-
|
547
|
-
|
548
|
-
|
549
|
-
|
789
|
+
```ruby
|
790
|
+
describe User do
|
791
|
+
it { should ensure_valid_url_format_of(:url) }
|
792
|
+
it { should_not ensure_valid_url_format_of(:name) }
|
793
|
+
end
|
794
|
+
```
|
550
795
|
|
551
796
|
### UsernameValidator
|
552
797
|
|
@@ -558,25 +803,31 @@ RSpec matcher is also available for your convenience:
|
|
558
803
|
|
559
804
|
With an ActiveRecord model:
|
560
805
|
|
561
|
-
|
562
|
-
|
563
|
-
|
564
|
-
|
806
|
+
```ruby
|
807
|
+
class User < ActiveRecord::Base
|
808
|
+
attr_accessor :username, :name
|
809
|
+
validates :username, username: true
|
810
|
+
end
|
811
|
+
```
|
565
812
|
|
566
813
|
Or any ruby class:
|
567
814
|
|
568
|
-
|
569
|
-
|
570
|
-
|
571
|
-
|
572
|
-
|
815
|
+
```ruby
|
816
|
+
class User
|
817
|
+
include ActiveModel::Validations
|
818
|
+
attr_accessor :username, :name
|
819
|
+
validates :username, username: true
|
820
|
+
end
|
821
|
+
```
|
573
822
|
|
574
823
|
RSpec matcher is also available for your convenience:
|
575
824
|
|
576
|
-
|
577
|
-
|
578
|
-
|
579
|
-
|
825
|
+
```ruby
|
826
|
+
describe User do
|
827
|
+
it { should ensure_valid_username_format_of(:username) }
|
828
|
+
it { should_not ensure_valid_username_format_of(:name) }
|
829
|
+
end
|
830
|
+
```
|
580
831
|
|
581
832
|
## Contributing
|
582
833
|
|