dry-schema 1.4.3 → 1.5.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (68) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +170 -97
  3. data/config/errors.yml +4 -0
  4. data/dry-schema.gemspec +46 -0
  5. data/lib/dry-schema.rb +1 -1
  6. data/lib/dry/schema.rb +19 -6
  7. data/lib/dry/schema/compiler.rb +4 -4
  8. data/lib/dry/schema/config.rb +15 -6
  9. data/lib/dry/schema/constants.rb +16 -7
  10. data/lib/dry/schema/dsl.rb +88 -27
  11. data/lib/dry/schema/extensions.rb +10 -2
  12. data/lib/dry/schema/extensions/hints.rb +15 -8
  13. data/lib/dry/schema/extensions/hints/message_compiler_methods.rb +1 -1
  14. data/lib/dry/schema/extensions/hints/message_set_methods.rb +0 -47
  15. data/lib/dry/schema/extensions/info.rb +27 -0
  16. data/lib/dry/schema/extensions/info/schema_compiler.rb +105 -0
  17. data/lib/dry/schema/extensions/monads.rb +1 -1
  18. data/lib/dry/schema/extensions/struct.rb +32 -0
  19. data/lib/dry/schema/json.rb +1 -1
  20. data/lib/dry/schema/key.rb +16 -1
  21. data/lib/dry/schema/key_coercer.rb +4 -4
  22. data/lib/dry/schema/key_map.rb +9 -4
  23. data/lib/dry/schema/key_validator.rb +66 -0
  24. data/lib/dry/schema/macros.rb +8 -8
  25. data/lib/dry/schema/macros/array.rb +17 -4
  26. data/lib/dry/schema/macros/core.rb +9 -4
  27. data/lib/dry/schema/macros/dsl.rb +34 -19
  28. data/lib/dry/schema/macros/each.rb +4 -4
  29. data/lib/dry/schema/macros/filled.rb +5 -5
  30. data/lib/dry/schema/macros/hash.rb +21 -3
  31. data/lib/dry/schema/macros/key.rb +9 -9
  32. data/lib/dry/schema/macros/maybe.rb +3 -3
  33. data/lib/dry/schema/macros/optional.rb +1 -1
  34. data/lib/dry/schema/macros/required.rb +1 -1
  35. data/lib/dry/schema/macros/schema.rb +23 -2
  36. data/lib/dry/schema/macros/value.rb +32 -10
  37. data/lib/dry/schema/message.rb +35 -9
  38. data/lib/dry/schema/message/or.rb +18 -39
  39. data/lib/dry/schema/message/or/abstract.rb +28 -0
  40. data/lib/dry/schema/message/or/multi_path.rb +37 -0
  41. data/lib/dry/schema/message/or/single_path.rb +64 -0
  42. data/lib/dry/schema/message_compiler.rb +37 -17
  43. data/lib/dry/schema/message_compiler/visitor_opts.rb +2 -2
  44. data/lib/dry/schema/message_set.rb +25 -36
  45. data/lib/dry/schema/messages.rb +6 -6
  46. data/lib/dry/schema/messages/abstract.rb +54 -56
  47. data/lib/dry/schema/messages/i18n.rb +29 -27
  48. data/lib/dry/schema/messages/namespaced.rb +12 -2
  49. data/lib/dry/schema/messages/template.rb +19 -44
  50. data/lib/dry/schema/messages/yaml.rb +60 -13
  51. data/lib/dry/schema/params.rb +1 -1
  52. data/lib/dry/schema/path.rb +44 -5
  53. data/lib/dry/schema/predicate.rb +2 -2
  54. data/lib/dry/schema/predicate_inferrer.rb +4 -184
  55. data/lib/dry/schema/predicate_registry.rb +2 -2
  56. data/lib/dry/schema/primitive_inferrer.rb +16 -0
  57. data/lib/dry/schema/processor.rb +49 -28
  58. data/lib/dry/schema/processor_steps.rb +50 -27
  59. data/lib/dry/schema/result.rb +43 -5
  60. data/lib/dry/schema/rule_applier.rb +8 -7
  61. data/lib/dry/schema/step.rb +79 -0
  62. data/lib/dry/schema/trace.rb +5 -4
  63. data/lib/dry/schema/type_container.rb +3 -3
  64. data/lib/dry/schema/type_registry.rb +2 -2
  65. data/lib/dry/schema/types.rb +1 -1
  66. data/lib/dry/schema/value_coercer.rb +2 -2
  67. data/lib/dry/schema/version.rb +1 -1
  68. metadata +22 -8
@@ -1,9 +1,9 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'dry/initializer'
4
- require 'dry/equalizer'
3
+ require "dry/initializer"
4
+ require "dry/equalizer"
5
5
 
6
- require 'dry/schema/path'
6
+ require "dry/schema/path"
7
7
 
8
8
  module Dry
9
9
  module Schema
@@ -31,13 +31,44 @@ module Dry
31
31
  # @api private
32
32
  option :message_compiler
33
33
 
34
+ # @api private
35
+ option :parent, default: -> { nil }
36
+
34
37
  # @api private
35
38
  def self.new(*, **)
36
39
  result = super
37
- yield(result)
40
+ yield(result) if block_given?
38
41
  result.freeze
39
42
  end
40
43
 
44
+ # Return a new result scoped to a specific path
45
+ #
46
+ # @param [Symbol, Array, Path]
47
+ #
48
+ # @return [Result]
49
+ #
50
+ # @api private
51
+ def at(path, &block)
52
+ new(Path[path].reduce(output) { |a, e| a[e] }, parent: self, &block)
53
+ end
54
+
55
+ # @api private
56
+ def new(output, **opts, &block)
57
+ self.class.new(
58
+ output,
59
+ message_compiler: message_compiler,
60
+ results: results,
61
+ **opts,
62
+ &block
63
+ )
64
+ end
65
+
66
+ # @api private
67
+ def update(hash)
68
+ output.update(hash)
69
+ self
70
+ end
71
+
41
72
  # @api private
42
73
  def replace(hash)
43
74
  @output = hash
@@ -136,7 +167,7 @@ module Dry
136
167
  "#<#{self.class}#{to_h.inspect} errors=#{errors.to_h.inspect}>"
137
168
  end
138
169
 
139
- if RUBY_VERSION >= '2.7'
170
+ if RUBY_VERSION >= "2.7"
140
171
  # Pattern matching support
141
172
  #
142
173
  # @api private
@@ -145,6 +176,13 @@ module Dry
145
176
  end
146
177
  end
147
178
 
179
+ # Add a new error AST node
180
+ #
181
+ # @api private
182
+ def add_error(node)
183
+ result_ast << node
184
+ end
185
+
148
186
  private
149
187
 
150
188
  # A list of failure ASTs produced by rule result objects
@@ -1,12 +1,13 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'dry/initializer'
3
+ require "dry/initializer"
4
4
 
5
- require 'dry/schema/constants'
6
- require 'dry/schema/config'
7
- require 'dry/schema/result'
8
- require 'dry/schema/messages'
9
- require 'dry/schema/message_compiler'
5
+ require "dry/schema"
6
+ require "dry/schema/constants"
7
+ require "dry/schema/config"
8
+ require "dry/schema/result"
9
+ require "dry/schema/messages"
10
+ require "dry/schema/message_compiler"
10
11
 
11
12
  module Dry
12
13
  module Schema
@@ -20,7 +21,7 @@ module Dry
20
21
  param :rules
21
22
 
22
23
  # @api private
23
- option :config, default: -> { Config.new }
24
+ option :config, default: -> { Schema.config.dup }
24
25
 
25
26
  # @api private
26
27
  option :message_compiler, default: -> { MessageCompiler.new(Messages.setup(config.messages)) }
@@ -0,0 +1,79 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "dry/schema/constants"
4
+ require "dry/schema/path"
5
+
6
+ module Dry
7
+ module Schema
8
+ # @api private
9
+ class Step
10
+ # @api private
11
+ attr_reader :name
12
+
13
+ # @api private
14
+ attr_reader :type
15
+
16
+ # @api private
17
+ attr_reader :executor
18
+
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
48
+
49
+ # @api private
50
+ def initialize(type:, name:, executor:)
51
+ @type = type
52
+ @name = name
53
+ @executor = executor
54
+ validate_name(name)
55
+ end
56
+
57
+ # @api private
58
+ def call(result)
59
+ output = executor.(result)
60
+ result.replace(output) if output.is_a?(Hash)
61
+ output
62
+ end
63
+
64
+ # @api private
65
+ def scoped(path)
66
+ Scoped.new(path, self)
67
+ end
68
+
69
+ private
70
+
71
+ # @api private
72
+ def validate_name(name)
73
+ return if STEPS_IN_ORDER.include?(name)
74
+
75
+ raise ArgumentError, "Undefined step name #{name}. Available names: #{STEPS_IN_ORDER}"
76
+ end
77
+ end
78
+ end
79
+ end
@@ -1,8 +1,8 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'dry/schema/constants'
4
- require 'dry/schema/compiler'
5
- require 'dry/schema/predicate'
3
+ require "dry/schema/constants"
4
+ require "dry/schema/compiler"
5
+ require "dry/schema/predicate"
6
6
 
7
7
  module Dry
8
8
  module Schema
@@ -29,8 +29,9 @@ module Dry
29
29
  end
30
30
 
31
31
  # @api private
32
- def evaluate(*args, type_spec: ::Dry::Schema::Undefined, **opts)
32
+ def evaluate(*args, **opts)
33
33
  predicates = opts.empty? ? args : args.push(opts)
34
+
34
35
  evaluate_predicates(predicates).each do |rule|
35
36
  append(rule)
36
37
  end
@@ -1,7 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'dry/container'
4
- require 'dry/types'
3
+ require "dry/container"
4
+ require "dry/types"
5
5
 
6
6
  module Dry
7
7
  module Schema
@@ -21,7 +21,7 @@ module Dry
21
21
  merge(types_container)
22
22
  end
23
23
 
24
- alias registered? key?
24
+ alias_method :registered?, :key?
25
25
  end
26
26
  end
27
27
  end
@@ -1,7 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'dry/schema/constants'
4
- require 'dry/schema/types'
3
+ require "dry/schema/constants"
4
+ require "dry/schema/types"
5
5
 
6
6
  module Dry
7
7
  module Schema
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'dry/types'
3
+ require "dry/types"
4
4
 
5
5
  module Dry
6
6
  module Schema
@@ -1,7 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'dry/equalizer'
4
- require 'dry/initializer'
3
+ require "dry/equalizer"
4
+ require "dry/initializer"
5
5
 
6
6
  module Dry
7
7
  module Schema
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Dry
4
4
  module Schema
5
- VERSION = '1.4.3'
5
+ VERSION = "1.5.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.4.3
4
+ version: 1.5.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: 2020-01-08 00:00:00.000000000 Z
11
+ date: 2020-03-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: concurrent-ruby
@@ -106,14 +106,14 @@ dependencies:
106
106
  requirements:
107
107
  - - "~>"
108
108
  - !ruby/object:Gem::Version
109
- version: '1.2'
109
+ version: '1.4'
110
110
  type: :runtime
111
111
  prerelease: false
112
112
  version_requirements: !ruby/object:Gem::Requirement
113
113
  requirements:
114
114
  - - "~>"
115
115
  - !ruby/object:Gem::Version
116
- version: '1.2'
116
+ version: '1.4'
117
117
  - !ruby/object:Gem::Dependency
118
118
  name: bundler
119
119
  requirement: !ruby/object:Gem::Requirement
@@ -170,6 +170,7 @@ files:
170
170
  - LICENSE
171
171
  - README.md
172
172
  - config/errors.yml
173
+ - dry-schema.gemspec
173
174
  - lib/dry-schema.rb
174
175
  - lib/dry/schema.rb
175
176
  - lib/dry/schema/compiler.rb
@@ -182,11 +183,15 @@ files:
182
183
  - lib/dry/schema/extensions/hints/message_compiler_methods.rb
183
184
  - lib/dry/schema/extensions/hints/message_set_methods.rb
184
185
  - lib/dry/schema/extensions/hints/result_methods.rb
186
+ - lib/dry/schema/extensions/info.rb
187
+ - lib/dry/schema/extensions/info/schema_compiler.rb
185
188
  - lib/dry/schema/extensions/monads.rb
189
+ - lib/dry/schema/extensions/struct.rb
186
190
  - lib/dry/schema/json.rb
187
191
  - lib/dry/schema/key.rb
188
192
  - lib/dry/schema/key_coercer.rb
189
193
  - lib/dry/schema/key_map.rb
194
+ - lib/dry/schema/key_validator.rb
190
195
  - lib/dry/schema/macros.rb
191
196
  - lib/dry/schema/macros/array.rb
192
197
  - lib/dry/schema/macros/core.rb
@@ -202,6 +207,9 @@ files:
202
207
  - lib/dry/schema/macros/value.rb
203
208
  - lib/dry/schema/message.rb
204
209
  - lib/dry/schema/message/or.rb
210
+ - lib/dry/schema/message/or/abstract.rb
211
+ - lib/dry/schema/message/or/multi_path.rb
212
+ - lib/dry/schema/message/or/single_path.rb
205
213
  - lib/dry/schema/message_compiler.rb
206
214
  - lib/dry/schema/message_compiler/visitor_opts.rb
207
215
  - lib/dry/schema/message_set.rb
@@ -217,20 +225,26 @@ files:
217
225
  - lib/dry/schema/predicate.rb
218
226
  - lib/dry/schema/predicate_inferrer.rb
219
227
  - lib/dry/schema/predicate_registry.rb
228
+ - lib/dry/schema/primitive_inferrer.rb
220
229
  - lib/dry/schema/processor.rb
221
230
  - lib/dry/schema/processor_steps.rb
222
231
  - lib/dry/schema/result.rb
223
232
  - lib/dry/schema/rule_applier.rb
233
+ - lib/dry/schema/step.rb
224
234
  - lib/dry/schema/trace.rb
225
235
  - lib/dry/schema/type_container.rb
226
236
  - lib/dry/schema/type_registry.rb
227
237
  - lib/dry/schema/types.rb
228
238
  - lib/dry/schema/value_coercer.rb
229
239
  - lib/dry/schema/version.rb
230
- homepage: https://github.com/dry-rb/dry-schema
240
+ homepage: https://dry-rb.org/gems/dry-schema
231
241
  licenses:
232
242
  - MIT
233
- metadata: {}
243
+ metadata:
244
+ allowed_push_host: https://rubygems.org
245
+ changelog_uri: https://github.com/dry-rb/dry-schema/blob/master/CHANGELOG.md
246
+ source_code_uri: https://github.com/dry-rb/dry-schema
247
+ bug_tracker_uri: https://github.com/dry-rb/dry-schema/issues
234
248
  post_install_message:
235
249
  rdoc_options: []
236
250
  require_paths:
@@ -239,14 +253,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
239
253
  requirements:
240
254
  - - ">="
241
255
  - !ruby/object:Gem::Version
242
- version: '2.4'
256
+ version: 2.4.0
243
257
  required_rubygems_version: !ruby/object:Gem::Requirement
244
258
  requirements:
245
259
  - - ">="
246
260
  - !ruby/object:Gem::Version
247
261
  version: '0'
248
262
  requirements: []
249
- rubygems_version: 3.1.2
263
+ rubygems_version: 3.0.3
250
264
  signing_key:
251
265
  specification_version: 4
252
266
  summary: Coercion and validation for data structures