type_validator 0.11.0 → 1.0.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 +53 -13
- data/lib/type_validator.rb +7 -1
- data/lib/type_validator/default_validation.rb +17 -0
- data/lib/type_validator/error.rb +13 -2
- data/lib/type_validator/version.rb +1 -1
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ba79f5bd84487f22cc5c8c6147373f6c85a45fa3a75eb30d5b5206a7aca5a10e
|
4
|
+
data.tar.gz: 4e43d6578742bf34ac3ef1670e6fd266b481834484f94e97722ac1594993389b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 721ae336cca31a9b8f884e70c24da3f8f73de887baf080208d2cea65b8b336e368e0608d1fa7862c746a918e63e146f82a3b868f104669093053bd2ef2b4916a
|
7
|
+
data.tar.gz: 5a2ede7c1908ae96b60e0398acbbb3ac02d30dd51886bbaccd3299617b62747541a046739ef93aadc6933f3afa90ee379cee7a21b3854bc3fb6f03f332b1fcf4
|
data/README.md
CHANGED
@@ -1,10 +1,23 @@
|
|
1
|
+
[](https://rubygems.org/gems/type_validator)
|
1
2
|
[](https://travis-ci.com/serradura/type_validator)
|
3
|
+
[](https://codeclimate.com/github/serradura/type_validator/maintainability)
|
2
4
|
[](https://coveralls.io/github/serradura/type_validator?branch=master)
|
3
5
|
|
4
6
|
# TypeValidator
|
5
7
|
|
6
8
|
Adds type validation for classes with [`ActiveModel::Validations >= 3.2`](https://api.rubyonrails.org/classes/ActiveModel/Validations.html).
|
7
9
|
|
10
|
+
- [TypeValidator](#typevalidator)
|
11
|
+
- [Required Ruby version](#required-ruby-version)
|
12
|
+
- [Installation](#installation)
|
13
|
+
- [Usage](#usage)
|
14
|
+
- [Default validation](#default-validation)
|
15
|
+
- [`allow_nil` option and `strict` mode](#allownil-option-and-strict-mode)
|
16
|
+
- [Development](#development)
|
17
|
+
- [Contributing](#contributing)
|
18
|
+
- [License](#license)
|
19
|
+
- [Code of Conduct](#code-of-conduct)
|
20
|
+
|
8
21
|
## Required Ruby version
|
9
22
|
> \>= 2.2.0
|
10
23
|
|
@@ -26,7 +39,7 @@ Or install it yourself as:
|
|
26
39
|
|
27
40
|
## Usage
|
28
41
|
|
29
|
-
Use
|
42
|
+
Use any of the type validations below into your models/classes:
|
30
43
|
|
31
44
|
**[Object#instance_of?](https://ruby-doc.org/core-2.6.4/Object.html#method-i-instance_of-3F)**
|
32
45
|
|
@@ -88,18 +101,45 @@ validates :account_types, type: { array_of: [String, Symbol] }
|
|
88
101
|
validates :account_types, type: { array_with: ['foo', 'bar'] }
|
89
102
|
```
|
90
103
|
|
91
|
-
###
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
104
|
+
### Default validation
|
105
|
+
|
106
|
+
By default, you can define the attribute type directly (without a hash). e.g.
|
107
|
+
|
108
|
+
```ruby
|
109
|
+
validates :name, type: String
|
110
|
+
# or
|
111
|
+
validates :name, type: [String, Symbol]
|
112
|
+
```
|
113
|
+
|
114
|
+
To changes this behavior you can set another strategy to validates the attributes types:
|
115
|
+
|
116
|
+
```ruby
|
117
|
+
TypeValidator.default_validation = :instance_of
|
118
|
+
|
119
|
+
# Tip: Create an initializer if you are in a Rails application.
|
120
|
+
```
|
121
|
+
|
122
|
+
And these are the available options to define the default validation:
|
123
|
+
- `kind_of` *(default)*
|
124
|
+
- `is_a`
|
125
|
+
- `instance_of`
|
126
|
+
|
127
|
+
### `allow_nil` option and `strict` mode
|
128
|
+
|
129
|
+
You can use the `allow_nil` option with any of the type validations. e.g.
|
130
|
+
|
131
|
+
```ruby
|
132
|
+
validates :name, type: { is_a: String }, allow_nil: true
|
133
|
+
```
|
134
|
+
|
135
|
+
And any of the validations work with the`strict: true` option
|
136
|
+
or with the `validates!` method. e.g.
|
137
|
+
|
138
|
+
```ruby
|
139
|
+
validates :name, type: { is_a: String }, strict: true
|
140
|
+
#or
|
141
|
+
validates! :name, type: { is_a: String }
|
142
|
+
```
|
103
143
|
|
104
144
|
## Development
|
105
145
|
|
data/lib/type_validator.rb
CHANGED
@@ -16,9 +16,13 @@ class TypeValidator < ActiveModel::EachValidator
|
|
16
16
|
private
|
17
17
|
|
18
18
|
def validate_type_of(attribute, value)
|
19
|
+
if expected = options[:with] || options[:in]
|
20
|
+
return send("validate_#{self.class.default_validation}", value, expected)
|
21
|
+
end
|
22
|
+
|
19
23
|
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
24
|
if expected = options[:kind_of] ; return validate_kind_of(value, expected) ; end
|
25
|
+
if expected = options[:is_a] ; return validate_is_a(value, expected) ; end
|
22
26
|
if expected = options[:klass] ; return validate_klass(value, expected) ; end
|
23
27
|
if expected = options[:respond_to] ; return validate_respond_to(value, expected) ; end
|
24
28
|
if expected = options[:array_of] ; return validate_array_of(value, expected) ; end
|
@@ -42,6 +46,7 @@ class TypeValidator < ActiveModel::EachValidator
|
|
42
46
|
|
43
47
|
"must be a kind of: #{types.map { |klass| klass.name }.join(', ')}"
|
44
48
|
end
|
49
|
+
alias_method :validate_is_a, :validate_kind_of
|
45
50
|
|
46
51
|
def validate_klass(value, klass)
|
47
52
|
require_a_class(value)
|
@@ -80,4 +85,5 @@ class TypeValidator < ActiveModel::EachValidator
|
|
80
85
|
end
|
81
86
|
|
82
87
|
require 'type_validator/version'
|
88
|
+
require 'type_validator/default_validation'
|
83
89
|
require 'type_validator/error'
|
@@ -0,0 +1,17 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
class TypeValidator
|
4
|
+
DEFAULT_VALIDATION_OPTIONS = %w[instance_of is_a kind_of].freeze
|
5
|
+
|
6
|
+
def self.default_validation
|
7
|
+
@default_validation ||= :kind_of
|
8
|
+
end
|
9
|
+
|
10
|
+
def self.default_validation=(option)
|
11
|
+
if DEFAULT_VALIDATION_OPTIONS.include?(String(option))
|
12
|
+
@default_validation = option.to_sym
|
13
|
+
else
|
14
|
+
raise Error::InvalidDefaultValidation.new(option)
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
data/lib/type_validator/error.rb
CHANGED
@@ -1,7 +1,5 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require 'active_model'
|
4
|
-
|
5
3
|
class TypeValidator
|
6
4
|
module Error
|
7
5
|
class InvalidDefinition < ArgumentError
|
@@ -13,5 +11,18 @@ class TypeValidator
|
|
13
11
|
|
14
12
|
private_constant :OPTIONS
|
15
13
|
end
|
14
|
+
|
15
|
+
class InvalidDefaultValidation < ArgumentError
|
16
|
+
OPTIONS =
|
17
|
+
TypeValidator::DEFAULT_VALIDATION_OPTIONS
|
18
|
+
.map { |option| ":#{option}" }
|
19
|
+
.join(', ')
|
20
|
+
|
21
|
+
def initialize(option)
|
22
|
+
super "#{option.inspect} is an invalid option. Please use one of these: #{OPTIONS}"
|
23
|
+
end
|
24
|
+
|
25
|
+
private_constant :OPTIONS
|
26
|
+
end
|
16
27
|
end
|
17
28
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: type_validator
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 1.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Rodrigo Serradura
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2019-09-
|
11
|
+
date: 2019-09-20 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activemodel
|
@@ -86,6 +86,7 @@ files:
|
|
86
86
|
- bin/console
|
87
87
|
- bin/setup
|
88
88
|
- lib/type_validator.rb
|
89
|
+
- lib/type_validator/default_validation.rb
|
89
90
|
- lib/type_validator/error.rb
|
90
91
|
- lib/type_validator/version.rb
|
91
92
|
- test.sh
|