active_validation 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.coveralls.yml +1 -0
- data/.gitignore +14 -0
- data/.rspec +4 -0
- data/.travis.yml +13 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +1099 -0
- data/Rakefile +1 -0
- data/active_validation.gemspec +29 -0
- data/config/locales/en.yml +109 -0
- data/lib/active_validation/matchers/ensure_valid_alpha_format_of.rb +26 -0
- data/lib/active_validation/matchers/ensure_valid_alpha_numeric_format_of.rb +26 -0
- data/lib/active_validation/matchers/ensure_valid_base64_format_of.rb +26 -0
- data/lib/active_validation/matchers/ensure_valid_boolean_format_of.rb +26 -0
- data/lib/active_validation/matchers/ensure_valid_credit_card_format_of.rb +26 -0
- data/lib/active_validation/matchers/ensure_valid_currency_format_of.rb +26 -0
- data/lib/active_validation/matchers/ensure_valid_cusip_format_of.rb +26 -0
- data/lib/active_validation/matchers/ensure_valid_email_format_of.rb +26 -0
- data/lib/active_validation/matchers/ensure_valid_equality_matcher_of.rb +40 -0
- data/lib/active_validation/matchers/ensure_valid_hex_format_of.rb +26 -0
- data/lib/active_validation/matchers/ensure_valid_imei_format_of.rb +26 -0
- data/lib/active_validation/matchers/ensure_valid_ip_format_of.rb +26 -0
- data/lib/active_validation/matchers/ensure_valid_isbn_format_of.rb +26 -0
- data/lib/active_validation/matchers/ensure_valid_isin_format_of.rb +26 -0
- data/lib/active_validation/matchers/ensure_valid_latitude_format_of.rb +26 -0
- data/lib/active_validation/matchers/ensure_valid_longitude_format_of.rb +26 -0
- data/lib/active_validation/matchers/ensure_valid_mac_address_format_of.rb +26 -0
- data/lib/active_validation/matchers/ensure_valid_name_format_of.rb +26 -0
- data/lib/active_validation/matchers/ensure_valid_password_format_of.rb +26 -0
- data/lib/active_validation/matchers/ensure_valid_phone_format_of.rb +26 -0
- data/lib/active_validation/matchers/ensure_valid_sedol_format_of.rb +26 -0
- data/lib/active_validation/matchers/ensure_valid_slug_format_of.rb +26 -0
- data/lib/active_validation/matchers/ensure_valid_ssn_format_of.rb +26 -0
- data/lib/active_validation/matchers/ensure_valid_url_format_of.rb +26 -0
- data/lib/active_validation/matchers/ensure_valid_username_format_of.rb +26 -0
- data/lib/active_validation/matchers/ensure_valid_uuid_format_of.rb +26 -0
- data/lib/active_validation/validators/alpha_numeric_validator.rb +30 -0
- data/lib/active_validation/validators/alpha_validator.rb +31 -0
- data/lib/active_validation/validators/base64_validator.rb +9 -0
- data/lib/active_validation/validators/boolean_validator.rb +9 -0
- data/lib/active_validation/validators/credit_card_validator.rb +133 -0
- data/lib/active_validation/validators/currency_validator.rb +23 -0
- data/lib/active_validation/validators/cusip_validator.rb +33 -0
- data/lib/active_validation/validators/email_validator.rb +25 -0
- data/lib/active_validation/validators/equality_validator.rb +27 -0
- data/lib/active_validation/validators/hex_validator.rb +9 -0
- data/lib/active_validation/validators/imei_validator.rb +37 -0
- data/lib/active_validation/validators/ip_validator.rb +9 -0
- data/lib/active_validation/validators/isbn_validator.rb +24 -0
- data/lib/active_validation/validators/isin_validator.rb +38 -0
- data/lib/active_validation/validators/latitude_validator.rb +9 -0
- data/lib/active_validation/validators/longitude_validator.rb +9 -0
- data/lib/active_validation/validators/mac_address_validator.rb +24 -0
- data/lib/active_validation/validators/name_validator.rb +9 -0
- data/lib/active_validation/validators/password_validator.rb +23 -0
- data/lib/active_validation/validators/phone_validator.rb +9 -0
- data/lib/active_validation/validators/sedol_validator.rb +32 -0
- data/lib/active_validation/validators/slug_validator.rb +9 -0
- data/lib/active_validation/validators/ssn_validator.rb +9 -0
- data/lib/active_validation/validators/url_validator.rb +36 -0
- data/lib/active_validation/validators/username_validator.rb +9 -0
- data/lib/active_validation/validators/uuid_validator.rb +28 -0
- data/lib/active_validation/version.rb +3 -0
- data/lib/active_validation.rb +91 -0
- data/spec/lib/alpha_numeric_validator_spec.rb +91 -0
- data/spec/lib/alpha_validator_spec.rb +182 -0
- data/spec/lib/base64_validator_spec.rb +33 -0
- data/spec/lib/boolean_validator_spec.rb +35 -0
- data/spec/lib/credit_card_validator_spec.rb +686 -0
- data/spec/lib/currency_validator_spec.rb +63 -0
- data/spec/lib/cusip_validator_spec.rb +27 -0
- data/spec/lib/email_validator_spec.rb +109 -0
- data/spec/lib/equality_validator_spec.rb +334 -0
- data/spec/lib/hex_validator_spec.rb +73 -0
- data/spec/lib/imei_validator_spec.rb +41 -0
- data/spec/lib/ip_validator_spec.rb +33 -0
- data/spec/lib/isbn_validator_spec.rb +41 -0
- data/spec/lib/isin_validator_spec.rb +35 -0
- data/spec/lib/latitude_validator_spec.rb +31 -0
- data/spec/lib/longitude_validator_spec.rb +31 -0
- data/spec/lib/mac_address_validator_spec.rb +54 -0
- data/spec/lib/name_validator_spec.rb +39 -0
- data/spec/lib/password_validator_spec.rb +85 -0
- data/spec/lib/phone_validator_spec.rb +42 -0
- data/spec/lib/sedol_validator_spec.rb +31 -0
- data/spec/lib/slug_validator_spec.rb +41 -0
- data/spec/lib/ssn_validator_spec.rb +36 -0
- data/spec/lib/url_validator_spec.rb +106 -0
- data/spec/lib/username_validator_spec.rb +37 -0
- data/spec/lib/uuid_validator_spec.rb +157 -0
- data/spec/spec_helper.rb +12 -0
- metadata +260 -0
data/README.md
ADDED
@@ -0,0 +1,1099 @@
|
|
1
|
+
# ActiveValidation
|
2
|
+
|
3
|
+
[](http://badge.fury.io/rb/active_validation)
|
4
|
+
[](https://travis-ci.org/drexed/active_validation)
|
5
|
+
[](https://coveralls.io/r/drexed/active_validation)
|
6
|
+
|
7
|
+
ActiveValidation is a collection of custom validators that are often required in Rails applications plus shoulda-style RSpec matchers to test the validation rules.
|
8
|
+
|
9
|
+
Currently supported validators: alpha, alpha-numeric, Base64, boolean, credit card, currency, CUSIP, email, equality, GTIN, hex, IMEI, IP, ISBN, ISIN, latitude, longitude, MAC address, name, password, phone, SEDOL, slug, SSN, url, username, and UUID.
|
10
|
+
|
11
|
+
Highly recommended validators:
|
12
|
+
* **DateTime:** Validates Timeliness - https://github.com/adzap/validates_timeliness
|
13
|
+
* **Existence:** Validates Existence - https://github.com/perfectline/validates_existence
|
14
|
+
* **Group:** Group Validations - https://github.com/adzap/grouped_validations
|
15
|
+
* **Overlap:** Validates Overlap - https://github.com/robinbortlik/validates_overlap
|
16
|
+
|
17
|
+
## Installation
|
18
|
+
|
19
|
+
Add this line to your application's Gemfile:
|
20
|
+
|
21
|
+
gem 'active_validation'
|
22
|
+
|
23
|
+
And then execute:
|
24
|
+
|
25
|
+
$ bundle
|
26
|
+
|
27
|
+
Or install it yourself as:
|
28
|
+
|
29
|
+
$ gem install active_validation
|
30
|
+
|
31
|
+
## Usage
|
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, case: [:lower, :upper]
|
61
|
+
|
62
|
+
```ruby
|
63
|
+
validates :title, alpha: { strict: true }
|
64
|
+
validates :title, alpha: { case: :lower }
|
65
|
+
validates :title, alpha: { case: :upper, strict: true }
|
66
|
+
```
|
67
|
+
|
68
|
+
RSpec matcher is also available for your convenience:
|
69
|
+
|
70
|
+
```ruby
|
71
|
+
describe Book 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
|
+
Options: :strict
|
105
|
+
Strict: requires not including spaces
|
106
|
+
|
107
|
+
```ruby
|
108
|
+
validates :title, alpha_numeric: { strict: true }
|
109
|
+
```
|
110
|
+
|
111
|
+
RSpec matcher is also available for your convenience:
|
112
|
+
|
113
|
+
```ruby
|
114
|
+
describe Book do
|
115
|
+
it { should ensure_valid_alpha_numeric_format_of(:title) }
|
116
|
+
it { should_not ensure_valid_alpha_numeric_format_of(:name) }
|
117
|
+
end
|
118
|
+
```
|
119
|
+
|
120
|
+
### Base64Validator
|
121
|
+
|
122
|
+
**Ex:** YW55IGNhcm5hbCBwbGVhcw==
|
123
|
+
|
124
|
+
**Rules:**
|
125
|
+
* Characters: 0-1 A-Z =
|
126
|
+
|
127
|
+
With an ActiveRecord model:
|
128
|
+
|
129
|
+
```ruby
|
130
|
+
class Code < ActiveRecord::Base
|
131
|
+
attr_accessor :code, :name
|
132
|
+
validates :code, base64: true
|
133
|
+
end
|
134
|
+
```
|
135
|
+
|
136
|
+
Or any ruby class:
|
137
|
+
|
138
|
+
```ruby
|
139
|
+
class Code
|
140
|
+
include ActiveModel::Validations
|
141
|
+
attr_accessor :code, :name
|
142
|
+
validates :code, base64: true
|
143
|
+
end
|
144
|
+
```
|
145
|
+
|
146
|
+
RSpec matcher is also available for your convenience:
|
147
|
+
|
148
|
+
```ruby
|
149
|
+
describe Code do
|
150
|
+
it { should ensure_valid_base64_format_of(:code) }
|
151
|
+
it { should_not ensure_valid_base64_format_of(:name) }
|
152
|
+
end
|
153
|
+
```
|
154
|
+
|
155
|
+
### BooleanValidator
|
156
|
+
|
157
|
+
**Ex:** true or false or 1 or 0
|
158
|
+
|
159
|
+
**Rules:**
|
160
|
+
* Characters: 0-1
|
161
|
+
* Equality: true or false
|
162
|
+
|
163
|
+
With an ActiveRecord model:
|
164
|
+
|
165
|
+
```ruby
|
166
|
+
class User < ActiveRecord::Base
|
167
|
+
attr_accessor :active, :name
|
168
|
+
validates :active, boolean: true
|
169
|
+
end
|
170
|
+
```
|
171
|
+
|
172
|
+
Or any ruby class:
|
173
|
+
|
174
|
+
```ruby
|
175
|
+
class User
|
176
|
+
include ActiveModel::Validations
|
177
|
+
attr_accessor :active, :name
|
178
|
+
validates :active, boolean: true
|
179
|
+
end
|
180
|
+
```
|
181
|
+
|
182
|
+
RSpec matcher is also available for your convenience:
|
183
|
+
|
184
|
+
```ruby
|
185
|
+
describe User do
|
186
|
+
it { should ensure_valid_boolean_format_of(:active) }
|
187
|
+
it { should_not ensure_valid_boolean_format_of(:name) }
|
188
|
+
end
|
189
|
+
```
|
190
|
+
|
191
|
+
### CreditCardValidator
|
192
|
+
|
193
|
+
**Ex:** 370000000000002
|
194
|
+
|
195
|
+
**Rules:**
|
196
|
+
* Characters: 0-9 .-
|
197
|
+
* Must include: 0-9
|
198
|
+
* Range for card digits: 12-19
|
199
|
+
|
200
|
+
With an ActiveRecord model:
|
201
|
+
|
202
|
+
```ruby
|
203
|
+
class Invoice < ActiveRecord::Base
|
204
|
+
attr_accessor :cc_number, :name
|
205
|
+
validates :cc_number, credit_card: true
|
206
|
+
end
|
207
|
+
```
|
208
|
+
|
209
|
+
Or any ruby class:
|
210
|
+
|
211
|
+
```ruby
|
212
|
+
class Invoice
|
213
|
+
include ActiveModel::Validations
|
214
|
+
attr_accessor :cc_number, :name
|
215
|
+
validates :cc_number, credit_card: true
|
216
|
+
end
|
217
|
+
```
|
218
|
+
|
219
|
+
Options: :strict, card: [:american_express (:amex), :diners_club, :discover, :jbc, :laser, :maestro, :mastercard, :solo, :unionpay, :visa]
|
220
|
+
Strict: requires not including spaces
|
221
|
+
|
222
|
+
```ruby
|
223
|
+
validates :cc_number, credit_card: { card: :visa }
|
224
|
+
validates :cc_number, credit_card: { strict: true }
|
225
|
+
validates :cc_number, credit_card: { card: :discover, strict: true }
|
226
|
+
```
|
227
|
+
|
228
|
+
RSpec matcher is also available for your convenience:
|
229
|
+
|
230
|
+
```ruby
|
231
|
+
describe Invoice do
|
232
|
+
it { should ensure_valid_credit_card_format_of(:cc_number) }
|
233
|
+
it { should_not ensure_valid_credit_card_format_of(:name) }
|
234
|
+
end
|
235
|
+
```
|
236
|
+
|
237
|
+
### CurrencyValidator
|
238
|
+
|
239
|
+
**Ex:** 123.00 or .1
|
240
|
+
|
241
|
+
**Rules:**
|
242
|
+
* Characters: 0-9 .
|
243
|
+
* Must include: .
|
244
|
+
* Range for cents: 1-2
|
245
|
+
|
246
|
+
With an ActiveRecord model:
|
247
|
+
|
248
|
+
```ruby
|
249
|
+
class Product < ActiveRecord::Base
|
250
|
+
attr_accessor :price, :name
|
251
|
+
validates :price, currency: true
|
252
|
+
end
|
253
|
+
```
|
254
|
+
|
255
|
+
Or any ruby class:
|
256
|
+
|
257
|
+
```ruby
|
258
|
+
class Product
|
259
|
+
include ActiveModel::Validations
|
260
|
+
attr_accessor :price, :name
|
261
|
+
validates :price, currency: true
|
262
|
+
end
|
263
|
+
```
|
264
|
+
|
265
|
+
Options: :strict
|
266
|
+
Strict: requires leading number and exactly two decimals, 1.45
|
267
|
+
|
268
|
+
```ruby
|
269
|
+
validates :price, currency: { strict: true }
|
270
|
+
```
|
271
|
+
|
272
|
+
RSpec matcher is also available for your convenience:
|
273
|
+
|
274
|
+
```ruby
|
275
|
+
describe Product do
|
276
|
+
it { should ensure_valid_currency_format_of(:price) }
|
277
|
+
it { should_not ensure_valid_currency_format_of(:name) }
|
278
|
+
end
|
279
|
+
```
|
280
|
+
|
281
|
+
### CusipValidator
|
282
|
+
|
283
|
+
**Ex:** 125509BG3
|
284
|
+
|
285
|
+
**Rules:**
|
286
|
+
* Characters: 0-1 A-Z
|
287
|
+
* Length: 1-9
|
288
|
+
|
289
|
+
With an ActiveRecord model:
|
290
|
+
|
291
|
+
```ruby
|
292
|
+
class Bank < ActiveRecord::Base
|
293
|
+
attr_accessor :code, :name
|
294
|
+
validates :code, cusip: true
|
295
|
+
end
|
296
|
+
```
|
297
|
+
|
298
|
+
Or any ruby class:
|
299
|
+
|
300
|
+
```ruby
|
301
|
+
class Bank
|
302
|
+
include ActiveModel::Validations
|
303
|
+
attr_accessor :code, :name
|
304
|
+
validates :code, cusip: true
|
305
|
+
end
|
306
|
+
```
|
307
|
+
|
308
|
+
RSpec matcher is also available for your convenience:
|
309
|
+
|
310
|
+
```ruby
|
311
|
+
describe Bank do
|
312
|
+
it { should ensure_valid_cusip_format_of(:code) }
|
313
|
+
it { should_not ensure_cusip_base64_format_of(:name) }
|
314
|
+
end
|
315
|
+
```
|
316
|
+
|
317
|
+
### EmailValidator
|
318
|
+
|
319
|
+
**Ex:** user@example.com or user+123@example-site.com
|
320
|
+
|
321
|
+
**Rules:**
|
322
|
+
* Characters in username: a-z 0-9 -_+.
|
323
|
+
* Must include: @
|
324
|
+
* Characters in domain: a-z 0-9 -
|
325
|
+
* Must include extension: .co, .org, .museum
|
326
|
+
|
327
|
+
With an ActiveRecord model:
|
328
|
+
|
329
|
+
```ruby
|
330
|
+
class User < ActiveRecord::Base
|
331
|
+
attr_accessor :email, :name
|
332
|
+
validates :email, email: true
|
333
|
+
end
|
334
|
+
```
|
335
|
+
|
336
|
+
Or any ruby class:
|
337
|
+
|
338
|
+
```ruby
|
339
|
+
class User
|
340
|
+
include ActiveModel::Validations
|
341
|
+
attr_accessor :email, :name
|
342
|
+
validates :email, email: true
|
343
|
+
end
|
344
|
+
```
|
345
|
+
|
346
|
+
Options: :domains
|
347
|
+
|
348
|
+
```ruby
|
349
|
+
validates :email, email: { domains: 'com' }
|
350
|
+
validates :email, email: { domains: :com }
|
351
|
+
validates :email, email: { domains: [:com, 'edu'] }
|
352
|
+
```
|
353
|
+
|
354
|
+
RSpec matcher is also available for your convenience:
|
355
|
+
|
356
|
+
```ruby
|
357
|
+
describe User do
|
358
|
+
it { should ensure_valid_email_format_of(:email) }
|
359
|
+
it { should_not ensure_valid_email_format_of(:name) }
|
360
|
+
end
|
361
|
+
```
|
362
|
+
|
363
|
+
### EqualityValidator
|
364
|
+
|
365
|
+
**Operators:**
|
366
|
+
* Less than: x < y
|
367
|
+
* Less than or equal to: x <= y
|
368
|
+
* Greater than: x > y
|
369
|
+
* Greater than or equal to: x >= y
|
370
|
+
* Equal to: x == y
|
371
|
+
* Not equal to: x != y
|
372
|
+
|
373
|
+
|
374
|
+
**Rules:**
|
375
|
+
* Equal and not equal to: cannot be nil
|
376
|
+
|
377
|
+
With an ActiveRecord model:
|
378
|
+
|
379
|
+
```ruby
|
380
|
+
class Auction < ActiveRecord::Base
|
381
|
+
attr_accessor :bid, :price, :product
|
382
|
+
validates :bid, equality: { operator: :greater_than_or_equal_to, to: :price }
|
383
|
+
end
|
384
|
+
```
|
385
|
+
|
386
|
+
Or any ruby class:
|
387
|
+
|
388
|
+
```ruby
|
389
|
+
class Auction
|
390
|
+
include ActiveModel::Validations
|
391
|
+
attr_accessor :bid, :price, :product
|
392
|
+
validates :bid, equality: { operator: :greater_than_or_equal_to, to: :price }
|
393
|
+
end
|
394
|
+
```
|
395
|
+
|
396
|
+
RSpec matcher is also available for your convenience:
|
397
|
+
|
398
|
+
```ruby
|
399
|
+
describe Auction do
|
400
|
+
it { should ensure_equality_of(:bid).to(:price) }
|
401
|
+
it { should_not ensure_equality_of(:bid).to(:product) }
|
402
|
+
end
|
403
|
+
```
|
404
|
+
|
405
|
+
### GtinValidator
|
406
|
+
|
407
|
+
**Ex:** 73513537 or 4 006381 33393 1
|
408
|
+
|
409
|
+
**Rules:**
|
410
|
+
* Length: 8, 12, 13, or 14
|
411
|
+
* Characters: 0-9
|
412
|
+
|
413
|
+
With an ActiveRecord model:
|
414
|
+
|
415
|
+
```ruby
|
416
|
+
class Trade < ActiveRecord::Base
|
417
|
+
attr_accessor :code, :name
|
418
|
+
validates :code, gtin: true
|
419
|
+
end
|
420
|
+
```
|
421
|
+
|
422
|
+
Or any ruby class:
|
423
|
+
|
424
|
+
```ruby
|
425
|
+
class Trade
|
426
|
+
include ActiveModel::Validations
|
427
|
+
attr_accessor :code, :name
|
428
|
+
validates :code, gtin: true
|
429
|
+
end
|
430
|
+
```
|
431
|
+
|
432
|
+
Options: :strict, format: [:ean_8, :gtin_8, :ucc_8, :gtin_12, :upc, :upc_a, :ean, :ean_13, :gtin_13, :ucc_13, :gtin_14, :ucc_14]
|
433
|
+
Strict: requires not including spaces
|
434
|
+
|
435
|
+
```ruby
|
436
|
+
validates :code, gtin: { format: :ean_8 }
|
437
|
+
validates :code, gtin: { strict: true }
|
438
|
+
validates :code, gtin: { format: :ucc_13, strict: true }
|
439
|
+
```
|
440
|
+
|
441
|
+
RSpec matcher is also available for your convenience:
|
442
|
+
|
443
|
+
```ruby
|
444
|
+
describe Trade do
|
445
|
+
it { should ensure_valid_gtin_format_of(:code) }
|
446
|
+
it { should_not ensure_valid_gtin_format_of(:name) }
|
447
|
+
end
|
448
|
+
```
|
449
|
+
|
450
|
+
### HexValidator
|
451
|
+
|
452
|
+
**Ex:** #a9a9a9 or #999 or aaaaaa or AAA
|
453
|
+
|
454
|
+
**Rules:**
|
455
|
+
* Prefix (non-mandatory): #
|
456
|
+
* Length: 3 or 6
|
457
|
+
* Characters: A-F a-f 0-9
|
458
|
+
|
459
|
+
With an ActiveRecord model:
|
460
|
+
|
461
|
+
```ruby
|
462
|
+
class Profile < ActiveRecord::Base
|
463
|
+
attr_accessor :color, :trim
|
464
|
+
validates :color, hex: true
|
465
|
+
end
|
466
|
+
```
|
467
|
+
|
468
|
+
Or any ruby class:
|
469
|
+
|
470
|
+
```ruby
|
471
|
+
class Profile
|
472
|
+
include ActiveModel::Validations
|
473
|
+
attr_accessor :color, :trim
|
474
|
+
validates :color, hex: true
|
475
|
+
end
|
476
|
+
```
|
477
|
+
|
478
|
+
RSpec matcher is also available for your convenience:
|
479
|
+
|
480
|
+
```ruby
|
481
|
+
describe Color do
|
482
|
+
it { should ensure_valid_hex_format_of(:color) }
|
483
|
+
it { should_not ensure_valid_hex_format_of(:trim) }
|
484
|
+
end
|
485
|
+
```
|
486
|
+
|
487
|
+
### ImeiValidator
|
488
|
+
|
489
|
+
**Ex:** 356843052637512 or 35-6843052-637512 or 35.6843052.637512
|
490
|
+
|
491
|
+
**Rules:**
|
492
|
+
* Length: min 14
|
493
|
+
* Characters: 0-9 -.
|
494
|
+
|
495
|
+
With an ActiveRecord model:
|
496
|
+
|
497
|
+
```ruby
|
498
|
+
class User < ActiveRecord::Base
|
499
|
+
attr_accessor :imei, :name
|
500
|
+
validates :imei, imei: true
|
501
|
+
end
|
502
|
+
```
|
503
|
+
|
504
|
+
Or any ruby class:
|
505
|
+
|
506
|
+
```ruby
|
507
|
+
class User
|
508
|
+
include ActiveModel::Validations
|
509
|
+
attr_accessor :imei, :name
|
510
|
+
validates :imei, imei: true
|
511
|
+
end
|
512
|
+
```
|
513
|
+
|
514
|
+
RSpec matcher is also available for your convenience:
|
515
|
+
|
516
|
+
```ruby
|
517
|
+
describe User do
|
518
|
+
it { should ensure_valid_imei_format_of(:imei) }
|
519
|
+
it { should_not ensure_valid_imei_format_of(:name) }
|
520
|
+
end
|
521
|
+
```
|
522
|
+
|
523
|
+
### IpValidator
|
524
|
+
|
525
|
+
**Ex:** 0.0.0.0 or 127.0.0.1 or 167.39.240.31
|
526
|
+
|
527
|
+
**Rules:**
|
528
|
+
* Length: min 7
|
529
|
+
* Characters: 0-9 .
|
530
|
+
|
531
|
+
With an ActiveRecord model:
|
532
|
+
|
533
|
+
```ruby
|
534
|
+
class User < ActiveRecord::Base
|
535
|
+
attr_accessor :ip, :name
|
536
|
+
validates :ip, ip: true
|
537
|
+
end
|
538
|
+
```
|
539
|
+
|
540
|
+
Or any ruby class:
|
541
|
+
|
542
|
+
```ruby
|
543
|
+
class User
|
544
|
+
include ActiveModel::Validations
|
545
|
+
attr_accessor :ip, :name
|
546
|
+
validates :ip, ip: true
|
547
|
+
end
|
548
|
+
```
|
549
|
+
|
550
|
+
RSpec matcher is also available for your convenience:
|
551
|
+
|
552
|
+
```ruby
|
553
|
+
describe User do
|
554
|
+
it { should ensure_valid_ip_format_of(:ip) }
|
555
|
+
it { should_not ensure_valid_ip_format_of(:name) }
|
556
|
+
end
|
557
|
+
```
|
558
|
+
|
559
|
+
### IsbnValidator
|
560
|
+
|
561
|
+
**Ex:** 9519854894 or 0-9722051-1-x or 978 159059 9938
|
562
|
+
|
563
|
+
**Rules:**
|
564
|
+
* Length: 10 or 13
|
565
|
+
* Characters: 0-9 -|
|
566
|
+
|
567
|
+
With an ActiveRecord model:
|
568
|
+
|
569
|
+
```ruby
|
570
|
+
class User < ActiveRecord::Base
|
571
|
+
attr_accessor :isbn, :name
|
572
|
+
validates :isbn, isbn: true
|
573
|
+
end
|
574
|
+
```
|
575
|
+
|
576
|
+
Or any ruby class:
|
577
|
+
|
578
|
+
```ruby
|
579
|
+
class User
|
580
|
+
include ActiveModel::Validations
|
581
|
+
attr_accessor :isbn, :name
|
582
|
+
validates :isbn, isbn: true
|
583
|
+
end
|
584
|
+
```
|
585
|
+
|
586
|
+
RSpec matcher is also available for your convenience:
|
587
|
+
|
588
|
+
```ruby
|
589
|
+
describe User do
|
590
|
+
it { should ensure_valid_isbn_format_of(:isbn) }
|
591
|
+
it { should_not ensure_valid_isbn_format_of(:name) }
|
592
|
+
end
|
593
|
+
```
|
594
|
+
|
595
|
+
### IsinValidator
|
596
|
+
|
597
|
+
**Ex:** US0378331005 or AU0000XVGZA3
|
598
|
+
|
599
|
+
**Rules:**
|
600
|
+
* Length: 12
|
601
|
+
* Characters: 0-9 A-Z
|
602
|
+
* Start: valid country code
|
603
|
+
|
604
|
+
With an ActiveRecord model:
|
605
|
+
|
606
|
+
```ruby
|
607
|
+
class Trade < ActiveRecord::Base
|
608
|
+
attr_accessor :isin, :name
|
609
|
+
validates :isin, isin: true
|
610
|
+
end
|
611
|
+
```
|
612
|
+
|
613
|
+
Or any ruby class:
|
614
|
+
|
615
|
+
```ruby
|
616
|
+
class User
|
617
|
+
include ActiveModel::Validations
|
618
|
+
attr_accessor :isin, :name
|
619
|
+
validates :isin, isin: true
|
620
|
+
end
|
621
|
+
```
|
622
|
+
|
623
|
+
RSpec matcher is also available for your convenience:
|
624
|
+
|
625
|
+
```ruby
|
626
|
+
describe User do
|
627
|
+
it { should ensure_valid_isin_format_of(:isin) }
|
628
|
+
it { should_not ensure_valid_isin_format_of(:name) }
|
629
|
+
end
|
630
|
+
```
|
631
|
+
|
632
|
+
### LatitudeValidator
|
633
|
+
|
634
|
+
**Ex:** 78.213 or -34.985
|
635
|
+
|
636
|
+
**Rules:**
|
637
|
+
* Range: 90 to -90
|
638
|
+
* Characters: 0-9
|
639
|
+
|
640
|
+
With an ActiveRecord model:
|
641
|
+
|
642
|
+
```ruby
|
643
|
+
class Location < ActiveRecord::Base
|
644
|
+
attr_accessor :lat, :name
|
645
|
+
validates :lat, latitude: true
|
646
|
+
end
|
647
|
+
```
|
648
|
+
|
649
|
+
Or any ruby class:
|
650
|
+
|
651
|
+
```ruby
|
652
|
+
class Location
|
653
|
+
include ActiveModel::Validations
|
654
|
+
attr_accessor :lat, :name
|
655
|
+
validates :lat, latitude: true
|
656
|
+
end
|
657
|
+
```
|
658
|
+
|
659
|
+
RSpec matcher is also available for your convenience:
|
660
|
+
|
661
|
+
```ruby
|
662
|
+
describe Location do
|
663
|
+
it { should ensure_valid_latitude_format_of(:lat) }
|
664
|
+
it { should_not ensure_valid_latitude_format_of(:name) }
|
665
|
+
end
|
666
|
+
```
|
667
|
+
|
668
|
+
### LongitudeValidator
|
669
|
+
|
670
|
+
**Ex:** 165.371 or -56.152
|
671
|
+
|
672
|
+
**Rules:**
|
673
|
+
* Range: 180 to -180
|
674
|
+
* Characters: 0-9
|
675
|
+
|
676
|
+
With an ActiveRecord model:
|
677
|
+
|
678
|
+
```ruby
|
679
|
+
class Location < ActiveRecord::Base
|
680
|
+
attr_accessor :lon, :name
|
681
|
+
validates :lon, longitude: true
|
682
|
+
end
|
683
|
+
```
|
684
|
+
|
685
|
+
Or any ruby class:
|
686
|
+
|
687
|
+
```ruby
|
688
|
+
class Location
|
689
|
+
include ActiveModel::Validations
|
690
|
+
attr_accessor :lon, :name
|
691
|
+
validates :lon, longitude: true
|
692
|
+
end
|
693
|
+
```
|
694
|
+
|
695
|
+
RSpec matcher is also available for your convenience:
|
696
|
+
|
697
|
+
```ruby
|
698
|
+
describe Location do
|
699
|
+
it { should ensure_valid_longitude_format_of(:lon) }
|
700
|
+
it { should_not ensure_valid_longitude_format_of(:name) }
|
701
|
+
end
|
702
|
+
```
|
703
|
+
|
704
|
+
### MacAddressValidator
|
705
|
+
|
706
|
+
**Ex:**
|
707
|
+
'08:00:2b:01:02:03'
|
708
|
+
'08-00-2b-01-02-03'
|
709
|
+
'08002b:010203'
|
710
|
+
'08002b-010203'
|
711
|
+
'0800.2b01.0203'
|
712
|
+
'08002b010203'
|
713
|
+
|
714
|
+
**Rules:**
|
715
|
+
* Characters: a-z 0-9 -.:
|
716
|
+
|
717
|
+
With an ActiveRecord model:
|
718
|
+
|
719
|
+
```ruby
|
720
|
+
class Device < ActiveRecord::Base
|
721
|
+
attr_accessor :mac, :name
|
722
|
+
validates :mac, mac_address: true
|
723
|
+
end
|
724
|
+
```
|
725
|
+
|
726
|
+
Or any ruby class:
|
727
|
+
|
728
|
+
```ruby
|
729
|
+
class Device
|
730
|
+
include ActiveModel::Validations
|
731
|
+
attr_accessor :mac, :name
|
732
|
+
validates :mac, mac_address: true
|
733
|
+
end
|
734
|
+
```
|
735
|
+
|
736
|
+
RSpec matcher is also available for your convenience:
|
737
|
+
|
738
|
+
```ruby
|
739
|
+
describe Device do
|
740
|
+
it { should ensure_valid_mac_address_format_of }
|
741
|
+
it { should_not ensure_valid_mac_address_format_of(:name) }
|
742
|
+
end
|
743
|
+
```
|
744
|
+
|
745
|
+
### NameValidator
|
746
|
+
|
747
|
+
**Ex:** James Brown or Billy Bob Thorton Jr
|
748
|
+
|
749
|
+
**Rules:**
|
750
|
+
* Range: 2 - 5 names
|
751
|
+
* Characters: a-z -
|
752
|
+
* Must include: First Last
|
753
|
+
|
754
|
+
With an ActiveRecord model:
|
755
|
+
|
756
|
+
```ruby
|
757
|
+
class User < ActiveRecord::Base
|
758
|
+
attr_accessor :name, :email
|
759
|
+
validates :name, name: true
|
760
|
+
end
|
761
|
+
```
|
762
|
+
|
763
|
+
Or any ruby class:
|
764
|
+
|
765
|
+
```ruby
|
766
|
+
class User
|
767
|
+
include ActiveModel::Validations
|
768
|
+
attr_accessor :name, :email
|
769
|
+
validates :name, name: true
|
770
|
+
end
|
771
|
+
```
|
772
|
+
|
773
|
+
RSpec matcher is also available for your convenience:
|
774
|
+
|
775
|
+
```ruby
|
776
|
+
describe User do
|
777
|
+
it { should ensure_valid_name_format_of(:name) }
|
778
|
+
it { should_not ensure_valid_name_format_of(:email) }
|
779
|
+
end
|
780
|
+
```
|
781
|
+
|
782
|
+
### PasswordValidator
|
783
|
+
|
784
|
+
**Ex:** password or password123 or pa!!word
|
785
|
+
|
786
|
+
**Rules:**
|
787
|
+
* Range: 6-18
|
788
|
+
* Characters: a-z 0-9 -_!@#$%^&*
|
789
|
+
|
790
|
+
With an ActiveRecord model:
|
791
|
+
|
792
|
+
```ruby
|
793
|
+
class User < ActiveRecord::Base
|
794
|
+
attr_accessor :password, :name
|
795
|
+
validates :password, password: true
|
796
|
+
end
|
797
|
+
```
|
798
|
+
|
799
|
+
Or any ruby class:
|
800
|
+
|
801
|
+
```ruby
|
802
|
+
class User
|
803
|
+
include ActiveModel::Validations
|
804
|
+
attr_accessor :password, :name
|
805
|
+
validates :password, password: true
|
806
|
+
end
|
807
|
+
```
|
808
|
+
Options: :strict
|
809
|
+
Strict: requires length between 6 and 18, one number, lowercase, upcase letter
|
810
|
+
|
811
|
+
```ruby
|
812
|
+
validates :password, password: { strict: true }
|
813
|
+
```
|
814
|
+
|
815
|
+
RSpec matcher is also available for your convenience:
|
816
|
+
|
817
|
+
```ruby
|
818
|
+
describe User do
|
819
|
+
it { should ensure_valid_password_format_of(:password) }
|
820
|
+
it { should_not ensure_valid_password_format_of(:name) }
|
821
|
+
end
|
822
|
+
```
|
823
|
+
|
824
|
+
### PhoneValidator
|
825
|
+
|
826
|
+
**Ex:** 555 333 4444 or (555) 123-4567 or +1 (555) 123 4567 ext-890
|
827
|
+
|
828
|
+
**Rules:**
|
829
|
+
* Characters: a-z 0-9 -()+
|
830
|
+
|
831
|
+
With an ActiveRecord model:
|
832
|
+
|
833
|
+
```ruby
|
834
|
+
class User < ActiveRecord::Base
|
835
|
+
attr_accessor :phone, :name
|
836
|
+
validates :phone, phone: true
|
837
|
+
end
|
838
|
+
```
|
839
|
+
|
840
|
+
Or any ruby class:
|
841
|
+
|
842
|
+
```ruby
|
843
|
+
class User
|
844
|
+
include ActiveModel::Validations
|
845
|
+
attr_accessor :phone, :name
|
846
|
+
validates :phone, phone: true
|
847
|
+
end
|
848
|
+
```
|
849
|
+
|
850
|
+
RSpec matcher is also available for your convenience:
|
851
|
+
|
852
|
+
```ruby
|
853
|
+
describe User do
|
854
|
+
it { should ensure_valid_phone_format_of(:phone) }
|
855
|
+
it { should_not ensure_valid_phone_format_of(:name) }
|
856
|
+
end
|
857
|
+
```
|
858
|
+
|
859
|
+
### SedolValidator
|
860
|
+
|
861
|
+
**Ex:** B0WNLY7
|
862
|
+
|
863
|
+
**Rules:**
|
864
|
+
* Characters: A-Z 0-9
|
865
|
+
|
866
|
+
With an ActiveRecord model:
|
867
|
+
|
868
|
+
```ruby
|
869
|
+
class Trade < ActiveRecord::Base
|
870
|
+
attr_accessor :sedol, :name
|
871
|
+
validates :sedol, sedol: true
|
872
|
+
end
|
873
|
+
```
|
874
|
+
|
875
|
+
Or any ruby class:
|
876
|
+
|
877
|
+
```ruby
|
878
|
+
class Trade
|
879
|
+
include ActiveModel::Validations
|
880
|
+
attr_accessor :sedol, :name
|
881
|
+
validates :sedol, sedol: true
|
882
|
+
end
|
883
|
+
```
|
884
|
+
|
885
|
+
RSpec matcher is also available for your convenience:
|
886
|
+
|
887
|
+
```ruby
|
888
|
+
describe Trade do
|
889
|
+
it { should ensure_valid_sedol_format_of(:sedol) }
|
890
|
+
it { should_not ensure_valid_sedol_format_of(:name) }
|
891
|
+
end
|
892
|
+
```
|
893
|
+
|
894
|
+
### SlugValidator
|
895
|
+
|
896
|
+
**Ex:** slug1234 or slug-1234
|
897
|
+
|
898
|
+
**Rules:**
|
899
|
+
* Characters: a-z 0-9 -
|
900
|
+
|
901
|
+
With an ActiveRecord model:
|
902
|
+
|
903
|
+
```ruby
|
904
|
+
class User < ActiveRecord::Base
|
905
|
+
attr_accessor :slug, :name
|
906
|
+
validates :slug, slug: true
|
907
|
+
end
|
908
|
+
```
|
909
|
+
|
910
|
+
Or any ruby class:
|
911
|
+
|
912
|
+
```ruby
|
913
|
+
class User
|
914
|
+
include ActiveModel::Validations
|
915
|
+
attr_accessor :slug, :name
|
916
|
+
validates :slug, slug: true
|
917
|
+
end
|
918
|
+
```
|
919
|
+
|
920
|
+
RSpec matcher is also available for your convenience:
|
921
|
+
|
922
|
+
```ruby
|
923
|
+
describe User do
|
924
|
+
it { should ensure_valid_slug_format_of(:slug) }
|
925
|
+
it { should_not ensure_valid_slug_format_of(:name) }
|
926
|
+
end
|
927
|
+
```
|
928
|
+
|
929
|
+
### SsnValidator
|
930
|
+
|
931
|
+
**Ex:** 333-22-4444 or 333224444
|
932
|
+
|
933
|
+
**Rules:**
|
934
|
+
* Characters: 0-9 -
|
935
|
+
|
936
|
+
With an ActiveRecord model:
|
937
|
+
|
938
|
+
```ruby
|
939
|
+
class User < ActiveRecord::Base
|
940
|
+
attr_accessor :ssn, :name
|
941
|
+
validates :ssn, ssn: true
|
942
|
+
end
|
943
|
+
```
|
944
|
+
|
945
|
+
Or any ruby class:
|
946
|
+
|
947
|
+
```ruby
|
948
|
+
class User
|
949
|
+
include ActiveModel::Validations
|
950
|
+
attr_accessor :ssn, :name
|
951
|
+
validates :ssn, ssn: true
|
952
|
+
end
|
953
|
+
```
|
954
|
+
|
955
|
+
RSpec matcher is also available for your convenience:
|
956
|
+
|
957
|
+
```ruby
|
958
|
+
describe User do
|
959
|
+
it { should ensure_valid_ssn_format_of(:ssn) }
|
960
|
+
it { should_not ensure_valid_ssn_format_of(:name) }
|
961
|
+
end
|
962
|
+
```
|
963
|
+
|
964
|
+
### UrlValidator
|
965
|
+
|
966
|
+
**Ex:** example.com or http://www.example.com
|
967
|
+
|
968
|
+
**Rules:**
|
969
|
+
* Characters in root: a-z 0-9 -.//:
|
970
|
+
* Characters in domain: a-z 0-9 -
|
971
|
+
* Must include extension: .co, .org, .museum
|
972
|
+
|
973
|
+
With an ActiveRecord model:
|
974
|
+
|
975
|
+
```ruby
|
976
|
+
class User < ActiveRecord::Base
|
977
|
+
attr_accessor :url, :name
|
978
|
+
validates :url, url: true
|
979
|
+
end
|
980
|
+
```
|
981
|
+
|
982
|
+
Or any ruby class:
|
983
|
+
|
984
|
+
```ruby
|
985
|
+
class User
|
986
|
+
include ActiveModel::Validations
|
987
|
+
attr_accessor :url, :name
|
988
|
+
validates :url, url: true
|
989
|
+
end
|
990
|
+
```
|
991
|
+
|
992
|
+
Options: :domains, :root, :scheme
|
993
|
+
|
994
|
+
```ruby
|
995
|
+
validates :url, url: { scheme: :http }
|
996
|
+
validates :url, url: { scheme: [:http, 'https'] }
|
997
|
+
validates :url, url: { scheme: :http, root: true, domains: :com }
|
998
|
+
validates :url, url: { root: true }
|
999
|
+
validates :url, url: { root: true, domains: :com }
|
1000
|
+
validates :url, url: { domains: 'com' }
|
1001
|
+
validates :url, url: { domains: :com }
|
1002
|
+
validates :url, url: { domains: [:com, 'edu'] }
|
1003
|
+
```
|
1004
|
+
|
1005
|
+
RSpec matcher is also available for your convenience:
|
1006
|
+
|
1007
|
+
```ruby
|
1008
|
+
describe User do
|
1009
|
+
it { should ensure_valid_url_format_of(:url) }
|
1010
|
+
it { should_not ensure_valid_url_format_of(:name) }
|
1011
|
+
end
|
1012
|
+
```
|
1013
|
+
|
1014
|
+
### UsernameValidator
|
1015
|
+
|
1016
|
+
**Ex:** username123 or username
|
1017
|
+
|
1018
|
+
**Rules:**
|
1019
|
+
* Range: 2-16
|
1020
|
+
* Characters: a-z 0-9 -_
|
1021
|
+
|
1022
|
+
With an ActiveRecord model:
|
1023
|
+
|
1024
|
+
```ruby
|
1025
|
+
class User < ActiveRecord::Base
|
1026
|
+
attr_accessor :username, :name
|
1027
|
+
validates :username, username: true
|
1028
|
+
end
|
1029
|
+
```
|
1030
|
+
|
1031
|
+
Or any ruby class:
|
1032
|
+
|
1033
|
+
```ruby
|
1034
|
+
class User
|
1035
|
+
include ActiveModel::Validations
|
1036
|
+
attr_accessor :username, :name
|
1037
|
+
validates :username, username: true
|
1038
|
+
end
|
1039
|
+
```
|
1040
|
+
|
1041
|
+
RSpec matcher is also available for your convenience:
|
1042
|
+
|
1043
|
+
```ruby
|
1044
|
+
describe User do
|
1045
|
+
it { should ensure_valid_username_format_of(:username) }
|
1046
|
+
it { should_not ensure_valid_username_format_of(:name) }
|
1047
|
+
end
|
1048
|
+
```
|
1049
|
+
|
1050
|
+
### UuidValidator
|
1051
|
+
|
1052
|
+
**Ex:** 886313e1-3b8a-5372-9b90-0c9aee199e5d
|
1053
|
+
|
1054
|
+
**Rules:**
|
1055
|
+
* Characters: A-Z a-z 0-9 -
|
1056
|
+
|
1057
|
+
With an ActiveRecord model:
|
1058
|
+
|
1059
|
+
```ruby
|
1060
|
+
class User < ActiveRecord::Base
|
1061
|
+
attr_accessor :uuid, :name
|
1062
|
+
validates :uuid, uuid: true
|
1063
|
+
end
|
1064
|
+
```
|
1065
|
+
|
1066
|
+
Or any ruby class:
|
1067
|
+
|
1068
|
+
```ruby
|
1069
|
+
class User
|
1070
|
+
include ActiveModel::Validations
|
1071
|
+
attr_accessor :uuid, :name
|
1072
|
+
validates :uuid, username: true
|
1073
|
+
end
|
1074
|
+
```
|
1075
|
+
|
1076
|
+
Options: :version
|
1077
|
+
|
1078
|
+
```ruby
|
1079
|
+
validates :uuid, uuid: { version: 3 }
|
1080
|
+
```
|
1081
|
+
|
1082
|
+
RSpec matcher is also available for your convenience:
|
1083
|
+
|
1084
|
+
```ruby
|
1085
|
+
describe User do
|
1086
|
+
it { should ensure_valid_uuid_format_of(:uuid) }
|
1087
|
+
it { should_not ensure_valid_uuid_format_of(:name) }
|
1088
|
+
end
|
1089
|
+
```
|
1090
|
+
|
1091
|
+
## Contributing
|
1092
|
+
|
1093
|
+
Your contribution is welcome.
|
1094
|
+
|
1095
|
+
1. Fork it
|
1096
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
1097
|
+
3. Commit your changes (`git commit -am 'Added some feature'`)
|
1098
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
1099
|
+
5. Create new Pull Request
|