lite-validators 1.5.0 → 1.7.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +14 -0
- data/Gemfile.lock +24 -24
- data/README.md +2 -0
- data/docs/LIST.md +28 -0
- data/docs/REFERENCE.md +27 -0
- data/lib/lite/validators/compare_validator.rb +2 -2
- data/lib/lite/validators/file_size_validator.rb +2 -2
- data/lib/lite/validators/ip_address_validator.rb +1 -1
- data/lib/lite/validators/list_validator.rb +35 -0
- data/lib/lite/validators/reference_validator.rb +37 -0
- data/lib/lite/validators/url_validator.rb +1 -1
- data/lib/lite/validators/version.rb +1 -1
- data/lib/lite/validators.rb +2 -0
- metadata +6 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 0cbc212144b10828145cc04c3117cd994c6b5b5ffd6539b8e9cb08d45e05b15b
|
4
|
+
data.tar.gz: 31deca154872899b7faa80838416c68c1830d97982c948301dc373238bc1a3b1
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: da30d06d89209f263bd559b008db96618947228c0a4ca358df3301fdbcc09adcfe04e6e2b0b07d426ca15cff22baf8e804deefc21e80e08b94372d2bee912535
|
7
|
+
data.tar.gz: a6219df0e0563e79ded1216f7b81794bbf02f85aa4c68fa69b395b0a31a9a2286a3d35076dc9a1aadd67955f6badf63b76dd06f69b4437d633f92183d583d646
|
data/CHANGELOG.md
CHANGED
@@ -6,6 +6,20 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|
6
6
|
|
7
7
|
## [Unreleased]
|
8
8
|
|
9
|
+
## [1.7.1] - 2022-03-17
|
10
|
+
### Changed
|
11
|
+
- Added polymorphic support to reference validator
|
12
|
+
|
13
|
+
## [1.7.0] - 2022-03-17
|
14
|
+
### Added
|
15
|
+
- Added Reference validator
|
16
|
+
|
17
|
+
## [1.6.0] - 2022-02-17
|
18
|
+
### Added
|
19
|
+
- Added List validator
|
20
|
+
### Changed
|
21
|
+
- Update to use public_send instead of send
|
22
|
+
|
9
23
|
## [1.5.0] - 2022-02-17
|
10
24
|
### Changed
|
11
25
|
- Added `include_address` to ip_address validator
|
data/Gemfile.lock
CHANGED
@@ -1,28 +1,28 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
lite-validators (1.
|
4
|
+
lite-validators (1.7.1)
|
5
5
|
activemodel
|
6
6
|
|
7
7
|
GEM
|
8
8
|
remote: https://rubygems.org/
|
9
9
|
specs:
|
10
|
-
actionpack (7.0.2.
|
11
|
-
actionview (= 7.0.2.
|
12
|
-
activesupport (= 7.0.2.
|
10
|
+
actionpack (7.0.2.3)
|
11
|
+
actionview (= 7.0.2.3)
|
12
|
+
activesupport (= 7.0.2.3)
|
13
13
|
rack (~> 2.0, >= 2.2.0)
|
14
14
|
rack-test (>= 0.6.3)
|
15
15
|
rails-dom-testing (~> 2.0)
|
16
16
|
rails-html-sanitizer (~> 1.0, >= 1.2.0)
|
17
|
-
actionview (7.0.2.
|
18
|
-
activesupport (= 7.0.2.
|
17
|
+
actionview (7.0.2.3)
|
18
|
+
activesupport (= 7.0.2.3)
|
19
19
|
builder (~> 3.1)
|
20
20
|
erubi (~> 1.4)
|
21
21
|
rails-dom-testing (~> 2.0)
|
22
22
|
rails-html-sanitizer (~> 1.1, >= 1.2.0)
|
23
|
-
activemodel (7.0.2.
|
24
|
-
activesupport (= 7.0.2.
|
25
|
-
activesupport (7.0.2.
|
23
|
+
activemodel (7.0.2.3)
|
24
|
+
activesupport (= 7.0.2.3)
|
25
|
+
activesupport (7.0.2.3)
|
26
26
|
concurrent-ruby (~> 1.0, >= 1.0.2)
|
27
27
|
i18n (>= 1.6, < 2)
|
28
28
|
minitest (>= 5.1)
|
@@ -39,17 +39,17 @@ GEM
|
|
39
39
|
ruby_parser (>= 3.14.1)
|
40
40
|
i18n (1.10.0)
|
41
41
|
concurrent-ruby (~> 1.0)
|
42
|
-
loofah (2.
|
42
|
+
loofah (2.15.0)
|
43
43
|
crass (~> 1.0.2)
|
44
44
|
nokogiri (>= 1.5.9)
|
45
45
|
method_source (1.0.0)
|
46
|
-
mini_portile2 (2.
|
46
|
+
mini_portile2 (2.8.0)
|
47
47
|
minitest (5.15.0)
|
48
|
-
nokogiri (1.13.
|
49
|
-
mini_portile2 (~> 2.
|
48
|
+
nokogiri (1.13.3)
|
49
|
+
mini_portile2 (~> 2.8.0)
|
50
50
|
racc (~> 1.4)
|
51
51
|
parallel (1.21.0)
|
52
|
-
parser (3.1.
|
52
|
+
parser (3.1.1.0)
|
53
53
|
ast (~> 2.4.1)
|
54
54
|
racc (1.6.0)
|
55
55
|
rack (2.2.3)
|
@@ -60,9 +60,9 @@ GEM
|
|
60
60
|
nokogiri (>= 1.6)
|
61
61
|
rails-html-sanitizer (1.4.2)
|
62
62
|
loofah (~> 2.3)
|
63
|
-
railties (7.0.2.
|
64
|
-
actionpack (= 7.0.2.
|
65
|
-
activesupport (= 7.0.2.
|
63
|
+
railties (7.0.2.3)
|
64
|
+
actionpack (= 7.0.2.3)
|
65
|
+
activesupport (= 7.0.2.3)
|
66
66
|
method_source
|
67
67
|
rake (>= 12.2)
|
68
68
|
thor (~> 1.0)
|
@@ -83,7 +83,7 @@ GEM
|
|
83
83
|
rspec-mocks (3.11.0)
|
84
84
|
diff-lcs (>= 1.2.0, < 2.0)
|
85
85
|
rspec-support (~> 3.11.0)
|
86
|
-
rspec-rails (5.1.
|
86
|
+
rspec-rails (5.1.1)
|
87
87
|
actionpack (>= 5.2)
|
88
88
|
activesupport (>= 5.2)
|
89
89
|
railties (>= 5.2)
|
@@ -92,23 +92,23 @@ GEM
|
|
92
92
|
rspec-mocks (~> 3.10)
|
93
93
|
rspec-support (~> 3.10)
|
94
94
|
rspec-support (3.11.0)
|
95
|
-
rubocop (1.
|
95
|
+
rubocop (1.26.0)
|
96
96
|
parallel (~> 1.10)
|
97
97
|
parser (>= 3.1.0.0)
|
98
98
|
rainbow (>= 2.2.2, < 4.0)
|
99
99
|
regexp_parser (>= 1.8, < 3.0)
|
100
100
|
rexml
|
101
|
-
rubocop-ast (>= 1.
|
101
|
+
rubocop-ast (>= 1.16.0, < 2.0)
|
102
102
|
ruby-progressbar (~> 1.7)
|
103
103
|
unicode-display_width (>= 1.4.0, < 3.0)
|
104
|
-
rubocop-ast (1.
|
105
|
-
parser (>= 3.
|
106
|
-
rubocop-performance (1.13.
|
104
|
+
rubocop-ast (1.16.0)
|
105
|
+
parser (>= 3.1.1.0)
|
106
|
+
rubocop-performance (1.13.3)
|
107
107
|
rubocop (>= 1.7.0, < 2.0)
|
108
108
|
rubocop-ast (>= 0.4.0)
|
109
109
|
rubocop-rake (0.6.0)
|
110
110
|
rubocop (~> 1.0)
|
111
|
-
rubocop-rspec (2.
|
111
|
+
rubocop-rspec (2.9.0)
|
112
112
|
rubocop (~> 1.19)
|
113
113
|
ruby-progressbar (1.11.0)
|
114
114
|
ruby_parser (3.18.1)
|
data/README.md
CHANGED
@@ -51,10 +51,12 @@ Or install it yourself as:
|
|
51
51
|
* [IP address](https://github.com/drexed/lite-validators/blob/master/docs/IP_ADDRESS.md)
|
52
52
|
* [ISBN](https://github.com/drexed/lite-validators/blob/master/docs/ISBN.md)
|
53
53
|
* [ISIN](https://github.com/drexed/lite-validators/blob/master/docs/ISIN.md)
|
54
|
+
* [List](https://github.com/drexed/lite-validators/blob/master/docs/LIST.md)
|
54
55
|
* [MAC address](https://github.com/drexed/lite-validators/blob/master/docs/MAC.md)
|
55
56
|
* [Name](https://github.com/drexed/lite-validators/blob/master/docs/NAME.md)
|
56
57
|
* [Password](https://github.com/drexed/lite-validators/blob/master/docs/PASSWORD.md)
|
57
58
|
* [Phone number](https://github.com/drexed/lite-validators/blob/master/docs/PHONE_NUMBER.md)
|
59
|
+
* [Reference](https://github.com/drexed/lite-validators/blob/master/docs/REFERENCE.md)
|
58
60
|
* [SEDOL](https://github.com/drexed/lite-validators/blob/master/docs/SEDOL.md)
|
59
61
|
* [Slug](https://github.com/drexed/lite-validators/blob/master/docs/SLUG.md)
|
60
62
|
* [SSN](https://github.com/drexed/lite-validators/blob/master/docs/SSN.md)
|
data/docs/LIST.md
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
# List
|
2
|
+
|
3
|
+
#### Rules
|
4
|
+
|
5
|
+
```ruby
|
6
|
+
# 1. Check
|
7
|
+
```
|
8
|
+
|
9
|
+
#### Options
|
10
|
+
|
11
|
+
Option | Type | Available | Default
|
12
|
+
--- | --- | --- | ---
|
13
|
+
all | array | |
|
14
|
+
any | array | |
|
15
|
+
none | array | |
|
16
|
+
|
17
|
+
#### Usage
|
18
|
+
|
19
|
+
```ruby
|
20
|
+
class User < ActiveRecord::Base
|
21
|
+
|
22
|
+
validates :input0, list: { all: %w[monday tuesday] }
|
23
|
+
validates :input1, list: { any: %w[saturday sunday] }
|
24
|
+
validates :input2, list: { none: %w[wednesday thursday] }
|
25
|
+
validates :input3, list: { any: %w[saturday sunday], none: %w[wednesday thursday] }
|
26
|
+
|
27
|
+
end
|
28
|
+
```
|
data/docs/REFERENCE.md
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
# Reference
|
2
|
+
|
3
|
+
#### Rules
|
4
|
+
|
5
|
+
```ruby
|
6
|
+
# 1. Pattern
|
7
|
+
/\A[+-]?\d+\z/
|
8
|
+
```
|
9
|
+
|
10
|
+
#### Options
|
11
|
+
|
12
|
+
Option | Type | Available | Default
|
13
|
+
--- | --- | --- | ---
|
14
|
+
association | symbol | object method | attribute without `_id`
|
15
|
+
polymorphic | symbol | true, false | false
|
16
|
+
|
17
|
+
#### Usage
|
18
|
+
|
19
|
+
```ruby
|
20
|
+
class User < ActiveRecord::Base
|
21
|
+
|
22
|
+
validates :input_id, reference: true
|
23
|
+
validates :output_id, reference: { polymorphic: true }
|
24
|
+
validates :output_id, reference: { association: :input }
|
25
|
+
|
26
|
+
end
|
27
|
+
```
|
@@ -59,9 +59,9 @@ class FileSizeValidator < BaseValidator
|
|
59
59
|
return false if size.nil?
|
60
60
|
|
61
61
|
if option_value.is_a?(Range)
|
62
|
-
option_value.
|
62
|
+
option_value.public_send(CHECKS[option], size)
|
63
63
|
else
|
64
|
-
size.
|
64
|
+
size.public_send(CHECKS[option], option_value)
|
65
65
|
end
|
66
66
|
end
|
67
67
|
|
@@ -37,7 +37,7 @@ class IpAddressValidator < BaseValidator
|
|
37
37
|
return true unless addresses
|
38
38
|
|
39
39
|
check = options[:include_address] ? :any? : :none?
|
40
|
-
check = Array(addresses).
|
40
|
+
check = Array(addresses).public_send(check) { |address| value.include?(address.to_s) }
|
41
41
|
return true if check
|
42
42
|
|
43
43
|
record.errors.add(attribute, error_message_for(:address))
|
@@ -0,0 +1,35 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
class ListValidator < BaseValidator
|
4
|
+
|
5
|
+
def validate_each(record, attribute, value)
|
6
|
+
assert_valid_all_or_any_or_none!
|
7
|
+
assign_attr_readers(record, attribute, value)
|
8
|
+
valid?
|
9
|
+
end
|
10
|
+
|
11
|
+
private
|
12
|
+
|
13
|
+
def assert_valid_all_or_any_or_none!
|
14
|
+
return if options.key?(:all) || options.key?(:any) || options.key?(:none)
|
15
|
+
|
16
|
+
raise ArgumentError, 'Missing ":all", ":any" or ":none" attribute for comparison.'
|
17
|
+
end
|
18
|
+
|
19
|
+
def valid_attr?
|
20
|
+
options.slice(:all, :any, :none).each do |option, option_value|
|
21
|
+
validate_check(option, option_value)
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
def valid_content_type?(option, option_value)
|
26
|
+
Array(value).public_send("#{option}?") { |val| option_value.include?(val) }
|
27
|
+
end
|
28
|
+
|
29
|
+
def validate_check(option, option_value)
|
30
|
+
return if valid_content_type?(option, option_value)
|
31
|
+
|
32
|
+
record.errors.add(attribute, *error_message)
|
33
|
+
end
|
34
|
+
|
35
|
+
end
|
@@ -0,0 +1,37 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
class ReferenceValidator < BaseValidator
|
4
|
+
|
5
|
+
REGEXP = /\A[+-]?\d+\z/
|
6
|
+
|
7
|
+
private
|
8
|
+
|
9
|
+
def valid?
|
10
|
+
(valid_id? && valid_type?) || valid_object?
|
11
|
+
end
|
12
|
+
|
13
|
+
def valid_id?
|
14
|
+
if value.blank?
|
15
|
+
false
|
16
|
+
elsif !valid_regexp?
|
17
|
+
record.errors.add(attribute, *error_message)
|
18
|
+
else
|
19
|
+
!value.to_i.negative?
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
def valid_object?
|
24
|
+
association_object = options[:association] || attribute.to_s.chomp('_id')
|
25
|
+
return true if record.send(association_object).present?
|
26
|
+
|
27
|
+
record.errors.add(association_object, *error_message)
|
28
|
+
end
|
29
|
+
|
30
|
+
def valid_type?
|
31
|
+
return true unless options[:polymorphic]
|
32
|
+
|
33
|
+
association_type = "#{attribute.to_s.chomp('_id')}_type"
|
34
|
+
record.send(association_type).present?
|
35
|
+
end
|
36
|
+
|
37
|
+
end
|
@@ -44,7 +44,7 @@ class UrlValidator < BaseValidator
|
|
44
44
|
|
45
45
|
value_downcased = value.host.to_s.downcase
|
46
46
|
check = options[:include_host] ? :any? : :none?
|
47
|
-
check = Array(hosts).
|
47
|
+
check = Array(hosts).public_send(check) { |host| value_downcased.include?(host.to_s.downcase) }
|
48
48
|
return true if check
|
49
49
|
|
50
50
|
record.errors.add(attribute, error_message_for(:host))
|
data/lib/lite/validators.rb
CHANGED
@@ -25,10 +25,12 @@ require 'lite/validators/imei_validator'
|
|
25
25
|
require 'lite/validators/ip_address_validator'
|
26
26
|
require 'lite/validators/isbn_validator'
|
27
27
|
require 'lite/validators/isin_validator'
|
28
|
+
require 'lite/validators/list_validator'
|
28
29
|
require 'lite/validators/mac_address_validator'
|
29
30
|
require 'lite/validators/name_validator'
|
30
31
|
require 'lite/validators/password_validator'
|
31
32
|
require 'lite/validators/phone_number_validator'
|
33
|
+
require 'lite/validators/reference_validator'
|
32
34
|
require 'lite/validators/sedol_validator'
|
33
35
|
require 'lite/validators/slug_validator'
|
34
36
|
require 'lite/validators/ssn_validator'
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: lite-validators
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.7.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Juan Gomez
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2022-
|
11
|
+
date: 2022-03-18 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activemodel
|
@@ -205,10 +205,12 @@ files:
|
|
205
205
|
- docs/IP_ADDRESS.md
|
206
206
|
- docs/ISBN.md
|
207
207
|
- docs/ISIN.md
|
208
|
+
- docs/LIST.md
|
208
209
|
- docs/MAC_ADDRESS.md
|
209
210
|
- docs/NAME.md
|
210
211
|
- docs/PASSWORD.md
|
211
212
|
- docs/PHONE_NUMBER.md
|
213
|
+
- docs/REFERENCE.md
|
212
214
|
- docs/SEDOL.md
|
213
215
|
- docs/SLUG.md
|
214
216
|
- docs/SSN.md
|
@@ -239,11 +241,13 @@ files:
|
|
239
241
|
- lib/lite/validators/ip_address_validator.rb
|
240
242
|
- lib/lite/validators/isbn_validator.rb
|
241
243
|
- lib/lite/validators/isin_validator.rb
|
244
|
+
- lib/lite/validators/list_validator.rb
|
242
245
|
- lib/lite/validators/mac_address_validator.rb
|
243
246
|
- lib/lite/validators/name_validator.rb
|
244
247
|
- lib/lite/validators/password_validator.rb
|
245
248
|
- lib/lite/validators/phone_number_validator.rb
|
246
249
|
- lib/lite/validators/railtie.rb
|
250
|
+
- lib/lite/validators/reference_validator.rb
|
247
251
|
- lib/lite/validators/sedol_validator.rb
|
248
252
|
- lib/lite/validators/slug_validator.rb
|
249
253
|
- lib/lite/validators/ssn_validator.rb
|