type_validator 0.5.0 → 0.6.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/type_validator/by_array_with.rb +18 -0
- data/lib/type_validator/error.rb +1 -1
- data/lib/type_validator/version.rb +1 -1
- data/lib/type_validator.rb +2 -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: bb4c476ec164e1a9b0de994b41e374307ef6dd2d3a893b8b18b905b60472dd8e
|
4
|
+
data.tar.gz: e9be80d3aca12db0140cac027ffed3a5d3e2f3ab67c5e5267711cc5f7db6502e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: da1a3abe19d8fb978109034d3341dc654d590e218eed093c31dae86e61b46327e7a8cdecd0a7fd8f3bff98fc0ea8187064c49ea74c42414441738885a43aa14c
|
7
|
+
data.tar.gz: 26b5661893d5699fff7ee08aff15e120dd57542b2147477c85467a5e3c796e749a2ed53913985d4cd3b6a345da782254ef16fe29c7d4c992b0d71f7f94398ee0
|
@@ -0,0 +1,18 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'active_model'
|
4
|
+
|
5
|
+
class TypeValidator
|
6
|
+
class ByArrayWith
|
7
|
+
def self.invalid?(value, options)
|
8
|
+
expected, allow_nil = options[:array_with], options[:allow_nil]
|
9
|
+
|
10
|
+
raise ArgumentError, "#{expected} must be an array" unless expected.is_a?(Array)
|
11
|
+
|
12
|
+
return if (allow_nil && value.nil?)
|
13
|
+
return if value.is_a?(Array) && !value.empty? && (value - expected).empty?
|
14
|
+
|
15
|
+
"must be an array with: #{expected.join(', ')}"
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
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: `:is_a`/`:kind_of`, :respond_to, :klass or :
|
8
|
+
OPTIONS = 'Options to define one: `: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
@@ -20,6 +20,7 @@ class TypeValidator < ActiveModel::EachValidator
|
|
20
20
|
return ByRespondTo if options.key?(:respond_to)
|
21
21
|
return ByKlass if options.key?(:klass)
|
22
22
|
return ByArrayOf if options.key?(:array_of)
|
23
|
+
return ByArrayWith if options.key?(:array_with)
|
23
24
|
end
|
24
25
|
end
|
25
26
|
|
@@ -28,4 +29,5 @@ require 'type_validator/error'
|
|
28
29
|
require 'type_validator/by_klass'
|
29
30
|
require 'type_validator/by_kind_of'
|
30
31
|
require 'type_validator/by_array_of'
|
32
|
+
require 'type_validator/by_array_with'
|
31
33
|
require 'type_validator/by_respond_to'
|
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.6.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Rodrigo Serradura
|
@@ -85,6 +85,7 @@ files:
|
|
85
85
|
- bin/setup
|
86
86
|
- lib/type_validator.rb
|
87
87
|
- lib/type_validator/by_array_of.rb
|
88
|
+
- lib/type_validator/by_array_with.rb
|
88
89
|
- lib/type_validator/by_kind_of.rb
|
89
90
|
- lib/type_validator/by_klass.rb
|
90
91
|
- lib/type_validator/by_respond_to.rb
|