dry-validation 1.0.0.beta2 → 1.0.0.rc1

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: 705706b2bc77cae54a87647ce3d85d685aa2fe619e40950533e1d96a513d1ca0
4
- data.tar.gz: a0ad40a08b44475bafa4510707870603517145602e5567bb10cd39f98a1895af
3
+ metadata.gz: 97e07b84e6d69b8ff4c887a633dba3862ebb44219d1aa36b0a4394e08761b8e0
4
+ data.tar.gz: 18a796e79f03f85f288307038a29c913a0340e6a1c77e2b6b138b96397b0aff9
5
5
  SHA512:
6
- metadata.gz: 7f2a5347e9b057d76cf9d99c0c313c7437133c1884e750a6c54d095e00f66eb600d7d79a033c2ba561a32c92b783c3153cbd8d88e62c715cd75e9d238f2a034a
7
- data.tar.gz: 0a6312efffe80b0156da48da466a9c4384e0b636ef34a93587097592e72a9a2d72f9503e8aa6e556399fc8ad9a18399cc1e5fea2ddb9e66b6268509fcb796d4a
6
+ metadata.gz: f3f8fd949d909e6823c5e5aced8fb055eec20263f06247b98e066187efc57a1e987b0c016f472b99693a79191aafc64c63b0ec68d64ef8148b2f4901a861335f
7
+ data.tar.gz: c146b67aaab383aa39f908b3e487fa249f7eeb9d13b7e2c165a2c845cb5450775c52f3fc3811a900d6a1c2a2e36fc79fc002b2c04a843d0fa1e635888abb7e6a
data/CHANGELOG.md CHANGED
@@ -1,3 +1,22 @@
1
+ # v1.0.0.rc1 2019-04-26
2
+
3
+ ### Added
4
+
5
+ * `:hints` extension is back (solnic)
6
+ * `Result` objects have access to the context object which is shared between rules (flash-gordon)
7
+
8
+ ### Fixed
9
+
10
+ * Multiple hint messages no longer crash message set (flash-gordon)
11
+ * `Contract#inspect` no longer crashes (solnic)
12
+
13
+ ### Changed
14
+
15
+ * Dependency on `dry-schema` was bumped to `~> 0.6` - this pulls in `dry-types 1.0.0` and `dry-logic 1.0.0` (solnic)
16
+ * Dependency on `dry-initializer` was bumped to `~> 3.0` (solnic)
17
+
18
+ [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)
19
+
1
20
  # v1.0.0.beta2 2019-04-04
2
21
 
3
22
  ### Added
@@ -12,5 +12,9 @@ module Dry
12
12
  register_extension(:monads) do
13
13
  require 'dry/validation/extensions/monads'
14
14
  end
15
+
16
+ register_extension(:hints) do
17
+ require 'dry/validation/extensions/hints'
18
+ end
15
19
  end
16
20
  end
@@ -25,7 +25,7 @@ module Dry
25
25
  # your rules.
26
26
  #
27
27
  # @example
28
- # class NewUser < Dry::Validation::Contract
28
+ # class NewUserContract < Dry::Validation::Contract
29
29
  # params do
30
30
  # required(:email).filled(:string)
31
31
  # required(:age).filled(:integer)
@@ -35,11 +35,11 @@ module Dry
35
35
  # end
36
36
  #
37
37
  # rule(:password) do
38
- # failure('is required') if values[:login] && !values[:password]
38
+ # key.failure('is required') if values[:login] && !values[:password]
39
39
  # end
40
40
  #
41
41
  # rule(:age) do
42
- # failure('must be greater or equal 18') if values[:age] < 18
42
+ # key.failure('must be greater or equal 18') if values[:age] < 18
43
43
  # end
44
44
  # end
45
45
  #
@@ -48,7 +48,7 @@ module Dry
48
48
  #
49
49
  # @api public
50
50
  class Contract
51
- include Dry::Equalizer(:schema, :rules, :messages)
51
+ include Dry::Equalizer(:schema, :rules, :messages, inspect: false)
52
52
 
53
53
  extend Dry::Initializer
54
54
  extend ClassInterface
@@ -86,18 +86,25 @@ module Dry
86
86
  #
87
87
  # @api public
88
88
  def call(input)
89
- Result.new(schema.(input), locale: locale) do |result|
90
- context = Concurrent::Map.new
91
-
89
+ Result.new(schema.(input), Concurrent::Map.new, locale: locale) do |result|
92
90
  rules.each do |rule|
93
91
  next if rule.keys.any? { |key| result.error?(key) }
94
92
 
95
- rule.(self, result, context).failures.each do |failure|
93
+ rule.(self, result, result.context).failures.each do |failure|
96
94
  result.add_error(message_resolver[failure])
97
95
  end
98
96
  end
99
97
  end
100
98
  end
99
+
100
+ # Return a nice string representation
101
+ #
102
+ # @return [String]
103
+ #
104
+ # @api public
105
+ def inspect
106
+ %(#<#{self.class} schema=#{schema.inspect} rules=#{rules.inspect}>)
107
+ end
101
108
  end
102
109
  end
103
110
  end
@@ -0,0 +1,61 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'dry/monads/result'
4
+
5
+ module Dry
6
+ module Validation
7
+ # Hints extension for contract results
8
+ #
9
+ # @example
10
+ # Dry::Validation.load_extensions(:hints)
11
+ #
12
+ # contract = Dry::Validation::Contract.build do
13
+ # schema do
14
+ # required(:name).filled(:string, min_size?: 2..4)
15
+ # end
16
+ # end
17
+ #
18
+ # contract.call(name: "fo").hints
19
+ # # {:name=>["size must be within 2 - 4"]}
20
+ #
21
+ # contract.call(name: "").messages
22
+ # # {:name=>["must be filled", "size must be within 2 - 4"]}
23
+ #
24
+ # @api public
25
+ module Hints
26
+ module ResultExtensions
27
+ # Return error messages excluding hints
28
+ #
29
+ # @return [MessageSet]
30
+ #
31
+ # @api public
32
+ def errors(new_opts = EMPTY_HASH)
33
+ opts = new_opts.merge(hints: false)
34
+ @errors.with(schema_errors(opts), opts)
35
+ end
36
+
37
+ # Return errors and hints
38
+ #
39
+ # @return [MessageSet]
40
+ #
41
+ # @api public
42
+ def messages(new_opts = EMPTY_HASH)
43
+ errors.with(hints.to_a, options.merge(**new_opts))
44
+ end
45
+
46
+ # Return hint messages
47
+ #
48
+ # @return [MessageSet]
49
+ #
50
+ # @api public
51
+ def hints(new_opts = EMPTY_HASH)
52
+ values.hints(new_opts)
53
+ end
54
+ end
55
+
56
+ Dry::Schema.load_extensions(:hints)
57
+
58
+ Result.prepend(ResultExtensions)
59
+ end
60
+ end
61
+ end
@@ -25,7 +25,7 @@ module Dry
25
25
  # @api private
26
26
  def initialize(messages, options = EMPTY_HASH)
27
27
  @locale = options.fetch(:locale, :en)
28
- @source_messages = options.fetch(:source, messages.dup)
28
+ @source_messages = options.fetch(:source) { messages.dup }
29
29
  super
30
30
  end
31
31
 
@@ -38,7 +38,7 @@ module Dry
38
38
  return self if new_options.empty?
39
39
 
40
40
  self.class.new(
41
- other + select { |err| err.is_a?(Message) },
41
+ (other + select { |err| err.is_a?(Message) }).uniq,
42
42
  options.merge(source: source_messages, **new_options)
43
43
  ).freeze
44
44
  end
@@ -101,7 +101,7 @@ module Dry
101
101
  def messages_map
102
102
  @messages_map ||= reduce(placeholders) { |hash, msg|
103
103
  node = msg.path.reduce(hash) { |a, e| a.is_a?(Hash) ? a[e] : a.last[e] }
104
- (node.size > 1 ? node[0] : node) << msg.dump
104
+ (node[0].is_a?(::Array) ? node[0] : node) << msg.dump
105
105
  hash
106
106
  }
107
107
  end
@@ -1,5 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require 'concurrent/map'
3
4
  require 'dry/equalizer'
4
5
 
5
6
  require 'dry/validation/constants'
@@ -11,14 +12,14 @@ module Dry
11
12
  #
12
13
  # @api public
13
14
  class Result
14
- include Dry::Equalizer(:values, :errors)
15
+ include Dry::Equalizer(:values, :context, :errors)
15
16
 
16
17
  # Build a new result
17
18
  #
18
19
  # @param [Dry::Schema::Result]
19
20
  #
20
21
  # @api private
21
- def self.new(values, options = EMPTY_HASH)
22
+ def self.new(values, context = ::Concurrent::Map.new, options = EMPTY_HASH)
22
23
  result = super
23
24
  yield(result) if block_given?
24
25
  result.freeze
@@ -29,6 +30,11 @@ module Dry
29
30
  # @api private
30
31
  attr_reader :values
31
32
 
33
+ # @!attribute [r] context
34
+ # @return [Concurrent::Map]
35
+ # @api public
36
+ attr_reader :context
37
+
32
38
  # @!attribute [r] options
33
39
  # @return [Hash]
34
40
  # @api private
@@ -37,8 +43,9 @@ module Dry
37
43
  # Initialize a new result
38
44
  #
39
45
  # @api private
40
- def initialize(values, options)
46
+ def initialize(values, context, options)
41
47
  @values = values
48
+ @context = context
42
49
  @options = options
43
50
  @errors = initialize_errors
44
51
  end
@@ -51,7 +58,6 @@ module Dry
51
58
  def errors(new_options = EMPTY_HASH)
52
59
  new_options.empty? ? @errors : @errors.with(schema_errors(new_options), new_options)
53
60
  end
54
- alias_method :messages, :errors
55
61
 
56
62
  # Check if result is successful
57
63
  #
@@ -82,7 +88,7 @@ module Dry
82
88
  #
83
89
  # @api private
84
90
  def add_error(error)
85
- errors.add(error)
91
+ @errors.add(error)
86
92
  self
87
93
  end
88
94
 
@@ -119,7 +125,11 @@ module Dry
119
125
  #
120
126
  # @api public
121
127
  def inspect
122
- "#<#{self.class}#{to_h.inspect} errors=#{errors.to_h.inspect}>"
128
+ if context.empty?
129
+ "#<#{self.class}#{to_h.inspect} errors=#{errors.to_h.inspect}>"
130
+ else
131
+ "#<#{self.class}#{to_h.inspect} errors=#{errors.to_h.inspect} context=#{context.each.to_h.inspect}>"
132
+ end
123
133
  end
124
134
 
125
135
  # Freeze result and its error set
@@ -9,7 +9,7 @@ module Dry
9
9
  #
10
10
  # @api private
11
11
  class Rule
12
- include Dry::Equalizer(:name, :block)
12
+ include Dry::Equalizer(:keys, :block, inspect: false)
13
13
 
14
14
  extend Dry::Initializer
15
15
 
@@ -33,6 +33,15 @@ module Dry
33
33
  def call(contract, result, context)
34
34
  Evaluator.new(contract, values: result, keys: keys, _context: context, &block)
35
35
  end
36
+
37
+ # Return a nice string representation
38
+ #
39
+ # @return [String]
40
+ #
41
+ # @api public
42
+ def inspect
43
+ %(#<#{self.class} keys=#{keys.inspect}>)
44
+ end
36
45
  end
37
46
  end
38
47
  end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Dry
4
4
  module Validation
5
- VERSION = '1.0.0.beta2'
5
+ VERSION = '1.0.0.rc1'
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.0.0.beta2
4
+ version: 1.0.0.rc1
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-04-04 00:00:00.000000000 Z
11
+ date: 2019-04-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: concurrent-ruby
@@ -30,20 +30,14 @@ dependencies:
30
30
  requirements:
31
31
  - - "~>"
32
32
  - !ruby/object:Gem::Version
33
- version: '0.2'
34
- - - ">="
35
- - !ruby/object:Gem::Version
36
- version: 0.2.1
33
+ version: '0.4'
37
34
  type: :runtime
38
35
  prerelease: false
39
36
  version_requirements: !ruby/object:Gem::Requirement
40
37
  requirements:
41
38
  - - "~>"
42
39
  - !ruby/object:Gem::Version
43
- version: '0.2'
44
- - - ">="
45
- - !ruby/object:Gem::Version
46
- version: 0.2.1
40
+ version: '0.4'
47
41
  - !ruby/object:Gem::Dependency
48
42
  name: dry-equalizer
49
43
  requirement: !ruby/object:Gem::Requirement
@@ -64,34 +58,34 @@ dependencies:
64
58
  requirements:
65
59
  - - "~>"
66
60
  - !ruby/object:Gem::Version
67
- version: '2.5'
61
+ version: '3.0'
68
62
  type: :runtime
69
63
  prerelease: false
70
64
  version_requirements: !ruby/object:Gem::Requirement
71
65
  requirements:
72
66
  - - "~>"
73
67
  - !ruby/object:Gem::Version
74
- version: '2.5'
68
+ version: '3.0'
75
69
  - !ruby/object:Gem::Dependency
76
70
  name: dry-schema
77
71
  requirement: !ruby/object:Gem::Requirement
78
72
  requirements:
79
73
  - - ">="
80
74
  - !ruby/object:Gem::Version
81
- version: '0.5'
75
+ version: '0.6'
82
76
  - - "~>"
83
77
  - !ruby/object:Gem::Version
84
- version: '0.5'
78
+ version: '0.6'
85
79
  type: :runtime
86
80
  prerelease: false
87
81
  version_requirements: !ruby/object:Gem::Requirement
88
82
  requirements:
89
83
  - - ">="
90
84
  - !ruby/object:Gem::Version
91
- version: '0.5'
85
+ version: '0.6'
92
86
  - - "~>"
93
87
  - !ruby/object:Gem::Version
94
- version: '0.5'
88
+ version: '0.6'
95
89
  - !ruby/object:Gem::Dependency
96
90
  name: bundler
97
91
  requirement: !ruby/object:Gem::Requirement
@@ -151,6 +145,7 @@ files:
151
145
  - lib/dry/validation/contract.rb
152
146
  - lib/dry/validation/contract/class_interface.rb
153
147
  - lib/dry/validation/evaluator.rb
148
+ - lib/dry/validation/extensions/hints.rb
154
149
  - lib/dry/validation/extensions/monads.rb
155
150
  - lib/dry/validation/message.rb
156
151
  - lib/dry/validation/message_set.rb