type_validator 0.9.0 → 0.10.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 +4 -4
- data/README.md +21 -3
- data/lib/type_validator/error.rb +1 -1
- data/lib/type_validator/version.rb +1 -1
- data/lib/type_validator.rb +27 -9
- data/type_validator.gemspec +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: c6d336fb78cd054bc714f18871e7fdf708526d29beb9c7d19ec6156f20e627e7
|
4
|
+
data.tar.gz: 2ae7196178b31af7ee65bdf7c545869a84c2895a6ca6e7064d486b84fd5817fb
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e867c62076ccf8355855e5b4e92753c6f7cc2121de1eb66a2c2474fcd650272072b1429c37428336efe0c579c35ce302c9224a8fec302bf763de4c1e14c3fdc2
|
7
|
+
data.tar.gz: 396af9fbb4eef0a2a2d0c51a43b23c6eedfe86cdf2a9ccc287d53e93ed3cedae51a0dee3711f5e6d55738cab7d26155b22bdee4938824caacba8a24e9dc69d0b
|
data/README.md
CHANGED
@@ -25,18 +25,34 @@ Or install it yourself as:
|
|
25
25
|
|
26
26
|
Use one or all of the type validations into your models/classes:
|
27
27
|
|
28
|
+
**[Object#instance_of?](https://ruby-doc.org/core-2.6.4/Object.html#method-i-instance_of-3F)**
|
29
|
+
|
30
|
+
```ruby
|
31
|
+
validates :name, type: { instance_of: String }
|
32
|
+
|
33
|
+
# or use an array to verify if the attribute
|
34
|
+
# is an instance of one of the classes
|
35
|
+
|
36
|
+
validates :name, type: { instance_of: [String, Symbol] }
|
37
|
+
```
|
38
|
+
|
39
|
+
**[Object#kind_of?](https://ruby-doc.org/core-2.6.4/Object.html#method-i-kind_of-3F)**
|
40
|
+
|
28
41
|
```ruby
|
29
42
|
validates :name, type: { is_a: String }
|
30
43
|
# or
|
31
44
|
validates :name, type: { kind_of: String }
|
32
45
|
|
33
|
-
# Use an array to verify if the attribute
|
46
|
+
# Use an array to verify if the attribute
|
47
|
+
# is an instance of one of the classes
|
34
48
|
|
35
49
|
validates :status, type: { is_a: [String, Symbol]}
|
36
50
|
# or
|
37
51
|
validates :status, type: { kind_of: [String, Symbol]}
|
38
52
|
```
|
39
53
|
|
54
|
+
**[Object#respond_to?](https://ruby-doc.org/core-2.6.4/Object.html#method-i-respond_to-3F)**
|
55
|
+
|
40
56
|
```ruby
|
41
57
|
validates :handler, type: { respond_to: :call }
|
42
58
|
```
|
@@ -50,13 +66,15 @@ validates :handler, type: { klass: Handler }
|
|
50
66
|
```ruby
|
51
67
|
validates :account_types, type: { array_of: String }
|
52
68
|
|
53
|
-
# or use an array to verify if the attribute
|
69
|
+
# or use an array to verify if the attribute
|
70
|
+
# is an instance of one of the classes
|
54
71
|
|
55
72
|
validates :account_types, type: { array_of: [String, Symbol] }
|
56
73
|
```
|
57
74
|
|
58
75
|
```ruby
|
59
|
-
# Verifies if the attribute value
|
76
|
+
# Verifies if the attribute value
|
77
|
+
# is an array with some or all the expected values.
|
60
78
|
|
61
79
|
validates :account_types, type: { array_with: ['foo', 'bar'] }
|
62
80
|
```
|
data/lib/type_validator/error.rb
CHANGED
@@ -5,7 +5,7 @@ require 'active_model'
|
|
5
5
|
class TypeValidator
|
6
6
|
module Error
|
7
7
|
class InvalidDefinition < ArgumentError
|
8
|
-
OPTIONS = 'Options to define one:
|
8
|
+
OPTIONS = 'Options to define one: :instance_of, :is_a/:kind_of, :respond_to, :klass, :array_of or :array_with'.freeze
|
9
9
|
|
10
10
|
def initialize(attribute)
|
11
11
|
super "invalid type definition for :#{attribute} attribute. #{OPTIONS}"
|
data/lib/type_validator.rb
CHANGED
@@ -16,16 +16,25 @@ class TypeValidator < ActiveModel::EachValidator
|
|
16
16
|
private
|
17
17
|
|
18
18
|
def validate_type_of(attribute, value)
|
19
|
-
if expected = options[:
|
20
|
-
if expected = options[:
|
21
|
-
if expected = options[:
|
22
|
-
if expected = options[:klass]
|
23
|
-
if expected = options[:
|
24
|
-
if expected = options[:
|
19
|
+
if expected = options[:instance_of]; return validate_instance_of(value, expected); end
|
20
|
+
if expected = options[:is_a] ; return validate_kind_of(value, expected) ; end
|
21
|
+
if expected = options[:kind_of] ; return validate_kind_of(value, expected) ; end
|
22
|
+
if expected = options[:klass] ; return validate_klass(value, expected) ; end
|
23
|
+
if expected = options[:respond_to] ; return validate_respond_to(value, expected) ; end
|
24
|
+
if expected = options[:array_of] ; return validate_array_of(value, expected) ; end
|
25
|
+
if expected = options[:array_with] ; return validate_array_with(value, expected) ; end
|
25
26
|
|
26
27
|
raise Error::InvalidDefinition.new(attribute)
|
27
28
|
end
|
28
29
|
|
30
|
+
def validate_instance_of(value, expected)
|
31
|
+
types = Array(expected)
|
32
|
+
|
33
|
+
return if types.any? { |type| value.instance_of?(type) }
|
34
|
+
|
35
|
+
"must be an instance of: #{types.map { |klass| klass.name }.join(', ')}"
|
36
|
+
end
|
37
|
+
|
29
38
|
def validate_kind_of(value, expected)
|
30
39
|
types = Array(expected)
|
31
40
|
|
@@ -34,10 +43,13 @@ class TypeValidator < ActiveModel::EachValidator
|
|
34
43
|
"must be a kind of: #{types.map { |klass| klass.name }.join(', ')}"
|
35
44
|
end
|
36
45
|
|
37
|
-
def
|
38
|
-
|
46
|
+
def validate_klass(value, klass)
|
47
|
+
require_a_class(value)
|
48
|
+
require_a_class(klass)
|
39
49
|
|
40
|
-
|
50
|
+
return if value == klass || value < klass
|
51
|
+
|
52
|
+
"must be the or a subclass of `#{klass.name}`"
|
41
53
|
end
|
42
54
|
|
43
55
|
def validate_klass(value, klass)
|
@@ -53,6 +65,12 @@ class TypeValidator < ActiveModel::EachValidator
|
|
53
65
|
raise ArgumentError, "#{arg} must be a class" unless arg.is_a?(Class)
|
54
66
|
end
|
55
67
|
|
68
|
+
def validate_respond_to(value, method_name)
|
69
|
+
return if value.respond_to?(method_name)
|
70
|
+
|
71
|
+
"must respond to the method `#{method_name}`"
|
72
|
+
end
|
73
|
+
|
56
74
|
def validate_array_of(value, expected)
|
57
75
|
types = Array(expected)
|
58
76
|
|
data/type_validator.gemspec
CHANGED
@@ -6,7 +6,7 @@ Gem::Specification.new do |spec|
|
|
6
6
|
spec.name = 'type_validator'
|
7
7
|
spec.version = File.readlines(File.join('.', 'lib', 'type_validator', 'version.rb'))
|
8
8
|
.find { |line| line =~ /VERSION/ }
|
9
|
-
.strip[/(\d
|
9
|
+
.strip[/(\d\d?\.?){3}/]
|
10
10
|
spec.authors = ['Rodrigo Serradura']
|
11
11
|
spec.email = ['rodrigo.serradura@gmail.com']
|
12
12
|
|