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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: ece9a123e1c83f525af3096c563de8a1efca129eb5e4a492c871203ea16aadcb
4
- data.tar.gz: 10687d371e4fbdca39871dda7ef7c9511b29551e2c09b082f6199a6f82dd0293
3
+ metadata.gz: bb4c476ec164e1a9b0de994b41e374307ef6dd2d3a893b8b18b905b60472dd8e
4
+ data.tar.gz: e9be80d3aca12db0140cac027ffed3a5d3e2f3ab67c5e5267711cc5f7db6502e
5
5
  SHA512:
6
- metadata.gz: a912cd368560070b58ccf17c71fabd64a2c1bd10e14847d5c667414e39749f7a9abce5d81fd7e4a54956dea1c91a208a03fe08d4c78ddd5c8c8e66b77ab536b9
7
- data.tar.gz: 4b4748578919138334e9a6e3c01c9976cf186b631808643a3c39969b5e018a9660bd300197f9f4db95a9cbde2648b78a66abc56a3fc14f260cc17a85ee391e75
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
@@ -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 :array_of'.freeze
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}"
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  class TypeValidator
4
- VERSION = '0.5.0'
4
+ VERSION = '0.6.0'
5
5
  end
@@ -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.5.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