dry-validation 1.5.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,211 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "dry/initializer"
4
+ require "dry/core/deprecations"
5
+
6
+ require "dry/validation/constants"
7
+ require "dry/validation/failures"
8
+
9
+ module Dry
10
+ module Validation
11
+ # Evaluator is the execution context for rules
12
+ #
13
+ # Evaluators expose an API for setting failure messages and forward
14
+ # method calls to the contracts, so that you can use your contract
15
+ # methods within rule blocks
16
+ #
17
+ # @api public
18
+ class Evaluator
19
+ extend Dry::Initializer
20
+ extend Dry::Core::Deprecations[:'dry-validation']
21
+
22
+ deprecate :error?, :schema_error?
23
+
24
+ # @!attribute [r] _contract
25
+ # @return [Contract]
26
+ # @api private
27
+ param :_contract
28
+
29
+ # @!attribute [r] result
30
+ # @return [Result]
31
+ # @api private
32
+ option :result
33
+
34
+ # @!attribute [r] keys
35
+ # @return [Array<String, Symbol, Hash>]
36
+ # @api private
37
+ option :keys
38
+
39
+ # @!attribute [r] macros
40
+ # @return [Array<Symbol>]
41
+ # @api private
42
+ option :macros, optional: true, default: proc { EMPTY_ARRAY.dup }
43
+
44
+ # @!attribute [r] _context
45
+ # @return [Concurrent::Map]
46
+ # @api private
47
+ option :_context
48
+
49
+ # @!attribute [r] path
50
+ # @return [Dry::Schema::Path]
51
+ # @api private
52
+ option :path, default: proc { Dry::Schema::Path[(key = keys.first) ? key : ROOT_PATH] }
53
+
54
+ # @!attribute [r] values
55
+ # @return [Object]
56
+ # @api private
57
+ option :values
58
+
59
+ # @!attribute [r] block_options
60
+ # @return [Hash<Symbol=>Symbol>]
61
+ # @api private
62
+ option :block_options, default: proc { EMPTY_HASH }
63
+
64
+ # @return [Hash]
65
+ attr_reader :_options
66
+
67
+ # Initialize a new evaluator
68
+ #
69
+ # @api private
70
+ def initialize(contract, **options, &block)
71
+ super(contract, **options)
72
+
73
+ @_options = options
74
+
75
+ if block
76
+ exec_opts = block_options.map { |key, value| [key, _options[value]] }.to_h
77
+ instance_exec(**exec_opts, &block)
78
+ end
79
+
80
+ macros.each do |args|
81
+ macro = macro(*args.flatten(1))
82
+ instance_exec(**macro.extract_block_options(_options.merge(macro: macro)), &macro.block)
83
+ end
84
+ end
85
+
86
+ # Get `Failures` object for the default or provided path
87
+ #
88
+ # @param [Symbol,String,Hash,Array<Symbol>] path
89
+ #
90
+ # @return [Failures]
91
+ #
92
+ # @see Failures#failure
93
+ #
94
+ # @api public
95
+ def key(path = self.path)
96
+ (@key ||= EMPTY_HASH.dup)[path] ||= Failures.new(path)
97
+ end
98
+
99
+ # Get `Failures` object for base errors
100
+ #
101
+ # @return [Failures]
102
+ #
103
+ # @see Failures#failure
104
+ #
105
+ # @api public
106
+ def base
107
+ @base ||= Failures.new
108
+ end
109
+
110
+ # Return aggregated failures
111
+ #
112
+ # @return [Array<Hash>]
113
+ #
114
+ # @api private
115
+ def failures
116
+ @failures ||= []
117
+ @failures += @base.opts if defined?(@base)
118
+ @failures.concat(@key.values.flat_map(&:opts)) if defined?(@key)
119
+ @failures
120
+ end
121
+
122
+ # @api private
123
+ def with(new_opts, &block)
124
+ self.class.new(_contract, **_options, **new_opts, &block)
125
+ end
126
+
127
+ # Return default (first) key name
128
+ #
129
+ # @return [Symbol]
130
+ #
131
+ # @api public
132
+ def key_name
133
+ @key_name ||= keys.first
134
+ end
135
+
136
+ # Return the value found under the first specified key
137
+ #
138
+ # This is a convenient method that can be used in all the common cases
139
+ # where a rule depends on just one key and you want a quick access to
140
+ # the value
141
+ #
142
+ # @example
143
+ # rule(:age) do
144
+ # key.failure(:invalid) if value < 18
145
+ # end
146
+ #
147
+ # @return [Object]
148
+ #
149
+ # @api public
150
+ def value
151
+ values[key_name]
152
+ end
153
+
154
+ # Return if the value under the default key is available
155
+ #
156
+ # This is useful when dealing with rules for optional keys
157
+ #
158
+ # @example
159
+ # rule(:age) do
160
+ # key.failure(:invalid) if key? && value < 18
161
+ # end
162
+ #
163
+ # @return [Boolean]
164
+ #
165
+ # @api public
166
+ def key?
167
+ values.key?(key_name)
168
+ end
169
+
170
+ # Check if there are any errors on the schema under the provided path
171
+ #
172
+ # @param path [Symbol, String, Array] A Path-compatible spec
173
+ #
174
+ # @return [Boolean]
175
+ #
176
+ # @api public
177
+ def schema_error?(path)
178
+ result.error?(path)
179
+ end
180
+
181
+ # Check if there are any errors on the current rule
182
+ #
183
+ # @return [Boolean]
184
+ #
185
+ # @api public
186
+ def rule_error?
187
+ !key(path).empty?
188
+ end
189
+
190
+ # @api private
191
+ def respond_to_missing?(meth, include_private = false)
192
+ super || _contract.respond_to?(meth, true)
193
+ end
194
+
195
+ private
196
+
197
+ # Forward to the underlying contract
198
+ #
199
+ # @api private
200
+ def method_missing(meth, *args, &block)
201
+ # yes, we do want to delegate to private methods too
202
+ if _contract.respond_to?(meth, true)
203
+ _contract.__send__(meth, *args, &block)
204
+ else
205
+ super
206
+ end
207
+ end
208
+ ruby2_keywords(:method_missing) if respond_to?(:ruby2_keywords, true)
209
+ end
210
+ end
211
+ end
@@ -0,0 +1,67 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Dry
4
+ module Validation
5
+ # Hints extension
6
+ #
7
+ # @example
8
+ # Dry::Validation.load_extensions(:hints)
9
+ #
10
+ # contract = Dry::Validation::Contract.build do
11
+ # schema do
12
+ # required(:name).filled(:string, min_size?: 2..4)
13
+ # end
14
+ # end
15
+ #
16
+ # contract.call(name: "fo").hints
17
+ # # {:name=>["size must be within 2 - 4"]}
18
+ #
19
+ # contract.call(name: "").messages
20
+ # # {:name=>["must be filled", "size must be within 2 - 4"]}
21
+ #
22
+ # @api public
23
+ module Hints
24
+ # Hints extensions for Result
25
+ #
26
+ # @api public
27
+ module ResultExtensions
28
+ # Return error messages excluding hints
29
+ #
30
+ # @macro errors-options
31
+ # @return [MessageSet]
32
+ #
33
+ # @api public
34
+ def errors(new_options = EMPTY_HASH)
35
+ opts = new_options.merge(hints: false)
36
+ @errors.with(schema_errors(opts), opts)
37
+ end
38
+
39
+ # Return errors and hints
40
+ #
41
+ # @macro errors-options
42
+ #
43
+ # @return [MessageSet]
44
+ #
45
+ # @api public
46
+ def messages(new_options = EMPTY_HASH)
47
+ errors.with(hints(new_options).to_a, options.merge(**new_options))
48
+ end
49
+
50
+ # Return hint messages
51
+ #
52
+ # @macro errors-options
53
+ #
54
+ # @return [MessageSet]
55
+ #
56
+ # @api public
57
+ def hints(new_options = EMPTY_HASH)
58
+ schema_result.hints(new_options)
59
+ end
60
+ end
61
+
62
+ Dry::Schema.load_extensions(:hints)
63
+
64
+ Result.prepend(ResultExtensions)
65
+ end
66
+ end
67
+ end
@@ -0,0 +1,34 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "dry/monads/result"
4
+
5
+ module Dry
6
+ module Validation
7
+ # Monad extension for contract results
8
+ #
9
+ # @example
10
+ # Dry::Validation.load_extensions(:monads)
11
+ #
12
+ # contract = Dry::Validation::Contract.build do
13
+ # schema do
14
+ # required(:name).filled(:string)
15
+ # end
16
+ # end
17
+ #
18
+ # contract.call(name: nil).to_monad
19
+ #
20
+ # @api public
21
+ class Result
22
+ include Dry::Monads::Result::Mixin
23
+
24
+ # Returns a result monad
25
+ #
26
+ # @return [Dry::Monads::Result]
27
+ #
28
+ # @api public
29
+ def to_monad
30
+ success? ? Success(self) : Failure(self)
31
+ end
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,75 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "dry/schema/predicate_registry"
4
+ require "dry/validation/contract"
5
+
6
+ module Dry
7
+ module Validation
8
+ # Predicate registry with additional needed methods.
9
+ class PredicateRegistry < Schema::PredicateRegistry
10
+ # List of predicates to be imported by `:predicates_as_macros`
11
+ # extension.
12
+ #
13
+ # @see Dry::Validation::Contract
14
+ WHITELIST = %i[
15
+ filled? format? gt? gteq? included_in? includes? inclusion? is? lt?
16
+ lteq? max_size? min_size? not_eql? odd? respond_to? size? true?
17
+ uuid_v4?
18
+ ].freeze
19
+
20
+ # @api private
21
+ def arg_names(name)
22
+ arg_list(name).map(&:first)
23
+ end
24
+
25
+ # @api private
26
+ def call(name, args)
27
+ self[name].(*args)
28
+ end
29
+
30
+ # @api private
31
+ def message_opts(name, arg_values)
32
+ arg_names(name).zip(arg_values).to_h
33
+ end
34
+ end
35
+
36
+ # Extension to use dry-logic predicates as macros.
37
+ #
38
+ # @see Dry::Validation::PredicateRegistry::WHITELIST Available predicates
39
+ #
40
+ # @example
41
+ # Dry::Validation.load_extensions(:predicates_as_macros)
42
+ #
43
+ # class ApplicationContract < Dry::Validation::Contract
44
+ # import_predicates_as_macros
45
+ # end
46
+ #
47
+ # class AgeContract < ApplicationContract
48
+ # schema do
49
+ # required(:age).filled(:integer)
50
+ # end
51
+ #
52
+ # rule(:age).validate(gteq?: 18)
53
+ # end
54
+ #
55
+ # AgeContract.new.(age: 17).errors.first.text
56
+ # # => 'must be greater than or equal to 18'
57
+ #
58
+ # @api public
59
+ class Contract
60
+ # Make macros available for self and its descendants.
61
+ def self.import_predicates_as_macros
62
+ registry = PredicateRegistry.new
63
+
64
+ PredicateRegistry::WHITELIST.each do |name|
65
+ register_macro(name) do |macro:|
66
+ predicate_args = [*macro.args, value]
67
+ message_opts = registry.message_opts(name, predicate_args)
68
+
69
+ key.failure(name, message_opts) unless registry.(name, predicate_args)
70
+ end
71
+ end
72
+ end
73
+ end
74
+ end
75
+ end
@@ -0,0 +1,70 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "dry/schema/path"
4
+ require "dry/validation/constants"
5
+
6
+ module Dry
7
+ module Validation
8
+ # Failure accumulator object
9
+ #
10
+ # @api public
11
+ class Failures
12
+ # The path for messages accumulated by failures object
13
+ #
14
+ # @return [Dry::Schema::Path]
15
+ #
16
+ # @api private
17
+ attr_reader :path
18
+
19
+ # Options for messages
20
+ #
21
+ # These options are used by MessageResolver
22
+ #
23
+ # @return [Hash]
24
+ #
25
+ # @api private
26
+ attr_reader :opts
27
+
28
+ # @api private
29
+ def initialize(path = ROOT_PATH)
30
+ @path = Dry::Schema::Path[path]
31
+ @opts = EMPTY_ARRAY.dup
32
+ end
33
+
34
+ # Set failure
35
+ #
36
+ # @overload failure(message)
37
+ # Set message text explicitly
38
+ # @param message [String] The message text
39
+ # @example
40
+ # failure('this failed')
41
+ #
42
+ # @overload failure(id)
43
+ # Use message identifier (needs localized messages setup)
44
+ # @param id [Symbol] The message id
45
+ # @example
46
+ # failure(:taken)
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
+ #
55
+ # @see Evaluator#key
56
+ # @see Evaluator#base
57
+ #
58
+ # @api public
59
+ def failure(message, tokens = EMPTY_HASH)
60
+ opts << {message: message, tokens: tokens, path: path}
61
+ self
62
+ end
63
+
64
+ # @api private
65
+ def empty?
66
+ opts.empty?
67
+ end
68
+ end
69
+ end
70
+ end