dry-schema 1.6.1 → 1.8.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: '07179bd67f80d0a500d2484a2169f09804612a38ce9601819203d89bc0d109a1'
4
- data.tar.gz: 1337a9d5197257e3bc773f87af7785e4297624b3d10699ea5b9c977be4a19a67
3
+ metadata.gz: fc91fbbcd77a2535c39ac072f7725e2cc89cf40fb1710c1f7f2f32867d37351e
4
+ data.tar.gz: a725bbc8ae7735fb576b827c5ee4dc00235e51287e7fb20882d8235fc61d72f5
5
5
  SHA512:
6
- metadata.gz: 10b8ea1df48c45fa50bcc35be0d7e9785c90285fb29bdc0ad712745803280f9de53ea80643dccefd923df263ba20973cd2b4956dd2d773701ff4ad9050ab3742
7
- data.tar.gz: 42776d375539ec537361f2c6e95c027020107e6f92bd14a866ed1b37e10af5ae0037eb0866278aa8877ce62f617222201b34f58db3e43925ae89f46585359a9a
6
+ metadata.gz: 1dcb14a96c4d9e1c38ca7bafaf04a621ee71cffc75bb4f3328d46752ec291f4ee580fea7f21bc4b430277af601ca31e29c5358d3c72d38ec53e128b411921245
7
+ data.tar.gz: 27d66147c3be3d089d92b56ddec1b00a230d83e04606a52c14c10cb0f40648b8beb893f874e00916c5450769b1bdaf72c0df54e3e89464f96bc96986904fe465
data/CHANGELOG.md CHANGED
@@ -1,5 +1,57 @@
1
1
  <!--- DO NOT EDIT THIS FILE - IT'S AUTOMATICALLY GENERATED VIA DEVTOOLS --->
2
2
 
3
+ ## 1.8.0 2021-09-12
4
+
5
+
6
+ ### Changed
7
+
8
+ - [internal] Upgraded to new `setting` API provided in dry-configurable 0.13.0 (@timriley in #356)
9
+
10
+ [Compare v1.7.1...v1.8.0](https://github.com/dry-rb/dry-schema/compare/v1.7.1...v1.8.0)
11
+
12
+ ## 1.7.1 2021-08-29
13
+
14
+
15
+ ### Changed
16
+
17
+ - [internal] Use explicit `#to_h` conversion of Dry::Configurable::Config, to ensure compatibility with upcoming dry-configurable 0.13.0 release (via #371) (@timriley)
18
+
19
+ [Compare v1.7.0...v1.7.1](https://github.com/dry-rb/dry-schema/compare/v1.7.0...v1.7.1)
20
+
21
+ ## 1.7.0 2021-06-29
22
+
23
+ This release ships with a bunch of internal refactorings that should improve performance but if you see any unexpected behavior please do report issues.
24
+
25
+ ### Fixed
26
+
27
+ - Handle arrays of hashes where Array constructor coerces non-Hash input (#351 fixed via #354) (@ojab)
28
+ - Run outer schema processor steps before inner ones (issue #350 fixed via #361) (@ojab)
29
+ - Fix key validator false negatives on empty collections (see #363) (@Drenmi)
30
+ - Prevent error message YAML files from being parsed multiple times (issue #352 via #364) (@alassek)
31
+ - Using constructor types should work fine now ie `required(:foo).filled(Types::Params::Integer.constructor(&:succ))` (issue #280 fixed via #365) (@solnic)
32
+ - Handle non-Hash to Hash transformation in `before(:key_coercer)` (issue #350 fixed via #362) (@ojab)
33
+
34
+ ### Changed
35
+
36
+ - [internal] `Dry::Schema::Path` clean up and performance improvements (via #358) (@ojab)
37
+ - [internal] simplify and speed up handling of steps in nested schemas (via #360) (@ojab)
38
+
39
+ [Compare v1.6.2...v1.7.0](https://github.com/dry-rb/dry-schema/compare/v1.6.2...v1.7.0)
40
+
41
+ ## 1.6.2 2021-04-15
42
+
43
+
44
+ ### Added
45
+
46
+ - A default error message for `respond_to?` predicate (@rindek)
47
+
48
+ ### Fixed
49
+
50
+ - Using `respond_to?` predicate in blocks works now (@rindek)
51
+
52
+
53
+ [Compare v1.6.1...v1.6.2](https://github.com/dry-rb/dry-schema/compare/v1.6.1...v1.6.2)
54
+
3
55
  ## 1.6.1 2021-02-02
4
56
 
5
57
 
data/README.md CHANGED
@@ -1,3 +1,4 @@
1
+ <!--- this file is synced from dry-rb/template-gem project -->
1
2
  [gem]: https://rubygems.org/gems/dry-schema
2
3
  [actions]: https://github.com/dry-rb/dry-schema/actions
3
4
  [codacy]: https://www.codacy.com/gh/dry-rb/dry-schema
@@ -7,22 +8,22 @@
7
8
  # dry-schema [![Join the chat at https://dry-rb.zulipchat.com](https://img.shields.io/badge/dry--rb-join%20chat-%23346b7a.svg)][chat]
8
9
 
9
10
  [![Gem Version](https://badge.fury.io/rb/dry-schema.svg)][gem]
10
- [![CI Status](https://github.com/dry-rb/dry-schema/workflows/ci/badge.svg)][actions]
11
+ [![CI Status](https://github.com/dry-rb/dry-schema/workflows/CI/badge.svg)][actions]
11
12
  [![Codacy Badge](https://api.codacy.com/project/badge/Grade/961f5c776f1d49218b2cede3745e059c)][codacy]
12
13
  [![Codacy Badge](https://api.codacy.com/project/badge/Coverage/961f5c776f1d49218b2cede3745e059c)][codacy]
13
14
  [![Inline docs](http://inch-ci.org/github/dry-rb/dry-schema.svg?branch=master)][inchpages]
14
15
 
15
16
  ## Links
16
17
 
17
- * [User documentation](http://dry-rb.org/gems/dry-schema)
18
+ * [User documentation](https://dry-rb.org/gems/dry-schema)
18
19
  * [API documentation](http://rubydoc.info/gems/dry-schema)
19
20
 
20
21
  ## Supported Ruby versions
21
22
 
22
23
  This library officially supports the following Ruby versions:
23
24
 
24
- * MRI >= `2.5`
25
- * jruby >= `9.2`
25
+ * MRI `>= 2.6.0`
26
+ * ~~jruby~~ `>= 9.3` (we are waiting for [2.6 support](https://github.com/jruby/jruby/issues/6161))
26
27
 
27
28
  ## License
28
29
 
data/config/errors.yml CHANGED
@@ -87,6 +87,8 @@ en:
87
87
 
88
88
  type?: "must be %{type}"
89
89
 
90
+ respond_to?: "must respond to %{method}"
91
+
90
92
  size?:
91
93
  arg:
92
94
  default: "size must be %{size}"
data/dry-schema.gemspec CHANGED
@@ -1,15 +1,16 @@
1
1
  # frozen_string_literal: true
2
- # this file is managed by dry-rb/devtools project
3
2
 
4
- lib = File.expand_path('lib', __dir__)
3
+ # this file is synced from dry-rb/template-gem project
4
+
5
+ lib = File.expand_path("lib", __dir__)
5
6
  $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
6
- require 'dry/schema/version'
7
+ require "dry/schema/version"
7
8
 
8
9
  Gem::Specification.new do |spec|
9
- spec.name = 'dry-schema'
10
+ spec.name = "dry-schema"
10
11
  spec.authors = ["Piotr Solnica"]
11
12
  spec.email = ["piotr.solnica@gmail.com"]
12
- spec.license = 'MIT'
13
+ spec.license = "MIT"
13
14
  spec.version = Dry::Schema::VERSION.dup
14
15
 
15
16
  spec.summary = "Coercion and validation for data structures"
@@ -17,23 +18,24 @@ Gem::Specification.new do |spec|
17
18
  dry-schema provides a DSL for defining schemas with keys and rules that should be applied to
18
19
  values. It supports coercion, input sanitization, custom types and localized error messages
19
20
  (with or without I18n gem). It's also used as the schema engine in dry-validation.
21
+
20
22
  TEXT
21
- spec.homepage = 'https://dry-rb.org/gems/dry-schema'
23
+ spec.homepage = "https://dry-rb.org/gems/dry-schema"
22
24
  spec.files = Dir["CHANGELOG.md", "LICENSE", "README.md", "dry-schema.gemspec", "lib/**/*", "config/*.yml"]
23
- spec.bindir = 'bin'
25
+ spec.bindir = "bin"
24
26
  spec.executables = []
25
- spec.require_paths = ['lib']
27
+ spec.require_paths = ["lib"]
26
28
 
27
- spec.metadata['allowed_push_host'] = 'https://rubygems.org'
28
- spec.metadata['changelog_uri'] = 'https://github.com/dry-rb/dry-schema/blob/master/CHANGELOG.md'
29
- spec.metadata['source_code_uri'] = 'https://github.com/dry-rb/dry-schema'
30
- spec.metadata['bug_tracker_uri'] = 'https://github.com/dry-rb/dry-schema/issues'
29
+ spec.metadata["allowed_push_host"] = "https://rubygems.org"
30
+ spec.metadata["changelog_uri"] = "https://github.com/dry-rb/dry-schema/blob/master/CHANGELOG.md"
31
+ spec.metadata["source_code_uri"] = "https://github.com/dry-rb/dry-schema"
32
+ spec.metadata["bug_tracker_uri"] = "https://github.com/dry-rb/dry-schema/issues"
31
33
 
32
- spec.required_ruby_version = ">= 2.5.0"
34
+ spec.required_ruby_version = ">= 2.6.0"
33
35
 
34
36
  # to update dependencies edit project.yml
35
37
  spec.add_runtime_dependency "concurrent-ruby", "~> 1.0"
36
- spec.add_runtime_dependency "dry-configurable", "~> 0.8", ">= 0.8.3"
38
+ spec.add_runtime_dependency "dry-configurable", "~> 0.13", ">= 0.13.0"
37
39
  spec.add_runtime_dependency "dry-core", "~> 0.5", ">= 0.5"
38
40
  spec.add_runtime_dependency "dry-initializer", "~> 3.0"
39
41
  spec.add_runtime_dependency "dry-logic", "~> 1.0"
@@ -25,7 +25,7 @@ module Dry
25
25
  # @return [Schema::PredicateRegistry]
26
26
  #
27
27
  # @api public
28
- setting(:predicates, Schema::PredicateRegistry.new)
28
+ setting :predicates, default: Schema::PredicateRegistry.new
29
29
 
30
30
  # @!method types
31
31
  #
@@ -34,7 +34,7 @@ module Dry
34
34
  # @return [Hash]
35
35
  #
36
36
  # @api public
37
- setting(:types, Dry::Types)
37
+ setting :types, default: Dry::Types
38
38
 
39
39
  # @!method messages
40
40
  #
@@ -43,12 +43,12 @@ module Dry
43
43
  # @return [Dry::Configurable::Config]
44
44
  #
45
45
  # @api public
46
- setting(:messages) do
47
- setting(:backend, :yaml)
48
- setting(:namespace)
49
- setting(:load_paths, Set[DEFAULT_MESSAGES_PATH], &:dup)
50
- setting(:top_namespace, DEFAULT_MESSAGES_ROOT)
51
- setting(:default_locale, nil)
46
+ setting :messages do
47
+ setting :backend, default: :yaml
48
+ setting :namespace
49
+ setting :load_paths, default: Set[DEFAULT_MESSAGES_PATH], constructor: :dup.to_proc
50
+ setting :top_namespace, default: DEFAULT_MESSAGES_ROOT
51
+ setting :default_locale
52
52
  end
53
53
 
54
54
  # @!method validate_keys
@@ -58,7 +58,7 @@ module Dry
58
58
  # @return [Boolean]
59
59
  #
60
60
  # @api public
61
- setting(:validate_keys, false)
61
+ setting :validate_keys, default: false
62
62
 
63
63
  # @api private
64
64
  def respond_to_missing?(meth, include_private = false)
@@ -151,7 +151,11 @@ module Dry
151
151
  # @api private
152
152
  def write(source, target)
153
153
  read(source) { |value|
154
- target[coerced_name] = value.is_a?(::Array) ? value.map { |el| member.write(el) } : value
154
+ target[coerced_name] = if value.is_a?(::Array)
155
+ value.map { |el| el.is_a?(::Hash) ? member.write(el) : el }
156
+ else
157
+ value
158
+ end
155
159
  }
156
160
  end
157
161
 
@@ -51,9 +51,14 @@ module Dry
51
51
  hash.flat_map { |key, _|
52
52
  case (value = hash[key])
53
53
  when Hash
54
+ next key.to_s if value.empty?
55
+
54
56
  [key].product(key_paths(hash[key])).map { |keys| keys.join(DOT) }
55
57
  when Array
56
- hashes_or_arrays = value.select { |e| e.is_a?(Array) || e.is_a?(Hash) }
58
+ hashes_or_arrays = value.select { |e| (e.is_a?(Array) || e.is_a?(Hash)) && !e.empty? }
59
+
60
+ next key.to_s if hashes_or_arrays.empty?
61
+
57
62
  hashes_or_arrays.flat_map.with_index { |el, idx|
58
63
  key_paths(el).map { |path| ["#{key}[#{idx}]", *path].join(DOT) }
59
64
  }
@@ -17,6 +17,7 @@ module Dry
17
17
 
18
18
  undef :eql?
19
19
  undef :nil?
20
+ undef :respond_to?
20
21
 
21
22
  # @!attribute [r] chain
22
23
  # Indicate if the macro should append its rules to the provided trace
@@ -11,9 +11,14 @@ module Dry
11
11
  # @api private
12
12
  class Value < DSL
13
13
  # @api private
14
- def call(*predicates, **opts, &block)
14
+ def call(*args, **opts, &block)
15
+ types, predicates = args.partition { |arg| arg.is_a?(Dry::Types::Type) }
16
+
17
+ constructor = types.select { |type| type.is_a?(Dry::Types::Constructor) }.reduce(:>>)
15
18
  schema = predicates.detect { |predicate| predicate.is_a?(Processor) }
16
19
 
20
+ schema_dsl.set_type(name, constructor) if constructor
21
+
17
22
  type_spec = opts[:type_spec]
18
23
 
19
24
  if schema
@@ -18,13 +18,13 @@ module Dry
18
18
  include Dry::Configurable
19
19
  include Dry::Equalizer(:config)
20
20
 
21
- setting :default_locale, nil
22
- setting :load_paths, Set[DEFAULT_MESSAGES_PATH]
23
- setting :top_namespace, DEFAULT_MESSAGES_ROOT
24
- setting :root, "errors"
25
- setting :lookup_options, %i[root predicate path val_type arg_type].freeze
21
+ setting :default_locale
22
+ setting :load_paths, default: Set[DEFAULT_MESSAGES_PATH]
23
+ setting :top_namespace, default: DEFAULT_MESSAGES_ROOT
24
+ setting :root, default: "errors"
25
+ setting :lookup_options, default: %i[root predicate path val_type arg_type].freeze
26
26
 
27
- setting :lookup_paths, [
27
+ setting :lookup_paths, default: [
28
28
  "%<root>s.rules.%<path>s.%<predicate>s.arg.%<arg_type>s",
29
29
  "%<root>s.rules.%<path>s.%<predicate>s",
30
30
  "%<root>s.%<predicate>s.%<message_type>s",
@@ -35,13 +35,13 @@ module Dry
35
35
  "%<root>s.%<predicate>s"
36
36
  ].freeze
37
37
 
38
- setting :rule_lookup_paths, ["rules.%<name>s"].freeze
38
+ setting :rule_lookup_paths, default: ["rules.%<name>s"].freeze
39
39
 
40
- setting :arg_types, Hash.new { |*| "default" }.update(
40
+ setting :arg_types, default: Hash.new { |*| "default" }.update(
41
41
  Range => "range"
42
42
  )
43
43
 
44
- setting :val_types, Hash.new { |*| "default" }.update(
44
+ setting :val_types, default: Hash.new { |*| "default" }.update(
45
45
  Range => "range",
46
46
  String => "string"
47
47
  )
@@ -71,6 +71,11 @@ module Dry
71
71
  @cache ||= Concurrent::Map.new { |h, k| h[k] = Concurrent::Map.new }
72
72
  end
73
73
 
74
+ # @api private
75
+ def self.source_cache
76
+ @source_cache ||= Concurrent::Map.new
77
+ end
78
+
74
79
  # @api private
75
80
  def initialize(data: EMPTY_HASH, config: nil)
76
81
  super()
@@ -179,7 +184,9 @@ module Dry
179
184
 
180
185
  # @api private
181
186
  def load_translations(path)
182
- data = self.class.flat_hash(YAML.load_file(path))
187
+ data = self.class.source_cache.fetch_or_store(path) do
188
+ self.class.flat_hash(YAML.load_file(path)).freeze
189
+ end
183
190
 
184
191
  return data unless custom_top_namespace?(path)
185
192
 
@@ -32,10 +32,10 @@ module Dry
32
32
  new(spec.split(DOT).map(&:to_sym))
33
33
  when Hash
34
34
  new(keys_from_hash(spec))
35
- when Path
35
+ when self
36
36
  spec
37
37
  else
38
- raise ArgumentError, "+spec+ must be either a Symbol, Array, Hash or a Path"
38
+ raise ArgumentError, "+spec+ must be either a Symbol, Array, Hash or a #{name}"
39
39
  end
40
40
  end
41
41
 
@@ -60,24 +60,9 @@ module Dry
60
60
 
61
61
  # @api private
62
62
  def to_h(value = EMPTY_ARRAY.dup)
63
- curr_idx = 0
64
- last_idx = keys.size - 1
65
- hash = EMPTY_HASH.dup
66
- node = hash
67
-
68
- while curr_idx <= last_idx
69
- node =
70
- node[keys[curr_idx]] =
71
- if curr_idx == last_idx
72
- value.is_a?(Array) ? value : [value]
73
- else
74
- EMPTY_HASH.dup
75
- end
76
-
77
- curr_idx += 1
78
- end
63
+ value = [value] unless value.is_a?(Array)
79
64
 
80
- hash
65
+ keys.reverse_each.reduce(value) { |result, key| {key => result} }
81
66
  end
82
67
 
83
68
  # @api private
@@ -85,59 +70,27 @@ module Dry
85
70
  keys.each(&block)
86
71
  end
87
72
 
88
- # @api private
89
- def index(key)
90
- keys.index(key)
91
- end
92
-
93
- def without_index
94
- self.class.new(to_a[0..-2])
95
- end
96
-
97
73
  # @api private
98
74
  def include?(other)
99
- if !same_root?(other)
100
- false
101
- elsif index?
102
- if other.index?
103
- last.equal?(other.last)
104
- else
105
- without_index.include?(other)
106
- end
107
- elsif other.index? && key_matches(other, :select).size < 2
108
- false
109
- else
110
- self >= other && !other.key_matches(self).include?(nil)
111
- end
75
+ keys[0, other.keys.length].eql?(other.keys)
112
76
  end
113
77
 
114
78
  # @api private
115
79
  def <=>(other)
116
- raise ArgumentError, "Can't compare paths from different branches" unless same_root?(other)
80
+ return keys.length <=> other.keys.length if include?(other) || other.include?(self)
117
81
 
118
- return 0 if keys.eql?(other.keys)
82
+ first_uncommon_index = (self & other).keys.length
119
83
 
120
- res = key_matches(other).compact.reject { |value| value.equal?(false) }
121
-
122
- res.size < count ? 1 : -1
84
+ keys[first_uncommon_index] <=> other.keys[first_uncommon_index]
123
85
  end
124
86
 
125
87
  # @api private
126
88
  def &(other)
127
- unless same_root?(other)
128
- raise ArgumentError, "#{other.inspect} doesn't have the same root #{inspect}"
129
- end
130
-
131
89
  self.class.new(
132
- key_matches(other, :select).compact.reject { |value| value.equal?(false) }
90
+ keys.take_while.with_index { |key, index| other.keys[index].eql?(key) }
133
91
  )
134
92
  end
135
93
 
136
- # @api private
137
- def key_matches(other, meth = :map)
138
- public_send(meth) { |key| (idx = other.index(key)) && keys[idx].equal?(key) }
139
- end
140
-
141
94
  # @api private
142
95
  def last
143
96
  keys.last
@@ -148,10 +101,7 @@ module Dry
148
101
  root.equal?(other.root)
149
102
  end
150
103
 
151
- # @api private
152
- def index?
153
- last.is_a?(Integer)
154
- end
104
+ EMPTY = new(EMPTY_ARRAY).freeze
155
105
  end
156
106
  end
157
107
  end
@@ -28,8 +28,8 @@ module Dry
28
28
  include Dry::Logic::Operators
29
29
 
30
30
  setting :key_map_type
31
- setting :type_registry_namespace, :strict
32
- setting :filter_empty_string, false
31
+ setting :type_registry_namespace, default: :strict
32
+ setting :filter_empty_string, default: false
33
33
 
34
34
  option :steps, default: -> { ProcessorSteps.new }
35
35
 
@@ -53,7 +53,7 @@ module Dry
53
53
  # @api public
54
54
  def define(&block)
55
55
  @definition ||= DSL.new(
56
- processor_type: self, parent: superclass.definition, **config, &block
56
+ processor_type: self, parent: superclass.definition, **config.to_h, &block
57
57
  )
58
58
  self
59
59
  end
@@ -84,14 +84,14 @@ module Dry
84
84
  #
85
85
  # @api public
86
86
  def call(input)
87
- Result.new(input, input: input, message_compiler: message_compiler) do |result|
87
+ Result.new(input.dup, message_compiler: message_compiler) do |result|
88
88
  steps.call(result)
89
89
  end
90
90
  end
91
91
  alias_method :[], :call
92
92
 
93
93
  # @api public
94
- def xor(other)
94
+ def xor(_other)
95
95
  raise NotImplementedError, "composing schemas using `xor` operator is not supported yet"
96
96
  end
97
97
  alias_method :^, :xor
@@ -87,6 +87,7 @@ module Dry
87
87
  def after(name, &block)
88
88
  after_steps[name] ||= EMPTY_ARRAY.dup
89
89
  after_steps[name] << Step.new(type: :after, name: name, executor: block)
90
+ after_steps[name].sort_by!(&:path)
90
91
  self
91
92
  end
92
93
 
@@ -100,6 +101,7 @@ module Dry
100
101
  def before(name, &block)
101
102
  before_steps[name] ||= EMPTY_ARRAY.dup
102
103
  before_steps[name] << Step.new(type: :before, name: name, executor: block)
104
+ before_steps[name].sort_by!(&:path)
103
105
  self
104
106
  end
105
107
 
@@ -120,18 +122,20 @@ module Dry
120
122
  # @api private
121
123
  def merge_callbacks(left, right)
122
124
  left.merge(right) do |_key, oldval, newval|
123
- oldval + newval
125
+ (oldval + newval).sort_by(&:path)
124
126
  end
125
127
  end
126
128
 
127
129
  # @api private
128
130
  def import_callbacks(path, other)
129
131
  other.before_steps.each do |name, steps|
130
- (before_steps[name] ||= []).concat(steps.map { |step| step.scoped(path) })
132
+ before_steps[name] ||= []
133
+ before_steps[name].concat(steps.map { |step| step.scoped(path) }).sort_by!(&:path)
131
134
  end
132
135
 
133
136
  other.after_steps.each do |name, steps|
134
- (after_steps[name] ||= []).concat(steps.map { |step| step.scoped(path) })
137
+ after_steps[name] ||= []
138
+ after_steps[name].concat(steps.map { |step| step.scoped(path) }).sort_by!(&:path)
135
139
  end
136
140
  end
137
141
  end
@@ -15,24 +15,21 @@ module Dry
15
15
  class Result
16
16
  include Dry::Equalizer(:output, :errors)
17
17
 
18
- extend Dry::Initializer
18
+ extend Dry::Initializer[undefined: false]
19
19
 
20
20
  # @api private
21
- param :output
21
+ param :output, reader: false
22
22
 
23
- # Dump result to a hash returning processed and validated data
23
+ # A list of failure ASTs produced by rule result objects
24
24
  #
25
- # @return [Hash]
26
- alias_method :to_h, :output
27
-
28
25
  # @api private
29
- param :results, default: -> { EMPTY_ARRAY.dup }
26
+ option :result_ast, default: -> { EMPTY_ARRAY.dup }
30
27
 
31
28
  # @api private
32
29
  option :message_compiler
33
30
 
34
31
  # @api private
35
- option :parent, default: -> { nil }
32
+ option :path, optional: true, reader: false
36
33
 
37
34
  # @api private
38
35
  def self.new(*, **)
@@ -48,8 +45,8 @@ module Dry
48
45
  # @return [Result]
49
46
  #
50
47
  # @api private
51
- def at(path, &block)
52
- new(Path[path].reduce(output) { |a, e| a[e] }, parent: self, &block)
48
+ def at(at_path, &block)
49
+ new(@output, path: Path.new([*path, *Path[at_path]]), &block)
53
50
  end
54
51
 
55
52
  # @api private
@@ -57,7 +54,7 @@ module Dry
57
54
  self.class.new(
58
55
  output,
59
56
  message_compiler: message_compiler,
60
- results: results,
57
+ result_ast: result_ast,
61
58
  **opts,
62
59
  &block
63
60
  )
@@ -70,14 +67,35 @@ module Dry
70
67
  end
71
68
 
72
69
  # @api private
73
- def replace(hash)
74
- @output = hash
70
+ def path
71
+ @path || Path::EMPTY
72
+ end
73
+
74
+ # Dump result to a hash returning processed and validated data
75
+ #
76
+ # @return [Hash]
77
+ def output
78
+ path.equal?(Path::EMPTY) ? @output : @output.dig(*path)
79
+ end
80
+ alias_method :to_h, :output
81
+
82
+ # @api private
83
+ def replace(value)
84
+ if value.is_a?(output.class)
85
+ output.replace(value)
86
+ elsif path.equal?(Path::EMPTY)
87
+ @output = value
88
+ else
89
+ value_holder = path.keys.length > 1 ? @output.dig(*path.to_a[0..-2]) : @output
90
+
91
+ value_holder[path.last] = value
92
+ end
93
+
75
94
  self
76
95
  end
77
96
 
78
97
  # @api private
79
98
  def concat(other)
80
- results.concat(other)
81
99
  result_ast.concat(other.map(&:to_ast))
82
100
  self
83
101
  end
@@ -164,7 +182,7 @@ module Dry
164
182
  #
165
183
  # @api public
166
184
  def inspect
167
- "#<#{self.class}#{to_h.inspect} errors=#{errors.to_h.inspect}>"
185
+ "#<#{self.class}#{to_h.inspect} errors=#{errors.to_h.inspect} path=#{path.keys.inspect}>"
168
186
  end
169
187
 
170
188
  if RUBY_VERSION >= "2.7"
@@ -182,15 +200,6 @@ module Dry
182
200
  def add_error(node)
183
201
  result_ast << node
184
202
  end
185
-
186
- private
187
-
188
- # A list of failure ASTs produced by rule result objects
189
- #
190
- # @api private
191
- def result_ast
192
- @result_ast ||= results.map(&:to_ast)
193
- end
194
203
  end
195
204
  end
196
205
  end
@@ -17,53 +17,34 @@ module Dry
17
17
  attr_reader :executor
18
18
 
19
19
  # @api private
20
- class Scoped
21
- # @api private
22
- attr_reader :path
23
-
24
- # @api private
25
- attr_reader :step
26
-
27
- # @api private
28
- def initialize(path, step)
29
- @path = Path[path]
30
- @step = step
31
- end
32
-
33
- # @api private
34
- def scoped(new_path)
35
- self.class.new(Path[[*new_path, *path]], step)
36
- end
37
-
38
- # @api private
39
- def call(result)
40
- result.at(path) do |scoped_result|
41
- output = step.(scoped_result).to_h
42
- target = Array(path)[0..-2].reduce(result) { |a, e| a[e] }
43
-
44
- target.update(path.last => output)
45
- end
46
- end
47
- end
20
+ attr_reader :path
48
21
 
49
22
  # @api private
50
- def initialize(type:, name:, executor:)
23
+ def initialize(type:, name:, executor:, path: Path::EMPTY)
51
24
  @type = type
52
25
  @name = name
53
26
  @executor = executor
27
+ @path = path
54
28
  validate_name(name)
55
29
  end
56
30
 
57
31
  # @api private
58
32
  def call(result)
59
- output = executor.(result)
60
- result.replace(output) if output.is_a?(Hash)
33
+ scoped_result = path.equal?(Path::EMPTY) ? result : result.at(path)
34
+
35
+ output = executor.(scoped_result)
36
+ scoped_result.replace(output) if output.is_a?(Hash)
61
37
  output
62
38
  end
63
39
 
64
40
  # @api private
65
- def scoped(path)
66
- Scoped.new(path, self)
41
+ def scoped(parent_path)
42
+ self.class.new(
43
+ type: type,
44
+ name: name,
45
+ executor: executor,
46
+ path: Path.new([*parent_path, *path])
47
+ )
67
48
  end
68
49
 
69
50
  private
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Dry
4
4
  module Schema
5
- VERSION = "1.6.1"
5
+ VERSION = "1.8.0"
6
6
  end
7
7
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dry-schema
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.6.1
4
+ version: 1.8.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: 2021-02-02 00:00:00.000000000 Z
11
+ date: 2021-09-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: concurrent-ruby
@@ -30,20 +30,20 @@ dependencies:
30
30
  requirements:
31
31
  - - "~>"
32
32
  - !ruby/object:Gem::Version
33
- version: '0.8'
33
+ version: '0.13'
34
34
  - - ">="
35
35
  - !ruby/object:Gem::Version
36
- version: 0.8.3
36
+ version: 0.13.0
37
37
  type: :runtime
38
38
  prerelease: false
39
39
  version_requirements: !ruby/object:Gem::Requirement
40
40
  requirements:
41
41
  - - "~>"
42
42
  - !ruby/object:Gem::Version
43
- version: '0.8'
43
+ version: '0.13'
44
44
  - - ">="
45
45
  - !ruby/object:Gem::Version
46
- version: 0.8.3
46
+ version: 0.13.0
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: dry-core
49
49
  requirement: !ruby/object:Gem::Requirement
@@ -148,10 +148,11 @@ dependencies:
148
148
  - - ">="
149
149
  - !ruby/object:Gem::Version
150
150
  version: '0'
151
- description: |
151
+ description: |+
152
152
  dry-schema provides a DSL for defining schemas with keys and rules that should be applied to
153
153
  values. It supports coercion, input sanitization, custom types and localized error messages
154
154
  (with or without I18n gem). It's also used as the schema engine in dry-validation.
155
+
155
156
  email:
156
157
  - piotr.solnica@gmail.com
157
158
  executables: []
@@ -245,14 +246,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
245
246
  requirements:
246
247
  - - ">="
247
248
  - !ruby/object:Gem::Version
248
- version: 2.5.0
249
+ version: 2.6.0
249
250
  required_rubygems_version: !ruby/object:Gem::Requirement
250
251
  requirements:
251
252
  - - ">="
252
253
  - !ruby/object:Gem::Version
253
254
  version: '0'
254
255
  requirements: []
255
- rubygems_version: 3.1.4
256
+ rubygems_version: 3.1.6
256
257
  signing_key:
257
258
  specification_version: 4
258
259
  summary: Coercion and validation for data structures