dry-validation 1.2.1 → 1.3.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: c16031447235e4a737bb2fd2c406017b6bc082415ade63f8fe4d364c9b720be1
4
- data.tar.gz: 10d0e6aaddeb0df982894c95e305eef3c6f05bbc3ac2cf229b9926f0acfe791f
3
+ metadata.gz: 46364c3c3ebe05837ab81f0406c1bbd6d0ad4f6359017dedc92f25627aae6bd4
4
+ data.tar.gz: eaefd86e8b6ce0eebd7140c8eae37f3b40eb7d85400b97cd3e7fa6cf58b8c8af
5
5
  SHA512:
6
- metadata.gz: a0363485af3db4081d46ea89ad5f1918c557d2b38b9ec357640bb1feb1eec3c6fbc4cd5a8ff5650f830bcf3440abe7d4f7e570377fad8fc803fd0ccc9d91ce49
7
- data.tar.gz: 7a89b67fe79eb7a8e3a63b939b33af67a13b89ad005d2d334c3d3db626bd960e19a957b25508239358bcb634deaeb116e91fab1878b393c3b106f6a028f96a8d
6
+ metadata.gz: ea2bf0a1bf9d3c451f6d073a6edfcbd6e2b1e33c67a6fa94d3f0c0005155d81fccc6159c9a575dc1b2736a6a00abb094fbd8bb556007f7e980b278404b9e4ed1
7
+ data.tar.gz: be84af9fc46f27e28afc97d7b58dbdf6f031c978d9d94e75a4d2e795a9e0bace3e0db42261259559618291b4dbf637afd8ddeea4ad9f620271dbc7033c5fd212
@@ -1,3 +1,19 @@
1
+ # v1.3.0 2019-08-14
2
+
3
+ ### Added
4
+
5
+ * Support for setting an external schema (that can be extended too) (fixed #574) (@solnic)
6
+
7
+ ### Fixed
8
+
9
+ * Using a hash spec to define rule keys with more than one value is properly handled by rule guard now (fixed #576) (@solnic)
10
+
11
+ ### Changed
12
+
13
+ * `values` within rules uses `Hash#fetch_values` internally now, which improves performance (@esparta)
14
+
15
+ [Compare v1.2.1...v1.3.0](https://github.com/dry-rb/dry-validation/compare/v1.2.1...v1.3.0)
16
+
1
17
  # v1.2.1 2019-07-16
2
18
 
3
19
  ### Fixed
@@ -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
@@ -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
@@ -192,16 +192,22 @@ module Dry
192
192
  end
193
193
 
194
194
  # @api private
195
- def schema_opts
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 defined?(@__schema__)
200
+ def define(method_name, external_schema, &block)
201
+ return __schema__ if 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)
@@ -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
- path = Schema::Path[key]
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
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Dry
4
4
  module Validation
5
- VERSION = '1.2.1'
5
+ VERSION = '1.3.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.2.1
4
+ version: 1.3.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-07-16 00:00:00.000000000 Z
11
+ date: 2019-08-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: concurrent-ruby