type_validator 0.6.0 → 0.7.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/.coveralls.yml +1 -0
- data/.travis.yml +0 -8
- data/Gemfile +6 -2
- data/README.md +53 -6
- data/lib/type_validator/by_array_of.rb +1 -2
- data/lib/type_validator/by_array_with.rb +1 -2
- data/lib/type_validator/by_kind_of.rb +1 -2
- data/lib/type_validator/by_klass.rb +2 -2
- data/lib/type_validator/by_respond_to.rb +2 -2
- data/lib/type_validator/version.rb +1 -1
- data/lib/type_validator.rb +1 -0
- metadata +2 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 16bbf2c1f264c32540b3a2a0b455dc14a5b808a85ff17ab2f2774070ac7a95a2
|
4
|
+
data.tar.gz: a7c6e27a77306aa5b79b9040c8823bb5da18d5bcd2e807b1de1b1a1f7f0c982a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 67949f5d4714cb33c9d2c6d759361993d0b28426644bacde350a86f597849e8313d13083d15abc046da4e03fe57226b9a6679dfc46b623be65f2f30ace13d672
|
7
|
+
data.tar.gz: 94af44dc82dbb1c52cbc2a7ea109e29d997db51a91852e409a7e2a5c1843aeb6e61fa353cc3a25e42f6305fed93b9f4646f2ac308aaacb45779f0a13777801d3
|
data/.coveralls.yml
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
service_name: travis-ci
|
data/.travis.yml
CHANGED
@@ -18,12 +18,4 @@ before_install:
|
|
18
18
|
|
19
19
|
install: bundle install --jobs=3 --retry=3
|
20
20
|
|
21
|
-
before_script:
|
22
|
-
- curl -L https://codeclimate.com/downloads/test-reporter/test-reporter-latest-linux-amd64 > ./cc-test-reporter
|
23
|
-
- chmod +x ./cc-test-reporter
|
24
|
-
- "./cc-test-reporter before-build"
|
25
|
-
|
26
21
|
script: "./.travis.sh"
|
27
|
-
|
28
|
-
after_success:
|
29
|
-
- "./cc-test-reporter after-build -t simplecov"
|
data/Gemfile
CHANGED
@@ -2,7 +2,7 @@ source 'https://rubygems.org'
|
|
2
2
|
|
3
3
|
git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
|
4
4
|
|
5
|
-
activemodel_version = ENV.fetch('ACTIVEMODEL_VERSION', '6.
|
5
|
+
activemodel_version = ENV.fetch('ACTIVEMODEL_VERSION', '6.0')
|
6
6
|
|
7
7
|
activemodel = case activemodel_version
|
8
8
|
when '3.2' then '3.2.22'
|
@@ -14,7 +14,7 @@ activemodel = case activemodel_version
|
|
14
14
|
when '5.2' then '5.2.3'
|
15
15
|
end
|
16
16
|
|
17
|
-
if activemodel_version < '6.
|
17
|
+
if activemodel_version < '6.0'
|
18
18
|
gem 'activemodel', activemodel, require: false
|
19
19
|
gem 'activesupport', activemodel, require: false
|
20
20
|
end
|
@@ -24,5 +24,9 @@ group :test do
|
|
24
24
|
gem 'simplecov', require: false
|
25
25
|
end
|
26
26
|
|
27
|
+
if RUBY_VERSION >= '2.6.0' && activemodel_version >= '6.0'
|
28
|
+
gem 'coveralls', require: false
|
29
|
+
end
|
30
|
+
|
27
31
|
# Specify your gem's dependencies in type_validator.gemspec
|
28
32
|
gemspec
|
data/README.md
CHANGED
@@ -1,10 +1,9 @@
|
|
1
1
|
[](https://travis-ci.com/serradura/type_validator)
|
2
|
+
[](https://coveralls.io/github/serradura/type_validator?branch=master)
|
2
3
|
|
3
4
|
# TypeValidator
|
4
5
|
|
5
|
-
|
6
|
-
|
7
|
-
TODO: Delete this and the text above, and describe your gem
|
6
|
+
Adds type validation for classes with [`ActiveModel::Validations >= 3.2`](https://api.rubyonrails.org/classes/ActiveModel/Validations.html).
|
8
7
|
|
9
8
|
## Installation
|
10
9
|
|
@@ -24,7 +23,55 @@ Or install it yourself as:
|
|
24
23
|
|
25
24
|
## Usage
|
26
25
|
|
27
|
-
|
26
|
+
Use one or all of the type validations into your models/classes:
|
27
|
+
|
28
|
+
```ruby
|
29
|
+
validates :name, type: { is_a: String }
|
30
|
+
# or
|
31
|
+
validates :name, type: { kind_of: String }
|
32
|
+
|
33
|
+
# Use an array to verify if the attribute is an instance of one of the classes
|
34
|
+
|
35
|
+
validates :status, type: { is_a: [String, Symbol]}
|
36
|
+
# or
|
37
|
+
validates :status, type: { kind_of: [String, Symbol]}
|
38
|
+
```
|
39
|
+
|
40
|
+
```ruby
|
41
|
+
validates :handler, type: { respond_to: :call }
|
42
|
+
```
|
43
|
+
|
44
|
+
```ruby
|
45
|
+
# Verifies if the attribute value is the class or a subclass.
|
46
|
+
|
47
|
+
validates :handler, type: { klass: Handler }
|
48
|
+
```
|
49
|
+
|
50
|
+
```ruby
|
51
|
+
validates :account_types, type: { array_of: String }
|
52
|
+
|
53
|
+
# or use an array to verify if the attribute is an instance of one of the classes
|
54
|
+
|
55
|
+
validates :account_types, type: { array_of: [String, Symbol] }
|
56
|
+
```
|
57
|
+
|
58
|
+
```ruby
|
59
|
+
# Verifies if the attribute value is an array with some or all the expected values.
|
60
|
+
|
61
|
+
validates :account_types, type: { array_with: ['foo', 'bar'] }
|
62
|
+
```
|
63
|
+
|
64
|
+
**All the validations above accept:**
|
65
|
+
- `allow_nil` option. e.g:. e.g:
|
66
|
+
```ruby
|
67
|
+
validates :name, type: { is_a: String }, allow_nil: true
|
68
|
+
```
|
69
|
+
- `strict: true` option or the usage of `validates!`method. e.g:
|
70
|
+
```ruby
|
71
|
+
validates :name, type: { is_a: String }, strict: true
|
72
|
+
#or
|
73
|
+
validates! :name, type: { is_a: String }
|
74
|
+
```
|
28
75
|
|
29
76
|
## Development
|
30
77
|
|
@@ -34,7 +81,7 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
|
|
34
81
|
|
35
82
|
## Contributing
|
36
83
|
|
37
|
-
Bug reports and pull requests are welcome on GitHub at https://github.com/
|
84
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/serradura/type_validator. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
|
38
85
|
|
39
86
|
## License
|
40
87
|
|
@@ -42,4 +89,4 @@ The gem is available as open source under the terms of the [MIT License](https:/
|
|
42
89
|
|
43
90
|
## Code of Conduct
|
44
91
|
|
45
|
-
Everyone interacting in the TypeValidator project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/
|
92
|
+
Everyone interacting in the TypeValidator project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/serradura/type_validator/blob/master/CODE_OF_CONDUCT.md).
|
@@ -5,9 +5,8 @@ require 'active_model'
|
|
5
5
|
class TypeValidator
|
6
6
|
class ByArrayOf
|
7
7
|
def self.invalid?(value, options)
|
8
|
-
types
|
8
|
+
types = Array(options[:array_of])
|
9
9
|
|
10
|
-
return if (allow_nil && value.nil?)
|
11
10
|
return if value.is_a?(Array) && !value.empty? && value.all? { |value| types.any? { |type| value.is_a?(type) } }
|
12
11
|
|
13
12
|
"must be an array of: #{types.map { |klass| klass.name }.join(', ')}"
|
@@ -5,11 +5,10 @@ require 'active_model'
|
|
5
5
|
class TypeValidator
|
6
6
|
class ByArrayWith
|
7
7
|
def self.invalid?(value, options)
|
8
|
-
expected
|
8
|
+
expected = options[:array_with]
|
9
9
|
|
10
10
|
raise ArgumentError, "#{expected} must be an array" unless expected.is_a?(Array)
|
11
11
|
|
12
|
-
return if (allow_nil && value.nil?)
|
13
12
|
return if value.is_a?(Array) && !value.empty? && (value - expected).empty?
|
14
13
|
|
15
14
|
"must be an array with: #{expected.join(', ')}"
|
@@ -6,9 +6,8 @@ class TypeValidator
|
|
6
6
|
class ByKindOf
|
7
7
|
def self.invalid?(value, options)
|
8
8
|
types = Array(options[:is_a] || options[:kind_of])
|
9
|
-
allow_nil = options[:allow_nil]
|
10
9
|
|
11
|
-
return if
|
10
|
+
return if types.any? { |type| value.is_a?(type) }
|
12
11
|
|
13
12
|
"must be a kind of: #{types.map { |klass| klass.name }.join(', ')}"
|
14
13
|
end
|
@@ -5,12 +5,12 @@ require 'active_model'
|
|
5
5
|
class TypeValidator
|
6
6
|
class ByKlass
|
7
7
|
def self.invalid?(value, options)
|
8
|
-
klass
|
8
|
+
klass = options[:klass]
|
9
9
|
|
10
10
|
require_a_class(value)
|
11
11
|
require_a_class(klass)
|
12
12
|
|
13
|
-
return if
|
13
|
+
return if value == klass || value < klass
|
14
14
|
|
15
15
|
"must be the or a subclass of `#{klass.name}`"
|
16
16
|
end
|
@@ -5,9 +5,9 @@ require 'active_model'
|
|
5
5
|
class TypeValidator
|
6
6
|
class ByRespondTo
|
7
7
|
def self.invalid?(value, options)
|
8
|
-
method_name
|
8
|
+
method_name = options[:respond_to]
|
9
9
|
|
10
|
-
return if
|
10
|
+
return if value.respond_to?(method_name)
|
11
11
|
|
12
12
|
"must respond to the method `#{method_name}`"
|
13
13
|
end
|
data/lib/type_validator.rb
CHANGED
@@ -8,6 +8,7 @@ class TypeValidator < ActiveModel::EachValidator
|
|
8
8
|
|
9
9
|
raise Error::InvalidDefinition.new(attribute) unless strategy
|
10
10
|
|
11
|
+
return if options[:allow_nil] && value.nil?
|
11
12
|
return unless error = strategy.invalid?(value, options)
|
12
13
|
|
13
14
|
raise TypeError, "#{attribute} #{error}" if options[:strict]
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: type_validator
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.7.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Rodrigo Serradura
|
@@ -73,6 +73,7 @@ executables: []
|
|
73
73
|
extensions: []
|
74
74
|
extra_rdoc_files: []
|
75
75
|
files:
|
76
|
+
- ".coveralls.yml"
|
76
77
|
- ".gitignore"
|
77
78
|
- ".travis.sh"
|
78
79
|
- ".travis.yml"
|