dry-validation 1.2.0 → 1.3.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 +4 -4
- data/CHANGELOG.md +35 -0
- data/lib/dry/validation/constants.rb +1 -0
- data/lib/dry/validation/contract.rb +7 -1
- data/lib/dry/validation/contract/class_interface.rb +19 -13
- data/lib/dry/validation/result.rb +16 -0
- data/lib/dry/validation/rule.rb +7 -5
- data/lib/dry/validation/schema_ext.rb +12 -0
- data/lib/dry/validation/values.rb +2 -4
- data/lib/dry/validation/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f61fe3205cc418d53bd0f2217bdd49a0e49119a69cae87ecf24e76a89567f845
|
4
|
+
data.tar.gz: 4ca306db1fd196c3253a78a6376fe0d4c197c5c8d625636bc37038b9caa3278b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d0c6a082c4a7077bd428e1bbe2f8ec4677950ea0f073d15cb955568307e2924b75e9d4bfb0f023b65739df7d061569e1aa8a3941968c263d68f4fd3fe12a61d5
|
7
|
+
data.tar.gz: 50fa2999ebe80ce40fe244b10bac6cf3644f4dd7e7825913726e8cc2587a901f0d2d164d1c29e17b55f613bd7258dc13d672a5787334f90afc4059d0480312b9
|
data/CHANGELOG.md
CHANGED
@@ -1,3 +1,38 @@
|
|
1
|
+
# v1.3.1 2019-08-16
|
2
|
+
|
3
|
+
### Changed
|
4
|
+
|
5
|
+
* You can now set an external schema without providing a block (alassek)
|
6
|
+
|
7
|
+
[Compare v1.3.0...v1.3.1](https://github.com/dry-rb/dry-validation/compare/v1.3.0...v1.3.1)
|
8
|
+
|
9
|
+
# v1.3.0 2019-08-14
|
10
|
+
|
11
|
+
### Added
|
12
|
+
|
13
|
+
* Support for setting an external schema (that can be extended too) (fixed #574) (@solnic)
|
14
|
+
|
15
|
+
### Fixed
|
16
|
+
|
17
|
+
* Using a hash spec to define rule keys with more than one value is properly handled by rule guard now (fixed #576) (@solnic)
|
18
|
+
|
19
|
+
### Changed
|
20
|
+
|
21
|
+
* `values` within rules uses `Hash#fetch_values` internally now, which improves performance (@esparta)
|
22
|
+
|
23
|
+
[Compare v1.2.1...v1.3.0](https://github.com/dry-rb/dry-validation/compare/v1.2.1...v1.3.0)
|
24
|
+
|
25
|
+
# v1.2.1 2019-07-16
|
26
|
+
|
27
|
+
### Fixed
|
28
|
+
|
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)
|
33
|
+
|
34
|
+
[Compare v1.2.0...v1.2.1](https://github.com/dry-rb/dry-validation/compare/v1.2.0...v1.2.1)
|
35
|
+
|
1
36
|
# v1.2.0 2019-07-08
|
2
37
|
|
3
38
|
### Added
|
@@ -119,6 +119,10 @@ module Dry
|
|
119
119
|
def error?(result, spec)
|
120
120
|
path = Schema::Path[spec]
|
121
121
|
|
122
|
+
if path.multi_value?
|
123
|
+
return path.expand.any? { |nested_path| error?(result, nested_path) }
|
124
|
+
end
|
125
|
+
|
122
126
|
return true if result.error?(path)
|
123
127
|
|
124
128
|
path
|
@@ -128,7 +132,9 @@ module Dry
|
|
128
132
|
|
129
133
|
return false unless result.error?(curr_path)
|
130
134
|
|
131
|
-
result.errors.any? { |err|
|
135
|
+
result.errors.any? { |err|
|
136
|
+
(other = Schema::Path[err.path]).same_root?(curr_path) && other == curr_path
|
137
|
+
}
|
132
138
|
}
|
133
139
|
end
|
134
140
|
|
@@ -53,36 +53,36 @@ module Dry
|
|
53
53
|
#
|
54
54
|
# This type of schema is suitable for HTTP parameters
|
55
55
|
#
|
56
|
-
# @return [Dry::Schema::Params]
|
56
|
+
# @return [Dry::Schema::Params,NilClass]
|
57
57
|
# @see https://dry-rb.org/gems/dry-schema/params/
|
58
58
|
#
|
59
59
|
# @api public
|
60
|
-
def params(&block)
|
61
|
-
define(:Params, &block)
|
60
|
+
def params(external_schema = nil, &block)
|
61
|
+
define(:Params, external_schema, &block)
|
62
62
|
end
|
63
63
|
|
64
64
|
# Define a JSON schema for your contract
|
65
65
|
#
|
66
66
|
# This type of schema is suitable for JSON data
|
67
67
|
#
|
68
|
-
# @return [Dry::Schema::JSON]
|
68
|
+
# @return [Dry::Schema::JSON,NilClass]
|
69
69
|
# @see https://dry-rb.org/gems/dry-schema/json/
|
70
70
|
#
|
71
71
|
# @api public
|
72
|
-
def json(&block)
|
73
|
-
define(:JSON, &block)
|
72
|
+
def json(external_schema = nil, &block)
|
73
|
+
define(:JSON, external_schema, &block)
|
74
74
|
end
|
75
75
|
|
76
76
|
# Define a plain schema for your contract
|
77
77
|
#
|
78
78
|
# This type of schema does not offer coercion out of the box
|
79
79
|
#
|
80
|
-
# @return [Dry::Schema::Processor]
|
80
|
+
# @return [Dry::Schema::Processor,NilClass]
|
81
81
|
# @see https://dry-rb.org/gems/dry-schema/
|
82
82
|
#
|
83
83
|
# @api public
|
84
|
-
def schema(&block)
|
85
|
-
define(:schema, &block)
|
84
|
+
def schema(external_schema = nil, &block)
|
85
|
+
define(:schema, external_schema, &block)
|
86
86
|
end
|
87
87
|
|
88
88
|
# Define a rule for your contract
|
@@ -101,7 +101,7 @@ module Dry
|
|
101
101
|
#
|
102
102
|
# @api public
|
103
103
|
def rule(*keys, &block)
|
104
|
-
ensure_valid_keys(*keys)
|
104
|
+
ensure_valid_keys(*keys) if __schema__
|
105
105
|
|
106
106
|
Rule.new(keys: keys, block: block).tap do |rule|
|
107
107
|
rules << rule
|
@@ -192,16 +192,22 @@ module Dry
|
|
192
192
|
end
|
193
193
|
|
194
194
|
# @api private
|
195
|
-
def
|
195
|
+
def core_schema_opts
|
196
196
|
{ parent: superclass&.__schema__, config: config }
|
197
197
|
end
|
198
198
|
|
199
199
|
# @api private
|
200
|
-
def define(method_name, &block)
|
201
|
-
if
|
200
|
+
def define(method_name, external_schema, &block)
|
201
|
+
return __schema__ if external_schema.nil? && block.nil?
|
202
|
+
|
203
|
+
unless __schema__.nil?
|
202
204
|
raise ::Dry::Validation::DuplicateSchemaError, 'Schema has already been defined'
|
203
205
|
end
|
204
206
|
|
207
|
+
schema_opts = core_schema_opts
|
208
|
+
|
209
|
+
schema_opts.update(parent: external_schema) if external_schema
|
210
|
+
|
205
211
|
case method_name
|
206
212
|
when :schema
|
207
213
|
@__schema__ = Schema.define(schema_opts, &block)
|
@@ -106,6 +106,22 @@ module Dry
|
|
106
106
|
schema_result.error?(key)
|
107
107
|
end
|
108
108
|
|
109
|
+
# Check if there's any error for the provided key
|
110
|
+
#
|
111
|
+
# This does not consider errors from the nested values
|
112
|
+
#
|
113
|
+
# @api private
|
114
|
+
def base_error?(key)
|
115
|
+
schema_result.errors.any? { |error|
|
116
|
+
key_path = Schema::Path[key]
|
117
|
+
err_path = Schema::Path[error.path]
|
118
|
+
|
119
|
+
return false unless key_path.same_root?(err_path)
|
120
|
+
|
121
|
+
key_path == err_path
|
122
|
+
}
|
123
|
+
end
|
124
|
+
|
109
125
|
# Add a new error for the provided key
|
110
126
|
#
|
111
127
|
# @api private
|
data/lib/dry/validation/rule.rb
CHANGED
@@ -82,14 +82,16 @@ module Dry
|
|
82
82
|
@keys = []
|
83
83
|
|
84
84
|
@block = proc do
|
85
|
-
(
|
86
|
-
|
85
|
+
unless result.base_error?(root) || !values.key?(root)
|
86
|
+
values[root].each_with_index do |_, idx|
|
87
|
+
path = [*Schema::Path[root].to_a, idx]
|
87
88
|
|
88
|
-
|
89
|
+
next if result.error?(path)
|
89
90
|
|
90
|
-
|
91
|
+
evaluator = with(macros: macros, keys: [path], &block)
|
91
92
|
|
92
|
-
|
93
|
+
failures.concat(evaluator.failures)
|
94
|
+
end
|
93
95
|
end
|
94
96
|
end
|
95
97
|
|
@@ -5,6 +5,18 @@ require 'dry/schema/key_map'
|
|
5
5
|
|
6
6
|
module Dry
|
7
7
|
module Schema
|
8
|
+
class Path
|
9
|
+
# @api private
|
10
|
+
def multi_value?
|
11
|
+
last.is_a?(Array)
|
12
|
+
end
|
13
|
+
|
14
|
+
# @api private
|
15
|
+
def expand
|
16
|
+
to_a[0..-2].product(last).map { |spec| self.class[spec] }
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
8
20
|
# @api private
|
9
21
|
#
|
10
22
|
# TODO: this should be moved to dry-schema at some point
|
@@ -45,15 +45,13 @@ module Dry
|
|
45
45
|
|
46
46
|
case (key = args[0])
|
47
47
|
when Symbol, String, Array, Hash
|
48
|
-
|
49
|
-
keys = path.to_a
|
48
|
+
keys = Schema::Path[key].to_a
|
50
49
|
|
51
50
|
return data.dig(*keys) unless keys.last.is_a?(Array)
|
52
51
|
|
53
52
|
last = keys.pop
|
54
53
|
vals = self.class.new(data.dig(*keys))
|
55
|
-
|
56
|
-
last.map { |name| vals[name] }
|
54
|
+
vals.fetch_values(*last) { nil }
|
57
55
|
else
|
58
56
|
raise ArgumentError, '+key+ must be a valid path specification'
|
59
57
|
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.
|
4
|
+
version: 1.3.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-
|
11
|
+
date: 2019-08-16 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: concurrent-ruby
|