lite-validators 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.fasterer.yml +19 -0
- data/.gitignore +11 -0
- data/.rspec +4 -0
- data/.rubocop.yml +31 -0
- data/.travis.yml +24 -0
- data/CHANGELOG.md +11 -0
- data/CODE_OF_CONDUCT.md +74 -0
- data/Gemfile +6 -0
- data/Gemfile.lock +128 -0
- data/LICENSE.txt +21 -0
- data/README.md +88 -0
- data/Rakefile +8 -0
- data/_config.yml +1 -0
- data/bin/console +15 -0
- data/bin/setup +8 -0
- data/config/locales/en.yml +23 -0
- data/docs/ALPHA.md +29 -0
- data/docs/ALPHA_NUMERIC.md +29 -0
- data/docs/BASE64.md +18 -0
- data/docs/BOOLEAN.md +19 -0
- data/docs/COMPARE.md +30 -0
- data/docs/COORDINATE.md +27 -0
- data/docs/CREDIT_CARD.md +29 -0
- data/docs/CSV.md +44 -0
- data/docs/CURRENCY.md +18 -0
- data/docs/CUSIP.md +20 -0
- data/docs/EMAIL.md +26 -0
- data/docs/FILE_CONTENT_TYPE.md +26 -0
- data/docs/FILE_EXTENSION.md +25 -0
- data/docs/FILE_SIZE.md +38 -0
- data/docs/HEX.md +18 -0
- data/docs/IMEI.md +20 -0
- data/docs/IP_ADDRESS.md +26 -0
- data/docs/ISBN.md +18 -0
- data/docs/ISIN.md +20 -0
- data/docs/MAC_ADDRESS.md +21 -0
- data/docs/NAME.md +18 -0
- data/docs/PASSWORD.md +26 -0
- data/docs/PHONE_NUMBER.md +18 -0
- data/docs/SEDOL.md +20 -0
- data/docs/SLUG.md +18 -0
- data/docs/SSN.md +18 -0
- data/docs/TIME_ZONE.md +17 -0
- data/docs/TYPE.md +28 -0
- data/docs/URL.md +28 -0
- data/docs/USERNAME.md +18 -0
- data/docs/UUID.md +28 -0
- data/lib/lite/validators/alpha_numeric_validator.rb +11 -0
- data/lib/lite/validators/alpha_validator.rb +34 -0
- data/lib/lite/validators/base64_validator.rb +9 -0
- data/lib/lite/validators/base_validator.rb +54 -0
- data/lib/lite/validators/boolean_validator.rb +15 -0
- data/lib/lite/validators/compare_validator.rb +41 -0
- data/lib/lite/validators/coordinate_validator.rb +37 -0
- data/lib/lite/validators/credit_card_validator.rb +144 -0
- data/lib/lite/validators/csv_validator.rb +73 -0
- data/lib/lite/validators/currency_validator.rb +7 -0
- data/lib/lite/validators/cusip_validator.rb +27 -0
- data/lib/lite/validators/email_validator.rb +19 -0
- data/lib/lite/validators/file_content_type_validator.rb +38 -0
- data/lib/lite/validators/file_extension_validator.rb +41 -0
- data/lib/lite/validators/file_size_validator.rb +75 -0
- data/lib/lite/validators/hex_validator.rb +7 -0
- data/lib/lite/validators/imei_validator.rb +27 -0
- data/lib/lite/validators/ip_address_validator.rb +39 -0
- data/lib/lite/validators/isbn_validator.rb +16 -0
- data/lib/lite/validators/isin_validator.rb +36 -0
- data/lib/lite/validators/mac_address_validator.rb +18 -0
- data/lib/lite/validators/name_validator.rb +7 -0
- data/lib/lite/validators/password_validator.rb +29 -0
- data/lib/lite/validators/phone_number_validator.rb +7 -0
- data/lib/lite/validators/sedol_validator.rb +23 -0
- data/lib/lite/validators/slug_validator.rb +7 -0
- data/lib/lite/validators/ssn_validator.rb +7 -0
- data/lib/lite/validators/time_zone_validator.rb +13 -0
- data/lib/lite/validators/type_validator.rb +41 -0
- data/lib/lite/validators/url_validator.rb +61 -0
- data/lib/lite/validators/username_validator.rb +7 -0
- data/lib/lite/validators/uuid_validator.rb +37 -0
- data/lib/lite/validators/version.rb +9 -0
- data/lib/lite/validators.rb +12 -0
- data/lite-validators.gemspec +51 -0
- metadata +265 -0
data/docs/CREDIT_CARD.md
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
# Credit card
|
2
|
+
|
3
|
+
#### Rules
|
4
|
+
|
5
|
+
The rules are for length and prefixes are dynamic based on the provider
|
6
|
+
[view list](https://github.com/drexed/lite-validators/blob/master/lib/lite/validators/credit_card_validator.rb).
|
7
|
+
|
8
|
+
```ruby
|
9
|
+
# 1. Length
|
10
|
+
# 2. Prefix
|
11
|
+
# 3. Checksum
|
12
|
+
```
|
13
|
+
|
14
|
+
#### Options
|
15
|
+
|
16
|
+
Option | Type | Available | Default
|
17
|
+
--- | --- | --- | ---
|
18
|
+
provider | symbol | [view here](https://github.com/drexed/lite-validators/blob/master/lib/lite/validators/credit_card_validator.rb) | all
|
19
|
+
|
20
|
+
#### Usage
|
21
|
+
|
22
|
+
```ruby
|
23
|
+
class User < ActiveRecord::Base
|
24
|
+
|
25
|
+
validates :input_0, credit_card: true
|
26
|
+
validates :input_1, credit_card: { provider: :visa }
|
27
|
+
|
28
|
+
end
|
29
|
+
```
|
data/docs/CSV.md
ADDED
@@ -0,0 +1,44 @@
|
|
1
|
+
# CSV
|
2
|
+
|
3
|
+
#### Rules
|
4
|
+
|
5
|
+
```ruby
|
6
|
+
# 1. Dimension
|
7
|
+
:columns
|
8
|
+
:rows
|
9
|
+
|
10
|
+
# 2. Check
|
11
|
+
:in => '==='
|
12
|
+
:less_than => '<'
|
13
|
+
:less_than_or_equal_to => '<='
|
14
|
+
:greater_than => '>'
|
15
|
+
:greater_than_or_equal_to => '>='
|
16
|
+
:equal_to => '=='
|
17
|
+
:not_equal_to => '!='
|
18
|
+
```
|
19
|
+
|
20
|
+
#### Options
|
21
|
+
|
22
|
+
Option | Type | Available | Default
|
23
|
+
--- | --- | --- | ---
|
24
|
+
columns | hash | |
|
25
|
+
rows | hash | |
|
26
|
+
in | range | |
|
27
|
+
less_than | numeric | |
|
28
|
+
less_than_or_equal_to | numeric | |
|
29
|
+
greater_than | numeric | |
|
30
|
+
greater_than_or_equal_to | numeric | |
|
31
|
+
equal_to | numeric | |
|
32
|
+
not_equal_to | numeric | |
|
33
|
+
|
34
|
+
#### Usage
|
35
|
+
|
36
|
+
```ruby
|
37
|
+
class User < ActiveRecord::Base
|
38
|
+
|
39
|
+
validates :input_0, csv: { columns: { less_than: 100 } }
|
40
|
+
validates :input_1, csv: { rows: { in: (50..100) } }
|
41
|
+
validates :input_2, csv: { columns: { equal_to: 5 }, rows: { greater_than: 5 } }
|
42
|
+
|
43
|
+
end
|
44
|
+
```
|
data/docs/CURRENCY.md
ADDED
data/docs/CUSIP.md
ADDED
data/docs/EMAIL.md
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
# Email
|
2
|
+
|
3
|
+
#### Rules
|
4
|
+
|
5
|
+
```ruby
|
6
|
+
# 1. Pattern
|
7
|
+
/\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\z/i
|
8
|
+
```
|
9
|
+
|
10
|
+
#### Options
|
11
|
+
|
12
|
+
Option | Type | Available | Default
|
13
|
+
--- | --- | --- | ---
|
14
|
+
domain | array, string, symbol | |
|
15
|
+
|
16
|
+
#### Usage
|
17
|
+
|
18
|
+
```ruby
|
19
|
+
class User < ActiveRecord::Base
|
20
|
+
|
21
|
+
validates :input_0, email: true
|
22
|
+
validates :input_1, email: { domain: 'com' }
|
23
|
+
validates :input_2, email: { domain: %w[com org] }
|
24
|
+
|
25
|
+
end
|
26
|
+
```
|
@@ -0,0 +1,26 @@
|
|
1
|
+
# File content type
|
2
|
+
|
3
|
+
#### Rules
|
4
|
+
|
5
|
+
```ruby
|
6
|
+
# 1. Check
|
7
|
+
```
|
8
|
+
|
9
|
+
#### Options
|
10
|
+
|
11
|
+
Option | Type | Available | Default
|
12
|
+
--- | --- | --- | ---
|
13
|
+
include | array, regexp, string, symbol | |
|
14
|
+
exclude | array, regexp, string, symbol | |
|
15
|
+
|
16
|
+
#### Usage
|
17
|
+
|
18
|
+
```ruby
|
19
|
+
class User < ActiveRecord::Base
|
20
|
+
|
21
|
+
validates :input_0, file_content_type: { include: 'text/csv' }
|
22
|
+
validates :input_1, file_content_type: { exclude: %w[image/png text/plain] }
|
23
|
+
validates :input_2, file_content_type: { include: /^text\/.*/, exclude: 'text/csv' }
|
24
|
+
|
25
|
+
end
|
26
|
+
```
|
@@ -0,0 +1,25 @@
|
|
1
|
+
# File extension
|
2
|
+
|
3
|
+
#### Rules
|
4
|
+
|
5
|
+
```ruby
|
6
|
+
# 1. Check
|
7
|
+
```
|
8
|
+
|
9
|
+
#### Options
|
10
|
+
|
11
|
+
Option | Type | Available | Default
|
12
|
+
--- | --- | --- | ---
|
13
|
+
include | array, string, symbol | |
|
14
|
+
exclude | array, string, symbol | |
|
15
|
+
|
16
|
+
#### Usage
|
17
|
+
|
18
|
+
```ruby
|
19
|
+
class User < ActiveRecord::Base
|
20
|
+
|
21
|
+
validates :input_0, file_extension: { include: 'csv' }
|
22
|
+
validates :input_1, file_extension: { exclude: [:png, 'text'] }
|
23
|
+
|
24
|
+
end
|
25
|
+
```
|
data/docs/FILE_SIZE.md
ADDED
@@ -0,0 +1,38 @@
|
|
1
|
+
# File size
|
2
|
+
|
3
|
+
#### Rules
|
4
|
+
|
5
|
+
```ruby
|
6
|
+
# 1. Check
|
7
|
+
:in => '==='
|
8
|
+
:less_than => '<'
|
9
|
+
:less_than_or_equal_to => '<='
|
10
|
+
:greater_than => '>'
|
11
|
+
:greater_than_or_equal_to => '>='
|
12
|
+
:equal_to => '=='
|
13
|
+
:not_equal_to => '!='
|
14
|
+
```
|
15
|
+
|
16
|
+
#### Options
|
17
|
+
|
18
|
+
Option | Type | Available | Default
|
19
|
+
--- | --- | --- | ---
|
20
|
+
in | range | |
|
21
|
+
less_than | numeric | |
|
22
|
+
less_than_or_equal_to | numeric | |
|
23
|
+
greater_than | numeric | |
|
24
|
+
greater_than_or_equal_to | numeric | |
|
25
|
+
equal_to | numeric | |
|
26
|
+
not_equal_to | numeric | |
|
27
|
+
|
28
|
+
#### Usage
|
29
|
+
|
30
|
+
```ruby
|
31
|
+
class User < ActiveRecord::Base
|
32
|
+
|
33
|
+
validates :input_0, file_size: { less_than: 100 }
|
34
|
+
validates :input_1, file_size: { greater_than_or_equal_to: 75, less_than: 100 }
|
35
|
+
validates :input_0, file_size: { in: (50..100) }
|
36
|
+
|
37
|
+
end
|
38
|
+
```
|
data/docs/HEX.md
ADDED
data/docs/IMEI.md
ADDED
data/docs/IP_ADDRESS.md
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
# IP address
|
2
|
+
|
3
|
+
#### Rules
|
4
|
+
|
5
|
+
```ruby
|
6
|
+
# 1. Pattern
|
7
|
+
:ipv4 => Resolv::IPv4::Regex
|
8
|
+
:ipv6 => Resolv::IPv6::Regex
|
9
|
+
```
|
10
|
+
|
11
|
+
#### Options
|
12
|
+
|
13
|
+
Option | Type | Available | Default
|
14
|
+
--- | --- | --- | ---
|
15
|
+
protocol | symbol | any, ipv4, ipv6 | any
|
16
|
+
|
17
|
+
#### Usage
|
18
|
+
|
19
|
+
```ruby
|
20
|
+
class User < ActiveRecord::Base
|
21
|
+
|
22
|
+
validates :input_0, ip_address: true
|
23
|
+
validates :input_1, ip_address: { protocol: :ipv4 }
|
24
|
+
|
25
|
+
end
|
26
|
+
```
|
data/docs/ISBN.md
ADDED
data/docs/ISIN.md
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
# ISIN
|
2
|
+
|
3
|
+
#### Rules
|
4
|
+
|
5
|
+
```ruby
|
6
|
+
# 1. Pattern
|
7
|
+
/^((AF|AX|AL|DZ|AS|AD|AO|AI|AQ|AG|AR|AM|AW|AU|AT|AZ|BS|BH|BD|BB|BY|BE|BZ|BJ|BM|BT|BO|BQ|BA|BW|BV|BR|IO|BN|BG|BF|BI|KH|CM|CA|CV|KY|CF|TD|CL|CN|CX|CC|CO|KM|CG|CD|CK|CR|CI|HR|CU|CW|CY|CZ|DK|DJ|DM|DO|EC|EG|SV|GQ|ER|EE|ET|FK|FO|FJ|FI|FR|GF|PF|TF|GA|GM|GE|DE|GH|GI|GR|GL|GD|GP|GU|GT|GG|GN|GW|GY|HT|HM|VA|HN|HK|HU|IS|IN|ID|IR|IQ|IE|IM|IL|IT|JM|JP|JE|JO|KZ|KE|KI|KP|KR|KW|KG|LA|LV|LB|LS|LR|LY|LI|LT|LU|MO|MK|MG|MW|MY|MV|ML|MT|MH|MQ|MR|MU|YT|MX|FM|MD|MC|MN|ME|MS|MA|MZ|MM|NA|NR|NP|NL|NC|NZ|NI|NE|NG|NU|NF|MP|NO|OM|PK|PW|PS|PA|PG|PY|PE|PH|PN|PL|PT|PR|QA|RE|RO|RU|RW|BL|SH|KN|LC|MF|PM|VC|WS|SM|ST|SA|SN|RS|SC|SL|SG|SX|SK|SI|SB|SO|ZA|GS|SS|ES|LK|SD|SR|SJ|SZ|SE|CH|SY|TW|TJ|TZ|TH|TL|TG|TK|TO|TT|TN|TR|TM|TC|TV|UG|UA|AE|GB|US|UM|UY|UZ|VU|VE|VN|VG|VI|WF|EH|YE|ZM|ZW{2})([A-Z0-9]{9}))(\d{1})$/
|
8
|
+
|
9
|
+
# 2. Checksum
|
10
|
+
```
|
11
|
+
|
12
|
+
#### Usage
|
13
|
+
|
14
|
+
```ruby
|
15
|
+
class User < ActiveRecord::Base
|
16
|
+
|
17
|
+
validates :input_0, isin: true
|
18
|
+
|
19
|
+
end
|
20
|
+
```
|
data/docs/MAC_ADDRESS.md
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
# MAC address
|
2
|
+
|
3
|
+
#### Rules
|
4
|
+
|
5
|
+
```ruby
|
6
|
+
# 1. Pattern
|
7
|
+
'2/5/2' => /^([\h]{2}[-|\.:|\s]){5}[\h]{2}?$/i
|
8
|
+
'4/2/4' => /^([\h]{4}[-|\.:|\s]){2}[\h]{4}?$/i
|
9
|
+
'6/6' => /^([\h]{6}[-|\.:|\s])[\h]{6}?$/i
|
10
|
+
'12' => /^[\h]{12}?$/i
|
11
|
+
```
|
12
|
+
|
13
|
+
#### Usage
|
14
|
+
|
15
|
+
```ruby
|
16
|
+
class User < ActiveRecord::Base
|
17
|
+
|
18
|
+
validates :input_0, mac_address: true
|
19
|
+
|
20
|
+
end
|
21
|
+
```
|
data/docs/NAME.md
ADDED
data/docs/PASSWORD.md
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
# Password
|
2
|
+
|
3
|
+
#### Rules
|
4
|
+
|
5
|
+
```ruby
|
6
|
+
# 1. Pattern
|
7
|
+
:strong => /\A(?=.{4,255})(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[[:^alnum:]])/x
|
8
|
+
:weak => /^[A-Za-z0-9.,<>:;!@#$%^&*?_~-]{1,255}$/
|
9
|
+
```
|
10
|
+
|
11
|
+
#### Options
|
12
|
+
|
13
|
+
Option | Type | Available | Default
|
14
|
+
--- | --- | --- | ---
|
15
|
+
strength | symbol | strong, weak | weak
|
16
|
+
|
17
|
+
#### Usage
|
18
|
+
|
19
|
+
```ruby
|
20
|
+
class User < ActiveRecord::Base
|
21
|
+
|
22
|
+
validates :input_0, password: true
|
23
|
+
validates :input_1, password: { strength: :strong }
|
24
|
+
|
25
|
+
end
|
26
|
+
```
|
data/docs/SEDOL.md
ADDED
data/docs/SLUG.md
ADDED
data/docs/SSN.md
ADDED
data/docs/TIME_ZONE.md
ADDED
data/docs/TYPE.md
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
# Type
|
2
|
+
|
3
|
+
#### Rules
|
4
|
+
|
5
|
+
```ruby
|
6
|
+
# 1. Check
|
7
|
+
```
|
8
|
+
|
9
|
+
#### Options
|
10
|
+
|
11
|
+
Option | Type | Available | Default
|
12
|
+
--- | --- | --- | ---
|
13
|
+
is | class | |
|
14
|
+
is_not | class | |
|
15
|
+
|
16
|
+
#### Usage
|
17
|
+
|
18
|
+
```ruby
|
19
|
+
class User < ActiveRecord::Base
|
20
|
+
|
21
|
+
validates :input_0, type: { is: Array }
|
22
|
+
validates :input_1, type: { is: Boolean } #=> Checks both TrueClass and FalseClass
|
23
|
+
validates :input_2, type: { is: [Array, Hash] }
|
24
|
+
validates :input_3, type: { is_not: CustomKlass } #=> Any Class based objects
|
25
|
+
validates :input_4, type: { is_not: Integer }
|
26
|
+
|
27
|
+
end
|
28
|
+
```
|
data/docs/URL.md
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
# URL
|
2
|
+
|
3
|
+
#### Rules
|
4
|
+
|
5
|
+
```ruby
|
6
|
+
# 1. Pattern
|
7
|
+
```
|
8
|
+
|
9
|
+
#### Options
|
10
|
+
|
11
|
+
Option | Type | Available | Default
|
12
|
+
--- | --- | --- | ---
|
13
|
+
domain | array, string, symbol | |
|
14
|
+
root_only | boolean | true, false | false
|
15
|
+
scheme | array, string, symbol | |
|
16
|
+
|
17
|
+
#### Usage
|
18
|
+
|
19
|
+
```ruby
|
20
|
+
class User < ActiveRecord::Base
|
21
|
+
|
22
|
+
validates :input_0, url: true
|
23
|
+
validates :input_1, url: { domain: [:com, 'org'] }
|
24
|
+
validates :input_2, url: { scheme: :https }
|
25
|
+
validates :input_3, url: { root_only: true }
|
26
|
+
|
27
|
+
end
|
28
|
+
```
|
data/docs/USERNAME.md
ADDED
data/docs/UUID.md
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
# UUID
|
2
|
+
|
3
|
+
#### Rules
|
4
|
+
|
5
|
+
```ruby
|
6
|
+
# 1. Pattern
|
7
|
+
:any => /^[0-9A-F]{8}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{12}$/i
|
8
|
+
3 => /^[0-9A-F]{8}-[0-9A-F]{4}-3[0-9A-F]{3}-[0-9A-F]{4}-[0-9A-F]{12}$/i
|
9
|
+
4 => /^[0-9A-F]{8}-[0-9A-F]{4}-4[0-9A-F]{3}-[89AB][0-9A-F]{3}-[0-9A-F]{12}$/i
|
10
|
+
5 => /^[0-9A-F]{8}-[0-9A-F]{4}-5[0-9A-F]{3}-[89AB][0-9A-F]{3}-[0-9A-F]{12}$/i
|
11
|
+
```
|
12
|
+
|
13
|
+
#### Options
|
14
|
+
|
15
|
+
Option | Type | Available | Default
|
16
|
+
--- | --- | --- | ---
|
17
|
+
version | symbol, integer | any, 3, 4, 5 | any
|
18
|
+
|
19
|
+
#### Usage
|
20
|
+
|
21
|
+
```ruby
|
22
|
+
class User < ActiveRecord::Base
|
23
|
+
|
24
|
+
validates :input_0, uuid: true
|
25
|
+
validates :input_1, uuid: { version: 3 }
|
26
|
+
|
27
|
+
end
|
28
|
+
```
|
@@ -0,0 +1,34 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
class AlphaValidator < BaseValidator
|
4
|
+
|
5
|
+
CASES ||= {
|
6
|
+
lower: 'a-z',
|
7
|
+
upper: 'A-Z',
|
8
|
+
any: 'A-Za-z'
|
9
|
+
}.freeze
|
10
|
+
|
11
|
+
def validate_each(record, attribute, value)
|
12
|
+
assert_valid_case!
|
13
|
+
super
|
14
|
+
end
|
15
|
+
|
16
|
+
private
|
17
|
+
|
18
|
+
def assert_valid_case!
|
19
|
+
assert_valid_option!(:case, CASES.keys, option: kase)
|
20
|
+
end
|
21
|
+
|
22
|
+
def kase
|
23
|
+
options[:case] || :any
|
24
|
+
end
|
25
|
+
|
26
|
+
def regexp
|
27
|
+
/^[#{CASES[kase]}#{' ' if options[:allow_space]}]+$/
|
28
|
+
end
|
29
|
+
|
30
|
+
def valid_regexp?
|
31
|
+
value.to_s =~ regexp
|
32
|
+
end
|
33
|
+
|
34
|
+
end
|