dry-validation 1.3.1 → 1.4.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: f61fe3205cc418d53bd0f2217bdd49a0e49119a69cae87ecf24e76a89567f845
4
- data.tar.gz: 4ca306db1fd196c3253a78a6376fe0d4c197c5c8d625636bc37038b9caa3278b
3
+ metadata.gz: 7da2738360b969decba415a2a6418b41ca8b55202d39dd6843871722b904164e
4
+ data.tar.gz: 20cfc387c6ebe8ce8ffd9b6d9227d59393cf304ba70bdd01ace0ff13d67e99fe
5
5
  SHA512:
6
- metadata.gz: d0c6a082c4a7077bd428e1bbe2f8ec4677950ea0f073d15cb955568307e2924b75e9d4bfb0f023b65739df7d061569e1aa8a3941968c263d68f4fd3fe12a61d5
7
- data.tar.gz: 50fa2999ebe80ce40fe244b10bac6cf3644f4dd7e7825913726e8cc2587a901f0d2d164d1c29e17b55f613bd7258dc13d672a5787334f90afc4059d0480312b9
6
+ metadata.gz: 5d86de3a87c58f51dabd171950a3d72398e3a93fde3ac521937c0e7b13518388519238127968eb330b991774a2d017b109e3ddc919e23d423771c2bee6361a63
7
+ data.tar.gz: 86c6cc8cf5ea0ba31dda616f68cedd2481f9e1f8455d787d4575583af7ad7428cee4de45477bf454084adacd6778216f2867dc95ac0cbe1e9fad2889a07f124a
@@ -1,8 +1,42 @@
1
+ # v1.4.1 2020-01-08
2
+
3
+ ### Added
4
+
5
+ - Pattern matching on result values (@flash-gordon)
6
+
7
+ ### Fixed
8
+
9
+ - List tokens are correctly interpolated as a comma separated list in rule messages (see #611) (@waiting-for-dev)
10
+ - Warnings about delegated keywords (@flash-gordon)
11
+ - A few delegation warnings about keyword arguments (@flash-gordon)
12
+
13
+ [Compare v1.4.0...v1.4.1](https://github.com/dry-rb/dry-validation/compare/v1.4.0...v1.4.1)
14
+
15
+ # v1.4.0 2019-12-12
16
+
17
+ ### Added
18
+
19
+ - Support for multi-schema inheritance (@ianwhite)
20
+
21
+ ### Fixed
22
+
23
+ - Keyword warnings reported by Ruby 2.7 (@flash-gordon)
24
+ - Fixed an issue where `MessageSet` would be marked as empty too early (@ianwhite)
25
+ - Messages are correctly generated when there are errors for both an array and one or more of its elements (see #599) (@Bugagazavr)
26
+
27
+ ### Changed
28
+
29
+ - A meaningful exception is raised when failure options are not valid (@MatElGran)
30
+ - [internal] improved performance in `Contract.ensure_valid_keys` (@grzegorz-jakubiak)
31
+ - [internal] fixed keyword warnings on MRI 2.7.0 (@flash-gordon)
32
+
33
+ [Compare v1.3.1...v1.4.0](https://github.com/dry-rb/dry-validation/compare/v1.3.1...v1.4.0)
34
+
1
35
  # v1.3.1 2019-08-16
2
36
 
3
37
  ### Changed
4
38
 
5
- * You can now set an external schema without providing a block (alassek)
39
+ - You can now set an external schema without providing a block (@alassek)
6
40
 
7
41
  [Compare v1.3.0...v1.3.1](https://github.com/dry-rb/dry-validation/compare/v1.3.0...v1.3.1)
8
42
 
@@ -10,15 +44,15 @@
10
44
 
11
45
  ### Added
12
46
 
13
- * Support for setting an external schema (that can be extended too) (fixed #574) (@solnic)
47
+ - Support for setting an external schema (that can be extended too) (fixed #574) (@solnic)
14
48
 
15
49
  ### Fixed
16
50
 
17
- * Using a hash spec to define rule keys with more than one value is properly handled by rule guard now (fixed #576) (@solnic)
51
+ - Using a hash spec to define rule keys with more than one value is properly handled by rule guard now (fixed #576) (@solnic)
18
52
 
19
53
  ### Changed
20
54
 
21
- * `values` within rules uses `Hash#fetch_values` internally now, which improves performance (@esparta)
55
+ - `values` within rules uses `Hash#fetch_values` internally now, which improves performance (@esparta)
22
56
 
23
57
  [Compare v1.2.1...v1.3.0](https://github.com/dry-rb/dry-validation/compare/v1.2.1...v1.3.0)
24
58
 
@@ -26,10 +60,10 @@
26
60
 
27
61
  ### Fixed
28
62
 
29
- * Defining an abstract contract class that has no schema no longer crashes (issue #565) (@solnic)
30
- * Fixed an issue where `Rule#each` would crash when the value is not an array (issue #567) (@solnic)
31
- * Fixed an issue where guarding a rule would crash when keys are missing in the input (issue #569) (@solnic)
32
- * Added missing "pathname" require (issue #570) (@solnic)
63
+ - Defining an abstract contract class that has no schema no longer crashes (issue #565) (@solnic)
64
+ - Fixed an issue where `Rule#each` would crash when the value is not an array (issue #567) (@solnic)
65
+ - Fixed an issue where guarding a rule would crash when keys are missing in the input (issue #569) (@solnic)
66
+ - Added missing "pathname" require (issue #570) (@solnic)
33
67
 
34
68
  [Compare v1.2.0...v1.2.1](https://github.com/dry-rb/dry-validation/compare/v1.2.0...v1.2.1)
35
69
 
@@ -37,15 +71,15 @@
37
71
 
38
72
  ### Added
39
73
 
40
- * New extension `:predicates_as_macros` (@waiting-for-dev)
74
+ - New extension `:predicates_as_macros` (@waiting-for-dev)
41
75
 
42
76
  ### Fixed
43
77
 
44
- * Guarding rules for nested keys works correctly (issue #560) (@solnic)
78
+ - Guarding rules for nested keys works correctly (issue #560) (@solnic)
45
79
 
46
80
  ### Changed
47
81
 
48
- * `dry-schema` dependency was bumped to `>= 1.3.1` (@solnic)
82
+ - `dry-schema` dependency was bumped to `>= 1.3.1` (@solnic)
49
83
 
50
84
  [Compare v1.1.1...v1.2.0](https://github.com/dry-rb/dry-validation/compare/v1.1.1...v1.2.0)
51
85
 
@@ -53,7 +87,7 @@
53
87
 
54
88
  ### Fixed
55
89
 
56
- * `Rule#each` works with array values from nested hashes (@mustardnoise)
90
+ - `Rule#each` works with array values from nested hashes (@mustardnoise)
57
91
 
58
92
  [Compare v1.1.0...v1.1.1](https://github.com/dry-rb/dry-validation/compare/v1.1.0...v1.1.1)
59
93
 
@@ -61,13 +95,13 @@
61
95
 
62
96
  ### Added
63
97
 
64
- * `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)
65
- * `value` supports hash-based path specifications now (refs #547) (@solnic)
66
- * `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)
98
+ - `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)
99
+ - `value` supports hash-based path specifications now (refs #547) (@solnic)
100
+ - `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)
67
101
 
68
102
  ### Fixed
69
103
 
70
- * Passing multiple macro names to `validate` or `each` works correctly (fixed #538 #541) (@jandudulski)
104
+ - Passing multiple macro names to `validate` or `each` works correctly (fixed #538 #541) (@jandudulski)
71
105
 
72
106
  [Compare v1.0.0...v1.1.0](https://github.com/dry-rb/dry-validation/compare/v1.0.0...v1.1.0)
73
107
 
@@ -114,14 +148,14 @@ See [the list of all addressed issues](https://github.com/dry-rb/dry-validation/
114
148
 
115
149
  ### Added
116
150
 
117
- * [EXPERIMENTAL] `Validation.register_macro` for registering global macros (solnic)
118
- * [EXPERIMENTAL] `Contract.register_macro` for registering macros available to specific contract classes (solnic)
119
- * `Dry::Validation.Contract` shortcut for quickly defining a contract and getting its instance back (solnic)
120
- * New configuration option `config.locale` for setting the default locale (solnic)
151
+ - [EXPERIMENTAL] `Validation.register_macro` for registering global macros (solnic)
152
+ - [EXPERIMENTAL] `Contract.register_macro` for registering macros available to specific contract classes (solnic)
153
+ - `Dry::Validation.Contract` shortcut for quickly defining a contract and getting its instance back (solnic)
154
+ - New configuration option `config.locale` for setting the default locale (solnic)
121
155
 
122
156
  ### Fixed
123
157
 
124
- * `config/errors.yml` are now bundled with the gem, **`rc2` was broken because of this** (solnic)
158
+ - `config/errors.yml` are now bundled with the gem, **`rc2` was broken because of this** (solnic)
125
159
 
126
160
  [Compare v1.0.0.rc2...v1.0.0.rc3](https://github.com/dry-rb/dry-validation/compare/v1.0.0.rc2...v1.0.0.rc3)
127
161
 
@@ -131,17 +165,17 @@ This was **yanked** on rubygems.org because the bundled gem was missing `config`
131
165
 
132
166
  ### Added
133
167
 
134
- * [EXPERIMENTAL] support for registering macros via `Dry::Validation::Macros.register(:your_macro, &block)` (solnic)
135
- * [EXPERIMENTAL] `:acceptance` as the first built-in macro (issue #157) (solnic)
168
+ - [EXPERIMENTAL] support for registering macros via `Dry::Validation::Macros.register(:your_macro, &block)` (solnic)
169
+ - [EXPERIMENTAL] `:acceptance` as the first built-in macro (issue #157) (solnic)
136
170
 
137
171
  ### Fixed
138
172
 
139
- * Passing invalid argument to `failure` will raise a meaningful error instead of crashing (solnic)
173
+ - Passing invalid argument to `failure` will raise a meaningful error instead of crashing (solnic)
140
174
 
141
175
  ### Changed
142
176
 
143
- * In rule validation blocks, `values` is now an instance of a hash-like `Dry::Validation::Values` class, rather than `Dry::Schema::Result`. This gives more convenient access to data within rules (solnic)
144
- * Dependency on `dry-schema` was updated to `~> 1.0` (solnic)
177
+ - In rule validation blocks, `values` is now an instance of a hash-like `Dry::Validation::Values` class, rather than `Dry::Schema::Result`. This gives more convenient access to data within rules (solnic)
178
+ - Dependency on `dry-schema` was updated to `~> 1.0` (solnic)
145
179
 
146
180
  [Compare v1.0.0.rc1...v1.0.0.rc2](https://github.com/dry-rb/dry-validation/compare/v1.0.0.rc1...v1.0.0.rc2)
147
181
 
@@ -149,18 +183,18 @@ This was **yanked** on rubygems.org because the bundled gem was missing `config`
149
183
 
150
184
  ### Added
151
185
 
152
- * `:hints` extension is back (solnic)
153
- * `Result` objects have access to the context object which is shared between rules (flash-gordon)
186
+ - `:hints` extension is back (solnic)
187
+ - `Result` objects have access to the context object which is shared between rules (flash-gordon)
154
188
 
155
189
  ### Fixed
156
190
 
157
- * Multiple hint messages no longer crash message set (flash-gordon)
158
- * `Contract#inspect` no longer crashes (solnic)
191
+ - Multiple hint messages no longer crash message set (flash-gordon)
192
+ - `Contract#inspect` no longer crashes (solnic)
159
193
 
160
194
  ### Changed
161
195
 
162
- * Dependency on `dry-schema` was bumped to `~> 0.6` - this pulls in `dry-types 1.0.0` and `dry-logic 1.0.0` (solnic)
163
- * Dependency on `dry-initializer` was bumped to `~> 3.0` (solnic)
196
+ - Dependency on `dry-schema` was bumped to `~> 0.6` - this pulls in `dry-types 1.0.0` and `dry-logic 1.0.0` (solnic)
197
+ - Dependency on `dry-initializer` was bumped to `~> 3.0` (solnic)
164
198
 
165
199
  [Compare v1.0.0.beta2...v1.0.0.rc1](https://github.com/dry-rb/dry-validation/compare/v1.0.0.beta2...v1.0.0.rc1)
166
200
 
@@ -168,7 +202,7 @@ This was **yanked** on rubygems.org because the bundled gem was missing `config`
168
202
 
169
203
  ### Added
170
204
 
171
- * Support for arbitrary meta-data in failures, ie:
205
+ - Support for arbitrary meta-data in failures, ie:
172
206
 
173
207
  ```ruby
174
208
  class NewUserContract < Dry::Validation::Contract
@@ -186,9 +220,9 @@ This was **yanked** on rubygems.org because the bundled gem was missing `config`
186
220
 
187
221
  ### Changed
188
222
 
189
- * [BREAKING] `Error` was renamed to `Message` as it is a more generic concept (solnic)
190
- * [BREAKING] `ErrorSet` was renamed to `MessageSet` for consistency (solnic)
191
- * [BREAKING] `:monads` extension wraps entire result objects in `Success` or `Failure` (flash-gordon)
223
+ - [BREAKING] `Error` was renamed to `Message` as it is a more generic concept (solnic)
224
+ - [BREAKING] `ErrorSet` was renamed to `MessageSet` for consistency (solnic)
225
+ - [BREAKING] `:monads` extension wraps entire result objects in `Success` or `Failure` (flash-gordon)
192
226
 
193
227
  [Compare v1.0.0.beta1...v1.0.0.beta2](https://github.com/dry-rb/dry-validation/compare/v1.0.0.beta1...v1.0.0.beta2)
194
228
 
@@ -196,20 +230,20 @@ This was **yanked** on rubygems.org because the bundled gem was missing `config`
196
230
 
197
231
  ### Added
198
232
 
199
- * New API for setting failures `base.failure` for base errors and `key.failure` for key errors (solnic)
200
- * Support for `base` errors associated with a key even when child keys have errors too (solnic)
201
- * Support for `base` errors not associated with any key (solnic)
202
- * Result objects use `ErrorSet` object now for managing messages (solnic)
203
- * Nested keys are properly handled when generating messages hash (issue #489) (flash-gordon + solnic)
204
- * Result objects support `locale` and `full` options now (solnic)
205
- * Ability to configure `top_namespace` for messages, which will be used for both schema and rule localization (solnic)
206
- * Rule blocks receive a context object that you can use to share data between rules (solnic)
233
+ - New API for setting failures `base.failure` for base errors and `key.failure` for key errors (solnic)
234
+ - Support for `base` errors associated with a key even when child keys have errors too (solnic)
235
+ - Support for `base` errors not associated with any key (solnic)
236
+ - Result objects use `ErrorSet` object now for managing messages (solnic)
237
+ - Nested keys are properly handled when generating messages hash (issue #489) (flash-gordon + solnic)
238
+ - Result objects support `locale` and `full` options now (solnic)
239
+ - Ability to configure `top_namespace` for messages, which will be used for both schema and rule localization (solnic)
240
+ - Rule blocks receive a context object that you can use to share data between rules (solnic)
207
241
 
208
242
  ### Changed
209
243
 
210
- * [BREAKING] `Result#errors` returns an instance of `ErrorSet` now, it's an enumerable, coerible to a hash (solnic)
211
- * [BREAKING] `failure` was removed in favor of `key.failure` or `key(:foo).failure` (solnic)
212
- * [BREAKING] `Result#to_hash` was removed (flash-gordon)
244
+ - [BREAKING] `Result#errors` returns an instance of `ErrorSet` now, it's an enumerable, coerible to a hash (solnic)
245
+ - [BREAKING] `failure` was removed in favor of `key.failure` or `key(:foo).failure` (solnic)
246
+ - [BREAKING] `Result#to_hash` was removed (flash-gordon)
213
247
 
214
248
  [Compare v1.0.0.alpha2...v1.0.0.beta1](https://github.com/dry-rb/dry-validation/compare/v1.0.0.alpha2...v1.0.0.beta1)
215
249
 
@@ -219,13 +253,13 @@ First round of bug fixes. Thanks for testing <3!
219
253
 
220
254
  ### Fixed
221
255
 
222
- * Errors with nested messages are correctly built (flash-gordon)
223
- * Messages for nested keys are correctly resolved (solnic)
224
- * A message for a nested key is resolved when it's defined under `errors.rule.%{key}` too, but a message under nested key will override it (solnic)
256
+ - Errors with nested messages are correctly built (flash-gordon)
257
+ - Messages for nested keys are correctly resolved (solnic)
258
+ - A message for a nested key is resolved when it's defined under `errors.rule.%{key}` too, but a message under nested key will override it (solnic)
225
259
 
226
260
  ### Changed
227
261
 
228
- * When a message template is not found a more meaningful error is raised that includes both rule identifier and key path (solnic)
262
+ - When a message template is not found a more meaningful error is raised that includes both rule identifier and key path (solnic)
229
263
 
230
264
  [Compare v1.0.0.alpha1...v1.0.0.alpha2](https://github.com/dry-rb/dry-validation/compare/v1.0.0.alpha1...v1.0.0.alpha2)
231
265
 
@@ -235,8 +269,8 @@ Complete rewrite on top of `dry-schema`.
235
269
 
236
270
  ### Added
237
271
 
238
- * [BREAKING] `Dry::Validation::Contract` as a replacement for validation schemas (solnic)
239
- * [BREAKING] New `rule` DSL with an improved API for setting error messages (solnic)
272
+ - [BREAKING] `Dry::Validation::Contract` as a replacement for validation schemas (solnic)
273
+ - [BREAKING] New `rule` DSL with an improved API for setting error messages (solnic)
240
274
 
241
275
  [Compare v0.13.0...v1.0.0.alpha1](https://github.com/dry-rb/dry-validation/compare/v0.13.0...v1.0.0.alpha1)
242
276
 
@@ -257,7 +291,7 @@ Complete rewrite on top of `dry-schema`.
257
291
 
258
292
  ### Changed
259
293
 
260
- * [internal] dry-logic was pinned to `~> 0.4.2` (flash-gordon)
294
+ - [internal] dry-logic was pinned to `~> 0.4.2` (flash-gordon)
261
295
 
262
296
  [Compare v0.12.2...v0.12.3](https://github.com/dry-rb/dry-validation/compare/v0.12.2...v0.12.3)
263
297
 
data/LICENSE CHANGED
@@ -1,6 +1,6 @@
1
1
  The MIT License (MIT)
2
2
 
3
- Copyright (c) 2015-2019 dry-rb team
3
+ Copyright (c) 2015-2020 dry-rb team
4
4
 
5
5
  Permission is hereby granted, free of charge, to any person obtaining a copy of
6
6
  this software and associated documentation files (the "Software"), to deal in
data/README.md CHANGED
@@ -1,26 +1,25 @@
1
1
  [gem]: https://rubygems.org/gems/dry-validation
2
- [travis]: https://travis-ci.com/dry-rb/dry-validation
3
- [codeclimate]: https://codeclimate.com/github/dry-rb/dry-validation
2
+ [actions]: https://github.com/dry-rb/dry-validation/actions
3
+ [codacy]: https://www.codacy.com/gh/dry-rb/dry-validation
4
4
  [chat]: https://dry-rb.zulipchat.com
5
5
  [inchpages]: http://inch-ci.org/github/dry-rb/dry-validation
6
6
 
7
7
  # dry-validation [![Join the chat at https://dry-rb.zulipchat.com](https://img.shields.io/badge/dry--rb-join%20chat-%23346b7a.svg)][chat]
8
8
 
9
9
  [![Gem Version](https://badge.fury.io/rb/dry-validation.svg)][gem]
10
- [![Build Status](https://travis-ci.com/dry-rb/dry-validation.svg?branch=master)][travis]
11
- [![Code Climate](https://codeclimate.com/github/dry-rb/dry-validation/badges/gpa.svg)][codeclimate]
12
- [![Test Coverage](https://codeclimate.com/github/dry-rb/dry-validation/badges/coverage.svg)][codeclimate]
10
+ [![CI Status](https://github.com/dry-rb/dry-validation/workflows/ci/badge.svg)][actions]
11
+ [![Codacy Badge](https://api.codacy.com/project/badge/Grade/f30e3ff5ec304c55a73868cdbf055c67)][codacy]
12
+ [![Codacy Badge](https://api.codacy.com/project/badge/Coverage/f30e3ff5ec304c55a73868cdbf055c67)][codacy]
13
13
  [![Inline docs](http://inch-ci.org/github/dry-rb/dry-validation.svg?branch=master)][inchpages]
14
14
 
15
15
  ## Links
16
16
 
17
- * [User documentation](https://dry-rb.org/gems/dry-validation)
17
+ * [User documentation](http://dry-rb.org/gems/dry-validation)
18
18
  * [API documentation](http://rubydoc.info/gems/dry-validation)
19
- * [Guidelines for contributing](CONTRIBUTING.md)
20
19
 
21
20
  ## Supported Ruby versions
22
21
 
23
- This library officially supports following Ruby versions:
22
+ This library officially supports the following Ruby versions:
24
23
 
25
24
  * MRI >= `2.4`
26
25
  * jruby >= `9.2`
@@ -98,7 +98,7 @@ module Dry
98
98
  rule_result = rule.(self, result)
99
99
 
100
100
  rule_result.failures.each do |failure|
101
- result.add_error(message_resolver[failure])
101
+ result.add_error(message_resolver.(**failure))
102
102
  end
103
103
  end
104
104
  end
@@ -57,8 +57,8 @@ module Dry
57
57
  # @see https://dry-rb.org/gems/dry-schema/params/
58
58
  #
59
59
  # @api public
60
- def params(external_schema = nil, &block)
61
- define(:Params, external_schema, &block)
60
+ def params(*external_schemas, &block)
61
+ define(:Params, external_schemas, &block)
62
62
  end
63
63
 
64
64
  # Define a JSON schema for your contract
@@ -69,8 +69,8 @@ module Dry
69
69
  # @see https://dry-rb.org/gems/dry-schema/json/
70
70
  #
71
71
  # @api public
72
- def json(external_schema = nil, &block)
73
- define(:JSON, external_schema, &block)
72
+ def json(*external_schemas, &block)
73
+ define(:JSON, external_schemas, &block)
74
74
  end
75
75
 
76
76
  # Define a plain schema for your contract
@@ -81,8 +81,8 @@ module Dry
81
81
  # @see https://dry-rb.org/gems/dry-schema/
82
82
  #
83
83
  # @api public
84
- def schema(external_schema = nil, &block)
85
- define(:schema, external_schema, &block)
84
+ def schema(*external_schemas, &block)
85
+ define(:schema, external_schemas, &block)
86
86
  end
87
87
 
88
88
  # Define a rule for your contract
@@ -123,7 +123,7 @@ module Dry
123
123
  #
124
124
  # @api public
125
125
  def build(options = EMPTY_HASH, &block)
126
- Class.new(self, &block).new(options)
126
+ Class.new(self, &block).new(**options)
127
127
  end
128
128
 
129
129
  # @api private
@@ -162,7 +162,7 @@ module Dry
162
162
  .map { |key|
163
163
  [key, Schema::Path[key]]
164
164
  }
165
- .map { |(key, path)|
165
+ .flat_map { |(key, path)|
166
166
  if (last = path.last).is_a?(Array)
167
167
  last.map { |last_key|
168
168
  path_key = [*path.to_a[0..-2], last_key]
@@ -172,7 +172,6 @@ module Dry
172
172
  [[key, path]]
173
173
  end
174
174
  }
175
- .flatten(1)
176
175
  .reject { |(_, path)|
177
176
  valid_paths.any? { |valid_path| valid_path.include?(path) }
178
177
  }
@@ -197,8 +196,8 @@ module Dry
197
196
  end
198
197
 
199
198
  # @api private
200
- def define(method_name, external_schema, &block)
201
- return __schema__ if external_schema.nil? && block.nil?
199
+ def define(method_name, external_schemas, &block)
200
+ return __schema__ if external_schemas.empty? && block.nil?
202
201
 
203
202
  unless __schema__.nil?
204
203
  raise ::Dry::Validation::DuplicateSchemaError, 'Schema has already been defined'
@@ -206,15 +205,15 @@ module Dry
206
205
 
207
206
  schema_opts = core_schema_opts
208
207
 
209
- schema_opts.update(parent: external_schema) if external_schema
208
+ schema_opts.update(parent: external_schemas) if external_schemas.any?
210
209
 
211
210
  case method_name
212
211
  when :schema
213
- @__schema__ = Schema.define(schema_opts, &block)
212
+ @__schema__ = Schema.define(**schema_opts, &block)
214
213
  when :Params
215
- @__schema__ = Schema.Params(schema_opts, &block)
214
+ @__schema__ = Schema.Params(**schema_opts, &block)
216
215
  when :JSON
217
- @__schema__ = Schema.JSON(schema_opts, &block)
216
+ @__schema__ = Schema.JSON(**schema_opts, &block)
218
217
  end
219
218
  end
220
219
  end
@@ -63,19 +63,19 @@ module Dry
63
63
  # Initialize a new evaluator
64
64
  #
65
65
  # @api private
66
- def initialize(contract, options, &block)
67
- super(contract, options)
66
+ def initialize(contract, **options, &block)
67
+ super(contract, **options)
68
68
 
69
69
  @_options = options
70
70
 
71
71
  if block
72
72
  exec_opts = block_options.map { |key, value| [key, _options[value]] }.to_h
73
- instance_exec(exec_opts, &block)
73
+ instance_exec(**exec_opts, &block)
74
74
  end
75
75
 
76
76
  macros.each do |args|
77
77
  macro = macro(*args.flatten(1))
78
- instance_exec(macro.extract_block_options(_options.merge(macro: macro)), &macro.block)
78
+ instance_exec(**macro.extract_block_options(_options.merge(macro: macro)), &macro.block)
79
79
  end
80
80
  end
81
81
 
@@ -117,7 +117,7 @@ module Dry
117
117
 
118
118
  # @api private
119
119
  def with(new_opts, &block)
120
- self.class.new(_contract, _options.merge(new_opts), &block)
120
+ self.class.new(_contract, **_options, **new_opts, &block)
121
121
  end
122
122
 
123
123
  # Return default (first) key name
@@ -192,6 +192,7 @@ module Dry
192
192
  super
193
193
  end
194
194
  end
195
+ ruby2_keywords(:method_missing) if respond_to?(:ruby2_keywords, true)
195
196
  end
196
197
  end
197
198
  end
@@ -12,7 +12,7 @@ module Dry
12
12
  #
13
13
  # @see Dry::Validation::Contract
14
14
  WHITELIST = %i[
15
- filled? gt? gteq? included_in? includes? inclusion? is? lt?
15
+ filled? format? gt? gteq? included_in? includes? inclusion? is? lt?
16
16
  lteq? max_size? min_size? not_eql? odd? respond_to? size? true?
17
17
  uuid_v4?
18
18
  ].freeze
@@ -45,6 +45,13 @@ module Dry
45
45
  # @example
46
46
  # failure(:taken)
47
47
  #
48
+ # @overload failure(meta_hash)
49
+ # Use meta_hash[:text] as a message (either explicitely or as an identifier),
50
+ # setting the rest of the hash as error meta attribute
51
+ # @param meta [Hash] The hash containing the message as value for the :text key
52
+ # @example
53
+ # failure({text: :invalid, key: value})
54
+ #
48
55
  # @see Evaluator#key
49
56
  # @see Evaluator#base
50
57
  #
@@ -52,7 +52,7 @@ module Dry
52
52
  #
53
53
  # @api public
54
54
  def evaluate(**opts)
55
- evaluated_text, rest = text.(opts)
55
+ evaluated_text, rest = text.(**opts)
56
56
  Message.new(evaluated_text, path: path, meta: rest.merge(meta))
57
57
  end
58
58
  end
@@ -41,7 +41,7 @@ module Dry
41
41
  return self if new_options.empty? && other.eql?(messages)
42
42
 
43
43
  self.class.new(
44
- (other + select { |err| err.is_a?(Message) }).uniq,
44
+ other | select { |err| err.is_a?(Message) },
45
45
  options.merge(source: source_messages, **new_options)
46
46
  ).freeze
47
47
  end
@@ -54,6 +54,7 @@ module Dry
54
54
  #
55
55
  # @api private
56
56
  def add(message)
57
+ @empty = nil
57
58
  source_messages << message
58
59
  messages << message
59
60
  initialize_placeholders!
@@ -114,7 +115,7 @@ module Dry
114
115
  # rubocop:disable Metrics/AbcSize
115
116
  # rubocop:disable Metrics/PerceivedComplexity
116
117
  def initialize_placeholders!
117
- @placeholders = unique_paths.each_with_object(EMPTY_HASH.dup) { |path, hash|
118
+ @placeholders = unique_paths.sort_by(&:size).each_with_object(EMPTY_HASH.dup) { |path, hash|
118
119
  curr_idx = 0
119
120
  last_idx = path.size - 1
120
121
  node = hash
@@ -22,6 +22,8 @@ module Dry
22
22
  # Resolve Message object from provided args and path
23
23
  #
24
24
  # This is used internally by contracts when rules are applied
25
+ # If message argument is a Hash, then it MUST have a :text key,
26
+ # which value will be used as the message value
25
27
  #
26
28
  # @return [Message, Message::Localized]
27
29
  #
@@ -34,7 +36,12 @@ module Dry
34
36
  Message[message, path, meta]
35
37
  when Hash
36
38
  meta = message.dup
37
- text = meta.delete(:text)
39
+ text = meta.delete(:text) { |key|
40
+ raise ArgumentError, <<~STR
41
+ +message+ Hash must contain :#{key} key (#{message.inspect} given)
42
+ STR
43
+ }
44
+
38
45
  call(message: text, tokens: tokens, path: path, meta: meta)
39
46
  else
40
47
  raise ArgumentError, <<~STR
@@ -68,11 +75,31 @@ module Dry
68
75
  STR
69
76
  end
70
77
 
71
- text = template.(template.data(tokens))
78
+ parsed_tokens = parse_tokens(tokens)
79
+ text = template.(template.data(parsed_tokens))
72
80
 
73
81
  [full ? "#{messages.rule(keys.last, msg_opts)} #{text}" : text, meta]
74
82
  end
75
83
  # rubocop:enable Metrics/AbcSize
84
+
85
+ private
86
+
87
+ def parse_tokens(tokens)
88
+ Hash[
89
+ tokens.map do |key, token|
90
+ [key, parse_token(token)]
91
+ end
92
+ ]
93
+ end
94
+
95
+ def parse_token(token)
96
+ case token
97
+ when Array
98
+ token.join(', ')
99
+ else
100
+ token
101
+ end
102
+ end
76
103
  end
77
104
  end
78
105
  end
@@ -179,6 +179,22 @@ module Dry
179
179
  super
180
180
  end
181
181
 
182
+ if RUBY_VERSION >= '2.7'
183
+ # Pattern matching
184
+ #
185
+ # @api private
186
+ def deconstruct_keys(keys)
187
+ values.deconstruct_keys(keys)
188
+ end
189
+
190
+ # Pattern matching
191
+ #
192
+ # @api private
193
+ def deconstruct
194
+ [values, context.each.to_h]
195
+ end
196
+ end
197
+
182
198
  private
183
199
 
184
200
  # @api private
@@ -30,7 +30,7 @@ module Dry
30
30
  class Hash < Key
31
31
  # @api private
32
32
  def to_dot_notation
33
- [name].product(members.map(&:to_dot_notation).flatten(1)).map { |e| e.join(DOT) }
33
+ [name].product(members.flat_map(&:to_dot_notation)).map { |e| e.join(DOT) }
34
34
  end
35
35
  end
36
36
  end
@@ -89,6 +89,7 @@ module Dry
89
89
  super
90
90
  end
91
91
  end
92
+ ruby2_keywords(:method_missing) if respond_to?(:ruby2_keywords, true)
92
93
  end
93
94
  end
94
95
  end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Dry
4
4
  module Validation
5
- VERSION = '1.3.1'
5
+ VERSION = '1.4.1'
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.3.1
4
+ version: 1.4.1
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-08-16 00:00:00.000000000 Z
11
+ date: 2020-01-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: concurrent-ruby
@@ -95,7 +95,7 @@ dependencies:
95
95
  version: '1.0'
96
96
  - - ">="
97
97
  - !ruby/object:Gem::Version
98
- version: 1.3.1
98
+ version: 1.4.3
99
99
  type: :runtime
100
100
  prerelease: false
101
101
  version_requirements: !ruby/object:Gem::Requirement
@@ -105,7 +105,7 @@ dependencies:
105
105
  version: '1.0'
106
106
  - - ">="
107
107
  - !ruby/object:Gem::Version
108
- version: 1.3.1
108
+ version: 1.4.3
109
109
  - !ruby/object:Gem::Dependency
110
110
  name: bundler
111
111
  requirement: !ruby/object:Gem::Requirement
@@ -200,7 +200,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
200
200
  - !ruby/object:Gem::Version
201
201
  version: '0'
202
202
  requirements: []
203
- rubygems_version: 3.0.3
203
+ rubygems_version: 3.1.2
204
204
  signing_key:
205
205
  specification_version: 4
206
206
  summary: Validation library