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