dry-validation 1.0.0 → 1.1.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: 7acb8c840fe5a4c12564c86985078e25cff1bea877b9e2dc4f29b39bc892b6a5
4
- data.tar.gz: ae09dc37911b0e4031918f22efabd53035501119a71da60ce08b4e0a00a3e0b6
3
+ metadata.gz: 4985e6191e9e249b510fceea630ed463f11c5ff07830e6f3f867e913b29860ac
4
+ data.tar.gz: 99cb37716fb12f4a09bb94a4048935e6ac8db658df4d5a4318be321c5aef90ff
5
5
  SHA512:
6
- metadata.gz: 9430d9ca1660c3d280dd5c32f1787bce32c06c38ecd22987f53ccce8b581debce2679b95dc3b2eb9a96598d16c2dc0d99f5b65aa26de14fe0f2a03c5bb822a87
7
- data.tar.gz: a07ad85054ae898cd155db7874219332e58956f57904c1dde445d27d34aa81aa2b7d1a431e6218f6aa320bfdb0e0fffd20fd619a98991929b12c1f643326c77a
6
+ metadata.gz: 2d25a5df6a56b6a8ed32f19ed3991297f2d878d24ba5349fd8c8d0ec50a3503842c8b591d7b176ddf9303b78a847002e638faeb29691329db3b2f41d2379116b
7
+ data.tar.gz: a21649e2476c688f9a4275e43819640823c6e6a2caf18e4b8d03c0df97c946d5edbb517c6482918bf3b3b9bc21079e667c06986c2adc81acdeb0561b1bc264aa
data/CHANGELOG.md CHANGED
@@ -1,3 +1,17 @@
1
+ # v1.1.0 2019-06-14
2
+
3
+ ### Added
4
+
5
+ * `key?` method available within rules, that can be used to check if there's a value under the rule's default key (refs #540) (@solnic)
6
+ * `value` supports hash-based path specifications now (refs #547) (@solnic)
7
+ * `value` can read multiple values when the key points to them, ie in case of `rule(geo: [:lat, :lon])` it would return an array with `lat` and `lon` (@solnic)
8
+
9
+ ### Fixed
10
+
11
+ * Passing multiple macro names to `validate` or `each` works correctly (fixed #538 #541) (@jandudulski)
12
+
13
+ [Compare v1.0.0...v1.1.0](https://github.com/dry-rb/dry-validation/compare/v1.0.0...v1.1.0)
14
+
1
15
  # v1.0.0 2019-06-10
2
16
 
3
17
  This release is a complete rewrite on top of `dry-schema` that uses contract classes to define schema and validation rules. It's **not backward-compatible**. This release addressed over 150 known issues, including bugs and missing features.
@@ -12,7 +26,7 @@ See [the list of all addressed issues](https://github.com/dry-rb/dry-validation/
12
26
  - Support for macros that encapsulate common rule logic
13
27
  - Built-in `:acceptance` macro
14
28
 
15
- [Compare v0.13.3...v1.0.0](https://github.com/dry-rb/dry-validation/compare/v1.13.3...v1.0.0)
29
+ [Compare v0.13.3...v1.0.0](https://github.com/dry-rb/dry-validation/compare/v0.13.3...v1.0.0)
16
30
 
17
31
  # v1.0.0 2019-06-10 (compared to 1.0.0.rc3)
18
32
 
@@ -147,6 +147,22 @@ module Dry
147
147
  values[key_name]
148
148
  end
149
149
 
150
+ # Return if the value under the default key is available
151
+ #
152
+ # This is useful when dealing with rules for optional keys
153
+ #
154
+ # @example
155
+ # rule(:age) do
156
+ # key.failure(:invalid) if key? && value < 18
157
+ # end
158
+ #
159
+ # @return [Boolean]
160
+ #
161
+ # @api public
162
+ def key?
163
+ values.key?(key_name)
164
+ end
165
+
150
166
  # Check if there are any errors under the provided path
151
167
  #
152
168
  # @param [Symbol, String, Array] A Path-compatible spec
@@ -54,7 +54,7 @@ module Dry
54
54
  #
55
55
  # @api public
56
56
  def validate(*macros, &block)
57
- @macros = macros.map { |spec| Array(spec) }.map(&:flatten)
57
+ @macros = parse_macros(*macros)
58
58
  @block = block if block
59
59
  self
60
60
  end
@@ -68,12 +68,14 @@ module Dry
68
68
  # rule(:nums).each do
69
69
  # key.failure("must be greater than 0") if value < 0
70
70
  # end
71
+ # rule(:nums).each(min: 3)
71
72
  #
72
73
  # @return [Rule]
73
74
  #
74
75
  # @api public
75
76
  def each(*macros, &block)
76
77
  root = keys
78
+ macros = parse_macros(*macros)
77
79
  @keys = []
78
80
 
79
81
  @block = proc do
@@ -99,6 +101,22 @@ module Dry
99
101
  def inspect
100
102
  %(#<#{self.class} keys=#{keys.inspect}>)
101
103
  end
104
+
105
+ # Parse function arguments into macros structure
106
+ #
107
+ # @return [Array]
108
+ #
109
+ # @api private
110
+ def parse_macros(*args)
111
+ args.each_with_object([]) do |spec, macros|
112
+ case spec
113
+ when Hash
114
+ spec.each { |k, v| macros << [k, Array(v)] }
115
+ else
116
+ macros << Array(spec)
117
+ end
118
+ end
119
+ end
102
120
  end
103
121
  end
104
122
  end
@@ -1,6 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require 'dry/equalizer'
4
+ require 'dry/schema/path'
4
5
  require 'dry/validation/constants'
5
6
 
6
7
  module Dry
@@ -40,17 +41,39 @@ module Dry
40
41
  #
41
42
  # @api public
42
43
  def [](*args)
43
- if args.size.equal?(1)
44
- case (key = args[0])
45
- when Symbol then data[key]
46
- when String then self[*key.split(DOT).map(&:to_sym)]
47
- when Array then self[*key]
44
+ return data.dig(*args) if args.size > 1
45
+
46
+ case (key = args[0])
47
+ when Symbol, String, Array, Hash
48
+ path = Schema::Path[key]
49
+ keys = path.to_a
50
+
51
+ return data.dig(*keys) unless keys.last.is_a?(Array)
52
+
53
+ last = keys.pop
54
+ vals = self.class.new(data.dig(*keys))
55
+
56
+ last.map { |name| vals[name] }
57
+ else
58
+ raise ArgumentError, '+key+ must be a valid path specification'
59
+ end
60
+ end
61
+
62
+ # @api public
63
+ def key?(key, hash = data)
64
+ return hash.key?(key) if key.is_a?(Symbol)
65
+
66
+ Schema::Path[key].reduce(hash) do |a, e|
67
+ if e.is_a?(Array)
68
+ result = e.all? { |k| key?(k, a) }
69
+ return result
48
70
  else
49
- raise ArgumentError, '+key+ must be a symbol, string, array, or a list of keys for dig'
71
+ return false unless a.is_a?(Array) ? (e >= 0 && e < a.size) : a.key?(e)
50
72
  end
51
- else
52
- data.dig(*args)
73
+ a[e]
53
74
  end
75
+
76
+ true
54
77
  end
55
78
 
56
79
  # @api private
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Dry
4
4
  module Validation
5
- VERSION = '1.0.0'
5
+ VERSION = '1.1.0'
6
6
  end
7
7
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dry-validation
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Piotr Solnica
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-06-10 00:00:00.000000000 Z
11
+ date: 2019-06-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: concurrent-ruby