flash_validators 0.0.1
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 +17 -0
- data/.rspec +4 -0
- data/.travis.yml +14 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +589 -0
- data/Rakefile +6 -0
- data/config/locales/en.yml +73 -0
- data/flash_validators.gemspec +28 -0
- data/lib/flash_validators/matchers/ensure_valid_boolean_format_of.rb +20 -0
- data/lib/flash_validators/matchers/ensure_valid_currency_format_of.rb +20 -0
- data/lib/flash_validators/matchers/ensure_valid_email_format_of.rb +20 -0
- data/lib/flash_validators/matchers/ensure_valid_equality_matcher_of.rb +32 -0
- data/lib/flash_validators/matchers/ensure_valid_hex_format_of.rb +20 -0
- data/lib/flash_validators/matchers/ensure_valid_imei_format_of.rb +20 -0
- data/lib/flash_validators/matchers/ensure_valid_ip_format_of.rb +20 -0
- data/lib/flash_validators/matchers/ensure_valid_latitude_format_of.rb +20 -0
- data/lib/flash_validators/matchers/ensure_valid_longitude_format_of.rb +20 -0
- data/lib/flash_validators/matchers/ensure_valid_mac_address_format_of.rb +20 -0
- data/lib/flash_validators/matchers/ensure_valid_name_format_of.rb +20 -0
- data/lib/flash_validators/matchers/ensure_valid_password_format_of.rb +20 -0
- data/lib/flash_validators/matchers/ensure_valid_phone_format_of.rb +20 -0
- data/lib/flash_validators/matchers/ensure_valid_slug_format_of.rb +20 -0
- data/lib/flash_validators/matchers/ensure_valid_ssn_format_of.rb +20 -0
- data/lib/flash_validators/matchers/ensure_valid_url_format_of.rb +20 -0
- data/lib/flash_validators/matchers/ensure_valid_username_format_of.rb +20 -0
- data/lib/flash_validators/validators/boolean_validator.rb +9 -0
- data/lib/flash_validators/validators/currency_validator.rb +17 -0
- data/lib/flash_validators/validators/email_validator.rb +25 -0
- data/lib/flash_validators/validators/equality_validator.rb +27 -0
- data/lib/flash_validators/validators/hex_validator.rb +9 -0
- data/lib/flash_validators/validators/imei_validator.rb +37 -0
- data/lib/flash_validators/validators/ip_validator.rb +9 -0
- data/lib/flash_validators/validators/latitude_validator.rb +9 -0
- data/lib/flash_validators/validators/longitude_validator.rb +9 -0
- data/lib/flash_validators/validators/mac_address_validator.rb +24 -0
- data/lib/flash_validators/validators/name_validator.rb +9 -0
- data/lib/flash_validators/validators/password_validator.rb +9 -0
- data/lib/flash_validators/validators/phone_validator.rb +9 -0
- data/lib/flash_validators/validators/slug_validator.rb +9 -0
- data/lib/flash_validators/validators/ssn_validator.rb +9 -0
- data/lib/flash_validators/validators/url_validator.rb +36 -0
- data/lib/flash_validators/validators/username_validator.rb +9 -0
- data/lib/flash_validators/version.rb +3 -0
- data/lib/flash_validators.rb +41 -0
- data/spec/lib/boolean_validator_spec.rb +35 -0
- data/spec/lib/currency_validator_spec.rb +63 -0
- data/spec/lib/email_validator_spec.rb +111 -0
- data/spec/lib/equality_validator_spec.rb +340 -0
- data/spec/lib/hex_validator_spec.rb +53 -0
- data/spec/lib/imei_validator_spec.rb +41 -0
- data/spec/lib/ip_validator_spec.rb +33 -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 +45 -0
- data/spec/lib/phone_validator_spec.rb +42 -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 +109 -0
- data/spec/lib/username_validator_spec.rb +37 -0
- data/spec/spec_helper.rb +11 -0
- metadata +224 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: d819664070dae5bc9091a3eebb5d63967faf32d4
|
4
|
+
data.tar.gz: 5b26cc91037a93c2b3f74d9b2444e73b6b60d71a
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 86ea4ff678d74c4488271ad78703ac653300d90b3c005455270cac338a4e5379ade7bd129d1adfe5abdad7f1b9198b3330a4b8400a021f4bb45646d71d9f0036
|
7
|
+
data.tar.gz: cf5fc12b6ecac763ac7968874d913c8fbd3d894d92242aba24783a89b0bbfec767fa4bb693b23fd5919de691342c5e5050dbfbf749d3688ab6613d8cbc9d5887
|
data/.coveralls.yml
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
service_name: travis-ci
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 Juan Gomez
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,589 @@
|
|
1
|
+
# Flash Validators
|
2
|
+
|
3
|
+
[](https://travis-ci.org/drexed/flash_validators)
|
4
|
+
[](https://coveralls.io/r/drexed/flash_validators)
|
5
|
+
[](https://codeclimate.com/github/drexed/flash_validators)
|
6
|
+
|
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
|
+
|
9
|
+
Currently supported validators: boolean, currency, email, equality, hex, IMEI, IP, latitude, longitude, mac address, name, password, phone, slug, ssn, url, and username.
|
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 'flash_validators'
|
22
|
+
|
23
|
+
And then execute:
|
24
|
+
|
25
|
+
$ bundle
|
26
|
+
|
27
|
+
Or install it yourself as:
|
28
|
+
|
29
|
+
$ gem install flash_validators
|
30
|
+
|
31
|
+
## Usage
|
32
|
+
|
33
|
+
### BooleanValidator
|
34
|
+
|
35
|
+
**Ex:** true or false or 1 or 0
|
36
|
+
|
37
|
+
**Rules:**
|
38
|
+
* Characters: 0-1 true or false
|
39
|
+
|
40
|
+
With an ActiveRecord model:
|
41
|
+
|
42
|
+
class User < ActiveRecord::Base
|
43
|
+
attr_accessor :active, :name
|
44
|
+
validates :active, boolean: true
|
45
|
+
end
|
46
|
+
|
47
|
+
Or any ruby class:
|
48
|
+
|
49
|
+
class User
|
50
|
+
include ActiveModel::Validations
|
51
|
+
attr_accessor :active, :name
|
52
|
+
validates :active, boolean: true
|
53
|
+
end
|
54
|
+
|
55
|
+
RSpec matcher is also available for your convenience:
|
56
|
+
|
57
|
+
describe Product do
|
58
|
+
it { should ensure_valid_boolean_format_of(:active) }
|
59
|
+
it { should_not ensure_valid_boolean_format_of(:name) }
|
60
|
+
end
|
61
|
+
|
62
|
+
### CurrencyValidator
|
63
|
+
|
64
|
+
**Ex:** 123.00 or .1
|
65
|
+
|
66
|
+
**Rules:**
|
67
|
+
* Characters: 0-9 .
|
68
|
+
* Must include: .
|
69
|
+
* Range for cents: 1-2
|
70
|
+
|
71
|
+
With an ActiveRecord model:
|
72
|
+
|
73
|
+
class Product < ActiveRecord::Base
|
74
|
+
attr_accessor :price, :name
|
75
|
+
validates :price, currency: true
|
76
|
+
end
|
77
|
+
|
78
|
+
Or any ruby class:
|
79
|
+
|
80
|
+
class Product
|
81
|
+
include ActiveModel::Validations
|
82
|
+
attr_accessor :price, :name
|
83
|
+
validates :price, currency: true
|
84
|
+
end
|
85
|
+
|
86
|
+
Strict: requires leading number and exactly two decimals, 1.45
|
87
|
+
|
88
|
+
validates :price, currency: { strict: true }
|
89
|
+
|
90
|
+
RSpec matcher is also available for your convenience:
|
91
|
+
|
92
|
+
describe Product do
|
93
|
+
it { should ensure_valid_currency_format_of(:price) }
|
94
|
+
it { should_not ensure_valid_currency_format_of(:name) }
|
95
|
+
end
|
96
|
+
|
97
|
+
### EmailValidator
|
98
|
+
|
99
|
+
**Ex:** user@example.com or user+123@example-site.com
|
100
|
+
|
101
|
+
**Rules:**
|
102
|
+
* Characters in username: a-z 0-9 -_+.
|
103
|
+
* Must include: @
|
104
|
+
* Characters in domain: a-z 0-9 -
|
105
|
+
* Must include extension: .co, .org, .museum
|
106
|
+
|
107
|
+
With an ActiveRecord model:
|
108
|
+
|
109
|
+
class User < ActiveRecord::Base
|
110
|
+
attr_accessor :email, :name
|
111
|
+
validates :email, email: true
|
112
|
+
end
|
113
|
+
|
114
|
+
Or any ruby class:
|
115
|
+
|
116
|
+
class User
|
117
|
+
include ActiveModel::Validations
|
118
|
+
attr_accessor :email, :name
|
119
|
+
validates :email, email: true
|
120
|
+
end
|
121
|
+
|
122
|
+
You can specify domains to which the email domain should belong in one of the folowing ways:
|
123
|
+
|
124
|
+
validates :email, email: { domains: 'com' }
|
125
|
+
validates :email, email: { domains: :com }
|
126
|
+
validates :email, email: { domains: [:com, 'edu'] }
|
127
|
+
|
128
|
+
RSpec matcher is also available for your convenience:
|
129
|
+
|
130
|
+
describe User do
|
131
|
+
it { should ensure_valid_email_format_of(:email) }
|
132
|
+
it { should_not ensure_valid_email_format_of(:name) }
|
133
|
+
end
|
134
|
+
|
135
|
+
### EqualityValidator
|
136
|
+
|
137
|
+
**Operators:**
|
138
|
+
* Less than: x < y
|
139
|
+
* Less than or equal to: x <= y
|
140
|
+
* Greater than: x > y
|
141
|
+
* Greater than or equal to: x >= y
|
142
|
+
* Equal to: x == y
|
143
|
+
* Not equal to: x != y
|
144
|
+
|
145
|
+
|
146
|
+
**Rules:**
|
147
|
+
* Equal and not equal to: cannot be nil
|
148
|
+
|
149
|
+
With an ActiveRecord model:
|
150
|
+
|
151
|
+
class Auction < ActiveRecord::Base
|
152
|
+
attr_accessor :bid, :price, :product
|
153
|
+
validates :bid, equality: { operator: :greater_than_or_equal_to, to: :price }
|
154
|
+
end
|
155
|
+
|
156
|
+
Or any ruby class:
|
157
|
+
|
158
|
+
class Auction
|
159
|
+
include ActiveModel::Validations
|
160
|
+
attr_accessor :bid, :price, :product
|
161
|
+
validates :bid, equality: { operator: :greater_than_or_equal_to, to: :price }
|
162
|
+
end
|
163
|
+
|
164
|
+
RSpec matcher is also available for your convenience:
|
165
|
+
|
166
|
+
describe Auction do
|
167
|
+
it { should ensure_equality_of(:bid).to(:price) }
|
168
|
+
it { should_not ensure_equality_of(:bid).to(:product) }
|
169
|
+
end
|
170
|
+
|
171
|
+
### HexValidator
|
172
|
+
|
173
|
+
**Ex:** #a9a9a9 or #999 or aaaaaa
|
174
|
+
|
175
|
+
**Rules:**
|
176
|
+
* Prefix (non-mandatory): #
|
177
|
+
* Length: 3 or 6
|
178
|
+
* Characters: a-f 0-9
|
179
|
+
|
180
|
+
With an ActiveRecord model:
|
181
|
+
|
182
|
+
class Profile < ActiveRecord::Base
|
183
|
+
attr_accessor :color, :trim
|
184
|
+
validates :color, hex: true
|
185
|
+
end
|
186
|
+
|
187
|
+
Or any ruby class:
|
188
|
+
|
189
|
+
class Profile
|
190
|
+
include ActiveModel::Validations
|
191
|
+
attr_accessor :color, :trim
|
192
|
+
validates :color, hex: true
|
193
|
+
end
|
194
|
+
|
195
|
+
RSpec matcher is also available for your convenience:
|
196
|
+
|
197
|
+
describe Color do
|
198
|
+
it { should ensure_valid_hex_format_of(:color) }
|
199
|
+
it { should_not ensure_valid_hex_format_of(:trim) }
|
200
|
+
end
|
201
|
+
|
202
|
+
### ImeiValidator
|
203
|
+
|
204
|
+
**Ex:** 356843052637512 or 35-6843052-637512 or 35.6843052.637512
|
205
|
+
|
206
|
+
**Rules:**
|
207
|
+
* Length: min 14
|
208
|
+
* Characters: 0-9 -.
|
209
|
+
|
210
|
+
With an ActiveRecord model:
|
211
|
+
|
212
|
+
class User < ActiveRecord::Base
|
213
|
+
attr_accessor :imei, :name
|
214
|
+
validates :imei, imei: true
|
215
|
+
end
|
216
|
+
|
217
|
+
Or any ruby class:
|
218
|
+
|
219
|
+
class User
|
220
|
+
include ActiveModel::Validations
|
221
|
+
attr_accessor :imei, :name
|
222
|
+
validates :imei, imei: true
|
223
|
+
end
|
224
|
+
|
225
|
+
RSpec matcher is also available for your convenience:
|
226
|
+
|
227
|
+
describe User do
|
228
|
+
it { should ensure_valid_imei_format_of(:imei) }
|
229
|
+
it { should_not ensure_valid_imei_format_of(:name) }
|
230
|
+
end
|
231
|
+
|
232
|
+
### IpValidator
|
233
|
+
|
234
|
+
**Ex:** 0.0.0.0 or 127.0.0.1 or 167.39.240.31
|
235
|
+
|
236
|
+
**Rules:**
|
237
|
+
* Length: min 7
|
238
|
+
* Characters: 0-9 .
|
239
|
+
|
240
|
+
With an ActiveRecord model:
|
241
|
+
|
242
|
+
class User < ActiveRecord::Base
|
243
|
+
attr_accessor :ip, :name
|
244
|
+
validates :ip, ip: true
|
245
|
+
end
|
246
|
+
|
247
|
+
Or any ruby class:
|
248
|
+
|
249
|
+
class User
|
250
|
+
include ActiveModel::Validations
|
251
|
+
attr_accessor :ip, :name
|
252
|
+
validates :ip, ip: true
|
253
|
+
end
|
254
|
+
|
255
|
+
RSpec matcher is also available for your convenience:
|
256
|
+
|
257
|
+
describe User do
|
258
|
+
it { should ensure_valid_ip_format_of(:ip) }
|
259
|
+
it { should_not ensure_valid_ip_format_of(:name) }
|
260
|
+
end
|
261
|
+
|
262
|
+
### LatitudeValidator
|
263
|
+
|
264
|
+
**Ex:** 78.213 or -34.985
|
265
|
+
|
266
|
+
**Rules:**
|
267
|
+
* Range: 90 to -90
|
268
|
+
* Characters: 0-9
|
269
|
+
|
270
|
+
With an ActiveRecord model:
|
271
|
+
|
272
|
+
class Location < ActiveRecord::Base
|
273
|
+
attr_accessor :lat, :name
|
274
|
+
validates :lat, latitude: true
|
275
|
+
end
|
276
|
+
|
277
|
+
Or any ruby class:
|
278
|
+
|
279
|
+
class Location
|
280
|
+
include ActiveModel::Validations
|
281
|
+
attr_accessor :lat, :name
|
282
|
+
validates :lat, latitude: true
|
283
|
+
end
|
284
|
+
|
285
|
+
RSpec matcher is also available for your convenience:
|
286
|
+
|
287
|
+
describe Location do
|
288
|
+
it { should ensure_valid_latitude_format_of(:lat) }
|
289
|
+
it { should_not ensure_valid_latitude_format_of(:name) }
|
290
|
+
end
|
291
|
+
|
292
|
+
### LongitudeValidator
|
293
|
+
|
294
|
+
**Ex:** 165.371 or -56.152
|
295
|
+
|
296
|
+
**Rules:**
|
297
|
+
* Range: 180 to -180
|
298
|
+
* Characters: 0-9
|
299
|
+
|
300
|
+
With an ActiveRecord model:
|
301
|
+
|
302
|
+
class Location < ActiveRecord::Base
|
303
|
+
attr_accessor :lon, :name
|
304
|
+
validates :lon, longitude: true
|
305
|
+
end
|
306
|
+
|
307
|
+
Or any ruby class:
|
308
|
+
|
309
|
+
class Location
|
310
|
+
include ActiveModel::Validations
|
311
|
+
attr_accessor :lon, :name
|
312
|
+
validates :lon, longitude: true
|
313
|
+
end
|
314
|
+
|
315
|
+
RSpec matcher is also available for your convenience:
|
316
|
+
|
317
|
+
describe Location do
|
318
|
+
it { should ensure_valid_longitude_format_of(:lon) }
|
319
|
+
it { should_not ensure_valid_longitude_format_of(:name) }
|
320
|
+
end
|
321
|
+
|
322
|
+
### MacAddressValidator
|
323
|
+
|
324
|
+
**Ex:**
|
325
|
+
'08:00:2b:01:02:03'
|
326
|
+
'08-00-2b-01-02-03'
|
327
|
+
'08002b:010203'
|
328
|
+
'08002b-010203'
|
329
|
+
'0800.2b01.0203'
|
330
|
+
'08002b010203'
|
331
|
+
|
332
|
+
**Rules:**
|
333
|
+
* Characters: a-z 0-9 -.:
|
334
|
+
|
335
|
+
With an ActiveRecord model:
|
336
|
+
|
337
|
+
class Device < ActiveRecord::Base
|
338
|
+
attr_accessor :mac, :name
|
339
|
+
validates :mac, mac_address: true
|
340
|
+
end
|
341
|
+
|
342
|
+
Or any ruby class:
|
343
|
+
|
344
|
+
class Device
|
345
|
+
include ActiveModel::Validations
|
346
|
+
attr_accessor :mac, :name
|
347
|
+
validates :mac, mac_address: true
|
348
|
+
end
|
349
|
+
|
350
|
+
RSpec matcher is also available for your convenience:
|
351
|
+
|
352
|
+
describe Device do
|
353
|
+
it { should ensure_valid_mac_address_format_of }
|
354
|
+
it { should_not ensure_valid_mac_address_format_of(:name) }
|
355
|
+
end
|
356
|
+
|
357
|
+
### NameValidator
|
358
|
+
|
359
|
+
**Ex:** James Brown or Billy Bob Thorton Jr
|
360
|
+
|
361
|
+
**Rules:**
|
362
|
+
* Range: 2 - 5 names
|
363
|
+
* Characters: a-z -
|
364
|
+
* Must include: First Last
|
365
|
+
|
366
|
+
With an ActiveRecord model:
|
367
|
+
|
368
|
+
class User < ActiveRecord::Base
|
369
|
+
attr_accessor :name, :email
|
370
|
+
validates :name, name: true
|
371
|
+
end
|
372
|
+
|
373
|
+
Or any ruby class:
|
374
|
+
|
375
|
+
class User
|
376
|
+
include ActiveModel::Validations
|
377
|
+
attr_accessor :name, :email
|
378
|
+
validates :name, name: true
|
379
|
+
end
|
380
|
+
|
381
|
+
RSpec matcher is also available for your convenience:
|
382
|
+
|
383
|
+
describe User do
|
384
|
+
it { should ensure_valid_name_format_of(:name) }
|
385
|
+
it { should_not ensure_valid_name_format_of(:email) }
|
386
|
+
end
|
387
|
+
|
388
|
+
### PasswordValidator
|
389
|
+
|
390
|
+
**Ex:** password or password123 or pa!!word
|
391
|
+
|
392
|
+
**Rules:**
|
393
|
+
* Range: 6-18
|
394
|
+
* Characters: a-z 0-9 -_!@#$%^&*
|
395
|
+
|
396
|
+
With an ActiveRecord model:
|
397
|
+
|
398
|
+
class User < ActiveRecord::Base
|
399
|
+
attr_accessor :password, :name
|
400
|
+
validates :password, password: true
|
401
|
+
end
|
402
|
+
|
403
|
+
Or any ruby class:
|
404
|
+
|
405
|
+
class User
|
406
|
+
include ActiveModel::Validations
|
407
|
+
attr_accessor :password, :name
|
408
|
+
validates :password, name: true
|
409
|
+
end
|
410
|
+
|
411
|
+
RSpec matcher is also available for your convenience:
|
412
|
+
|
413
|
+
describe User do
|
414
|
+
it { should ensure_valid_password_format_of(:password) }
|
415
|
+
it { should_not ensure_valid_password_format_of(:name) }
|
416
|
+
end
|
417
|
+
|
418
|
+
### PhoneValidator
|
419
|
+
|
420
|
+
**Ex:** 555 333 4444 or (555) 123-4567 or +1 (555) 123 4567 ext-890
|
421
|
+
|
422
|
+
**Rules:**
|
423
|
+
* Characters: a-z 0-9 -()+
|
424
|
+
|
425
|
+
With an ActiveRecord model:
|
426
|
+
|
427
|
+
class User < ActiveRecord::Base
|
428
|
+
attr_accessor :phone, :name
|
429
|
+
validates :phone, phone: true
|
430
|
+
end
|
431
|
+
|
432
|
+
Or any ruby class:
|
433
|
+
|
434
|
+
class User
|
435
|
+
include ActiveModel::Validations
|
436
|
+
attr_accessor :phone, :name
|
437
|
+
validates :phone, phone: true
|
438
|
+
end
|
439
|
+
|
440
|
+
RSpec matcher is also available for your convenience:
|
441
|
+
|
442
|
+
describe User do
|
443
|
+
it { should ensure_valid_phone_format_of(:phone) }
|
444
|
+
it { should_not ensure_valid_phone_format_of(:name) }
|
445
|
+
end
|
446
|
+
|
447
|
+
### SlugValidator
|
448
|
+
|
449
|
+
**Ex:** slug1234 or slug-1234
|
450
|
+
|
451
|
+
**Rules:**
|
452
|
+
* Characters: a-z 0-9 -
|
453
|
+
|
454
|
+
With an ActiveRecord model:
|
455
|
+
|
456
|
+
class User < ActiveRecord::Base
|
457
|
+
attr_accessor :slug, :name
|
458
|
+
validates :slug, slug: true
|
459
|
+
end
|
460
|
+
|
461
|
+
Or any ruby class:
|
462
|
+
|
463
|
+
class User
|
464
|
+
include ActiveModel::Validations
|
465
|
+
attr_accessor :slug, :name
|
466
|
+
validates :slug, slug: true
|
467
|
+
end
|
468
|
+
|
469
|
+
RSpec matcher is also available for your convenience:
|
470
|
+
|
471
|
+
describe User do
|
472
|
+
it { should ensure_valid_slug_format_of(:slug) }
|
473
|
+
it { should_not ensure_valid_slug_format_of(:name) }
|
474
|
+
end
|
475
|
+
|
476
|
+
### SsnValidator
|
477
|
+
|
478
|
+
**Ex:** 333-22-4444 or 333224444
|
479
|
+
|
480
|
+
**Rules:**
|
481
|
+
* Characters: 0-9 -
|
482
|
+
|
483
|
+
With an ActiveRecord model:
|
484
|
+
|
485
|
+
class User < ActiveRecord::Base
|
486
|
+
attr_accessor :ssn, :name
|
487
|
+
validates :ssn, ssn: true
|
488
|
+
end
|
489
|
+
|
490
|
+
Or any ruby class:
|
491
|
+
|
492
|
+
class User
|
493
|
+
include ActiveModel::Validations
|
494
|
+
attr_accessor :ssn, :name
|
495
|
+
validates :ssn, ssn: true
|
496
|
+
end
|
497
|
+
|
498
|
+
RSpec matcher is also available for your convenience:
|
499
|
+
|
500
|
+
describe User do
|
501
|
+
it { should ensure_valid_ssn_format_of(:ssn) }
|
502
|
+
it { should_not ensure_valid_ssn_format_of(:name) }
|
503
|
+
end
|
504
|
+
|
505
|
+
### UrlValidator
|
506
|
+
|
507
|
+
**Ex:** example.com or http://www.example.com
|
508
|
+
|
509
|
+
**Rules:**
|
510
|
+
* Characters in root: a-z 0-9 -.//:
|
511
|
+
* Characters in domain: a-z 0-9 -
|
512
|
+
* Must include extension: .co, .org, .museum
|
513
|
+
|
514
|
+
With an ActiveRecord model:
|
515
|
+
|
516
|
+
class User < ActiveRecord::Base
|
517
|
+
attr_accessor :url, :name
|
518
|
+
validates :url, url: true
|
519
|
+
end
|
520
|
+
|
521
|
+
Or any ruby class:
|
522
|
+
|
523
|
+
class User
|
524
|
+
include ActiveModel::Validations
|
525
|
+
attr_accessor :url, :name
|
526
|
+
validates :url, url: true
|
527
|
+
end
|
528
|
+
|
529
|
+
You can specify domains to which the URL domain should belong in one of the folowing ways:
|
530
|
+
|
531
|
+
validates :url, url: { domains: 'com' }
|
532
|
+
validates :url, url: { domains: :com }
|
533
|
+
validates :url, url: { domains: [:com, 'edu'] }
|
534
|
+
|
535
|
+
You can specify if the URL should the site root:
|
536
|
+
|
537
|
+
validates :url, url: { root: true }
|
538
|
+
|
539
|
+
You can specify the URL scheme:
|
540
|
+
|
541
|
+
validates :url, url: { scheme: :http }
|
542
|
+
validates :url, url: { scheme: [:http, 'https'] }
|
543
|
+
|
544
|
+
RSpec matcher is also available for your convenience:
|
545
|
+
|
546
|
+
describe User do
|
547
|
+
it { should ensure_valid_url_format_of(:url) }
|
548
|
+
it { should_not ensure_valid_url_format_of(:name) }
|
549
|
+
end
|
550
|
+
|
551
|
+
### UsernameValidator
|
552
|
+
|
553
|
+
**Ex:** username123 or username
|
554
|
+
|
555
|
+
**Rules:**
|
556
|
+
* Range: 2-16
|
557
|
+
* Characters: a-z 0-9 -_
|
558
|
+
|
559
|
+
With an ActiveRecord model:
|
560
|
+
|
561
|
+
class User < ActiveRecord::Base
|
562
|
+
attr_accessor :username, :name
|
563
|
+
validates :username, username: true
|
564
|
+
end
|
565
|
+
|
566
|
+
Or any ruby class:
|
567
|
+
|
568
|
+
class User
|
569
|
+
include ActiveModel::Validations
|
570
|
+
attr_accessor :username, :name
|
571
|
+
validates :username, username: true
|
572
|
+
end
|
573
|
+
|
574
|
+
RSpec matcher is also available for your convenience:
|
575
|
+
|
576
|
+
describe User do
|
577
|
+
it { should ensure_valid_username_format_of(:username) }
|
578
|
+
it { should_not ensure_valid_username_format_of(:name) }
|
579
|
+
end
|
580
|
+
|
581
|
+
## Contributing
|
582
|
+
|
583
|
+
Your contribution is welcome.
|
584
|
+
|
585
|
+
1. Fork it
|
586
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
587
|
+
3. Commit your changes (`git commit -am 'Added some feature'`)
|
588
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
589
|
+
5. Create new Pull Request
|