dry-validation 0.13.3 → 1.4.2
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 +390 -95
- data/LICENSE +1 -1
- data/README.md +15 -12
- data/config/errors.yml +3 -88
- data/dry-validation.gemspec +30 -19
- data/lib/dry-validation.rb +2 -0
- data/lib/dry/validation.rb +47 -28
- data/lib/dry/validation/config.rb +24 -0
- data/lib/dry/validation/constants.rb +43 -0
- data/lib/dry/validation/contract.rb +160 -0
- data/lib/dry/validation/contract/class_interface.rb +222 -0
- data/lib/dry/validation/evaluator.rb +198 -0
- data/lib/dry/validation/extensions/hints.rb +69 -0
- data/lib/dry/validation/extensions/monads.rb +23 -7
- data/lib/dry/validation/extensions/predicates_as_macros.rb +75 -0
- data/lib/dry/validation/failures.rb +65 -0
- data/lib/dry/validation/function.rb +44 -0
- data/lib/dry/validation/macro.rb +38 -0
- data/lib/dry/validation/macros.rb +104 -0
- data/lib/dry/validation/message.rb +79 -79
- data/lib/dry/validation/message_set.rb +109 -88
- data/lib/dry/validation/messages/resolver.rb +106 -0
- data/lib/dry/validation/result.rb +168 -40
- data/lib/dry/validation/rule.rb +135 -0
- data/lib/dry/validation/schema_ext.rb +46 -0
- data/lib/dry/validation/values.rb +95 -0
- data/lib/dry/validation/version.rb +3 -1
- metadata +45 -335
- data/.codeclimate.yml +0 -17
- data/.gitignore +0 -9
- data/.rspec +0 -3
- data/.travis.yml +0 -29
- data/CONTRIBUTING.md +0 -31
- data/Gemfile +0 -25
- data/Rakefile +0 -22
- data/benchmarks/benchmark_form_invalid.rb +0 -64
- data/benchmarks/benchmark_form_valid.rb +0 -64
- data/benchmarks/benchmark_schema_invalid_huge.rb +0 -52
- data/benchmarks/profile_schema_call_invalid.rb +0 -20
- data/benchmarks/profile_schema_call_valid.rb +0 -20
- data/benchmarks/profile_schema_definition.rb +0 -14
- data/benchmarks/profile_schema_huge_invalid.rb +0 -30
- data/benchmarks/profile_schema_messages_invalid.rb +0 -20
- data/benchmarks/suite.rb +0 -5
- data/examples/basic.rb +0 -15
- data/examples/each.rb +0 -14
- data/examples/json.rb +0 -12
- data/examples/multiple.rb +0 -27
- data/examples/nested.rb +0 -22
- data/examples/params.rb +0 -11
- data/lib/dry/validation/compat/form.rb +0 -67
- data/lib/dry/validation/deprecations.rb +0 -24
- data/lib/dry/validation/executor.rb +0 -91
- data/lib/dry/validation/extensions.rb +0 -7
- data/lib/dry/validation/extensions/struct.rb +0 -32
- data/lib/dry/validation/input_processor_compiler.rb +0 -137
- data/lib/dry/validation/input_processor_compiler/json.rb +0 -45
- data/lib/dry/validation/input_processor_compiler/params.rb +0 -49
- data/lib/dry/validation/input_processor_compiler/sanitizer.rb +0 -47
- data/lib/dry/validation/message_compiler.rb +0 -188
- data/lib/dry/validation/message_compiler/visitor_opts.rb +0 -37
- data/lib/dry/validation/messages.rb +0 -14
- data/lib/dry/validation/messages/abstract.rb +0 -119
- data/lib/dry/validation/messages/i18n.rb +0 -47
- data/lib/dry/validation/messages/namespaced.rb +0 -39
- data/lib/dry/validation/messages/yaml.rb +0 -61
- data/lib/dry/validation/predicate_registry.rb +0 -115
- data/lib/dry/validation/predicates.rb +0 -19
- data/lib/dry/validation/schema.rb +0 -126
- data/lib/dry/validation/schema/check.rb +0 -37
- data/lib/dry/validation/schema/class_interface.rb +0 -190
- data/lib/dry/validation/schema/deprecated.rb +0 -30
- data/lib/dry/validation/schema/dsl.rb +0 -118
- data/lib/dry/validation/schema/form.rb +0 -9
- data/lib/dry/validation/schema/json.rb +0 -21
- data/lib/dry/validation/schema/key.rb +0 -71
- data/lib/dry/validation/schema/params.rb +0 -22
- data/lib/dry/validation/schema/rule.rb +0 -202
- data/lib/dry/validation/schema/value.rb +0 -211
- data/lib/dry/validation/schema_compiler.rb +0 -81
- data/lib/dry/validation/template.rb +0 -66
- data/lib/dry/validation/type_specs.rb +0 -70
- data/spec/extensions/monads/result_spec.rb +0 -40
- data/spec/extensions/struct/schema_spec.rb +0 -32
- data/spec/fixtures/locales/en.yml +0 -8
- data/spec/fixtures/locales/pl.yml +0 -22
- data/spec/integration/custom_error_messages_spec.rb +0 -54
- data/spec/integration/custom_predicates_spec.rb +0 -228
- data/spec/integration/hints_spec.rb +0 -170
- data/spec/integration/injecting_rules_spec.rb +0 -30
- data/spec/integration/json/defining_base_schema_spec.rb +0 -41
- data/spec/integration/localized_error_messages_spec.rb +0 -72
- data/spec/integration/message_compiler_spec.rb +0 -405
- data/spec/integration/messages/i18n_spec.rb +0 -104
- data/spec/integration/optional_keys_spec.rb +0 -28
- data/spec/integration/params/predicates/array_spec.rb +0 -287
- data/spec/integration/params/predicates/empty_spec.rb +0 -263
- data/spec/integration/params/predicates/eql_spec.rb +0 -327
- data/spec/integration/params/predicates/even_spec.rb +0 -455
- data/spec/integration/params/predicates/excluded_from_spec.rb +0 -455
- data/spec/integration/params/predicates/excludes_spec.rb +0 -391
- data/spec/integration/params/predicates/false_spec.rb +0 -455
- data/spec/integration/params/predicates/filled_spec.rb +0 -467
- data/spec/integration/params/predicates/format_spec.rb +0 -454
- data/spec/integration/params/predicates/gt_spec.rb +0 -519
- data/spec/integration/params/predicates/gteq_spec.rb +0 -519
- data/spec/integration/params/predicates/included_in_spec.rb +0 -455
- data/spec/integration/params/predicates/includes_spec.rb +0 -391
- data/spec/integration/params/predicates/key_spec.rb +0 -67
- data/spec/integration/params/predicates/lt_spec.rb +0 -519
- data/spec/integration/params/predicates/lteq_spec.rb +0 -519
- data/spec/integration/params/predicates/max_size_spec.rb +0 -391
- data/spec/integration/params/predicates/min_size_spec.rb +0 -391
- data/spec/integration/params/predicates/none_spec.rb +0 -265
- data/spec/integration/params/predicates/not_eql_spec.rb +0 -327
- data/spec/integration/params/predicates/odd_spec.rb +0 -455
- data/spec/integration/params/predicates/size/fixed_spec.rb +0 -393
- data/spec/integration/params/predicates/size/range_spec.rb +0 -396
- data/spec/integration/params/predicates/true_spec.rb +0 -455
- data/spec/integration/params/predicates/type_spec.rb +0 -391
- data/spec/integration/result_spec.rb +0 -81
- data/spec/integration/schema/array_schema_spec.rb +0 -59
- data/spec/integration/schema/check_rules_spec.rb +0 -119
- data/spec/integration/schema/check_with_nested_el_spec.rb +0 -37
- data/spec/integration/schema/check_with_nth_el_spec.rb +0 -25
- data/spec/integration/schema/default_settings_spec.rb +0 -11
- data/spec/integration/schema/defining_base_schema_spec.rb +0 -41
- data/spec/integration/schema/dynamic_predicate_args_spec.rb +0 -43
- data/spec/integration/schema/each_with_set_spec.rb +0 -70
- data/spec/integration/schema/extending_dsl_spec.rb +0 -27
- data/spec/integration/schema/form_spec.rb +0 -236
- data/spec/integration/schema/hash_schema_spec.rb +0 -47
- data/spec/integration/schema/inheriting_schema_spec.rb +0 -31
- data/spec/integration/schema/input_processor_spec.rb +0 -46
- data/spec/integration/schema/json/explicit_types_spec.rb +0 -157
- data/spec/integration/schema/json_spec.rb +0 -163
- data/spec/integration/schema/macros/confirmation_spec.rb +0 -35
- data/spec/integration/schema/macros/each_spec.rb +0 -268
- data/spec/integration/schema/macros/filled_spec.rb +0 -87
- data/spec/integration/schema/macros/input_spec.rb +0 -139
- data/spec/integration/schema/macros/maybe_spec.rb +0 -99
- data/spec/integration/schema/macros/rule_spec.rb +0 -75
- data/spec/integration/schema/macros/value_spec.rb +0 -119
- data/spec/integration/schema/macros/when_spec.rb +0 -62
- data/spec/integration/schema/nested_schemas_spec.rb +0 -236
- data/spec/integration/schema/nested_values_spec.rb +0 -46
- data/spec/integration/schema/not_spec.rb +0 -34
- data/spec/integration/schema/numbers_spec.rb +0 -19
- data/spec/integration/schema/option_with_default_spec.rb +0 -64
- data/spec/integration/schema/or_spec.rb +0 -87
- data/spec/integration/schema/params/defining_base_schema_spec.rb +0 -41
- data/spec/integration/schema/params/explicit_types_spec.rb +0 -195
- data/spec/integration/schema/params_spec.rb +0 -234
- data/spec/integration/schema/predicate_verification_spec.rb +0 -9
- data/spec/integration/schema/predicates/array_spec.rb +0 -295
- data/spec/integration/schema/predicates/custom_spec.rb +0 -103
- data/spec/integration/schema/predicates/empty_spec.rb +0 -263
- data/spec/integration/schema/predicates/eql_spec.rb +0 -327
- data/spec/integration/schema/predicates/even_spec.rb +0 -455
- data/spec/integration/schema/predicates/excluded_from/array_spec.rb +0 -459
- data/spec/integration/schema/predicates/excluded_from/range_spec.rb +0 -459
- data/spec/integration/schema/predicates/excludes_spec.rb +0 -391
- data/spec/integration/schema/predicates/filled_spec.rb +0 -467
- data/spec/integration/schema/predicates/format_spec.rb +0 -455
- data/spec/integration/schema/predicates/gt_spec.rb +0 -519
- data/spec/integration/schema/predicates/gteq_spec.rb +0 -519
- data/spec/integration/schema/predicates/hash_spec.rb +0 -69
- data/spec/integration/schema/predicates/included_in/array_spec.rb +0 -459
- data/spec/integration/schema/predicates/included_in/range_spec.rb +0 -459
- data/spec/integration/schema/predicates/includes_spec.rb +0 -391
- data/spec/integration/schema/predicates/key_spec.rb +0 -88
- data/spec/integration/schema/predicates/lt_spec.rb +0 -520
- data/spec/integration/schema/predicates/lteq_spec.rb +0 -519
- data/spec/integration/schema/predicates/max_size_spec.rb +0 -391
- data/spec/integration/schema/predicates/min_size_spec.rb +0 -391
- data/spec/integration/schema/predicates/none_spec.rb +0 -265
- data/spec/integration/schema/predicates/not_eql_spec.rb +0 -391
- data/spec/integration/schema/predicates/odd_spec.rb +0 -455
- data/spec/integration/schema/predicates/size/fixed_spec.rb +0 -398
- data/spec/integration/schema/predicates/size/range_spec.rb +0 -395
- data/spec/integration/schema/predicates/type_spec.rb +0 -413
- data/spec/integration/schema/reusing_schema_spec.rb +0 -33
- data/spec/integration/schema/using_types_spec.rb +0 -135
- data/spec/integration/schema/validate_spec.rb +0 -120
- data/spec/integration/schema/xor_spec.rb +0 -35
- data/spec/integration/schema_builders_spec.rb +0 -17
- data/spec/integration/schema_spec.rb +0 -173
- data/spec/shared/message_compiler.rb +0 -11
- data/spec/shared/predicate_helper.rb +0 -15
- data/spec/shared/rule_compiler.rb +0 -8
- data/spec/spec_helper.rb +0 -62
- data/spec/support/define_struct.rb +0 -25
- data/spec/support/matchers.rb +0 -38
- data/spec/support/mutant.rb +0 -9
- data/spec/support/predicates_integration.rb +0 -7
- data/spec/unit/input_processor_compiler/json_spec.rb +0 -283
- data/spec/unit/input_processor_compiler/params_spec.rb +0 -328
- data/spec/unit/message_compiler/visit_failure_spec.rb +0 -38
- data/spec/unit/message_compiler/visit_spec.rb +0 -16
- data/spec/unit/message_compiler_spec.rb +0 -7
- data/spec/unit/predicate_registry_spec.rb +0 -34
- data/spec/unit/schema/key_spec.rb +0 -38
- data/spec/unit/schema/rule_spec.rb +0 -42
- data/spec/unit/schema/value_spec.rb +0 -131
- data/spec/unit/schema_spec.rb +0 -35
|
@@ -0,0 +1,222 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'dry/schema'
|
|
4
|
+
require 'dry/schema/messages'
|
|
5
|
+
require 'dry/schema/path'
|
|
6
|
+
require 'dry/schema/key_map'
|
|
7
|
+
|
|
8
|
+
require 'dry/validation/constants'
|
|
9
|
+
require 'dry/validation/macros'
|
|
10
|
+
require 'dry/validation/schema_ext'
|
|
11
|
+
|
|
12
|
+
module Dry
|
|
13
|
+
module Validation
|
|
14
|
+
class Contract
|
|
15
|
+
# Contract's class interface
|
|
16
|
+
#
|
|
17
|
+
# @see Contract
|
|
18
|
+
#
|
|
19
|
+
# @api public
|
|
20
|
+
module ClassInterface
|
|
21
|
+
include Macros::Registrar
|
|
22
|
+
|
|
23
|
+
# @api private
|
|
24
|
+
def inherited(klass)
|
|
25
|
+
super
|
|
26
|
+
klass.instance_variable_set('@config', config.dup)
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
# Configuration
|
|
30
|
+
#
|
|
31
|
+
# @example
|
|
32
|
+
# class MyContract < Dry::Validation::Contract
|
|
33
|
+
# config.messages.backend = :i18n
|
|
34
|
+
# end
|
|
35
|
+
#
|
|
36
|
+
# @return [Config]
|
|
37
|
+
#
|
|
38
|
+
# @api public
|
|
39
|
+
def config
|
|
40
|
+
@config ||= Validation::Config.new
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
# Return macros registered for this class
|
|
44
|
+
#
|
|
45
|
+
# @return [Macros::Container]
|
|
46
|
+
#
|
|
47
|
+
# @api public
|
|
48
|
+
def macros
|
|
49
|
+
config.macros
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
# Define a params schema for your contract
|
|
53
|
+
#
|
|
54
|
+
# This type of schema is suitable for HTTP parameters
|
|
55
|
+
#
|
|
56
|
+
# @return [Dry::Schema::Params,NilClass]
|
|
57
|
+
# @see https://dry-rb.org/gems/dry-schema/params/
|
|
58
|
+
#
|
|
59
|
+
# @api public
|
|
60
|
+
def params(*external_schemas, &block)
|
|
61
|
+
define(:Params, external_schemas, &block)
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
# Define a JSON schema for your contract
|
|
65
|
+
#
|
|
66
|
+
# This type of schema is suitable for JSON data
|
|
67
|
+
#
|
|
68
|
+
# @return [Dry::Schema::JSON,NilClass]
|
|
69
|
+
# @see https://dry-rb.org/gems/dry-schema/json/
|
|
70
|
+
#
|
|
71
|
+
# @api public
|
|
72
|
+
def json(*external_schemas, &block)
|
|
73
|
+
define(:JSON, external_schemas, &block)
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
# Define a plain schema for your contract
|
|
77
|
+
#
|
|
78
|
+
# This type of schema does not offer coercion out of the box
|
|
79
|
+
#
|
|
80
|
+
# @return [Dry::Schema::Processor,NilClass]
|
|
81
|
+
# @see https://dry-rb.org/gems/dry-schema/
|
|
82
|
+
#
|
|
83
|
+
# @api public
|
|
84
|
+
def schema(*external_schemas, &block)
|
|
85
|
+
define(:schema, external_schemas, &block)
|
|
86
|
+
end
|
|
87
|
+
|
|
88
|
+
# Define a rule for your contract
|
|
89
|
+
#
|
|
90
|
+
# @example using a symbol
|
|
91
|
+
# rule(:age) do
|
|
92
|
+
# failure('must be at least 18') if values[:age] < 18
|
|
93
|
+
# end
|
|
94
|
+
#
|
|
95
|
+
# @example using a path to a value and a custom predicate
|
|
96
|
+
# rule('address.street') do
|
|
97
|
+
# failure('please provide a valid street address') if valid_street?(values[:street])
|
|
98
|
+
# end
|
|
99
|
+
#
|
|
100
|
+
# @return [Rule]
|
|
101
|
+
#
|
|
102
|
+
# @api public
|
|
103
|
+
def rule(*keys, &block)
|
|
104
|
+
ensure_valid_keys(*keys) if __schema__
|
|
105
|
+
|
|
106
|
+
Rule.new(keys: keys, block: block).tap do |rule|
|
|
107
|
+
rules << rule
|
|
108
|
+
end
|
|
109
|
+
end
|
|
110
|
+
|
|
111
|
+
# A shortcut that can be used to define contracts that won't be reused or inherited
|
|
112
|
+
#
|
|
113
|
+
# @example
|
|
114
|
+
# my_contract = Dry::Validation::Contract.build do
|
|
115
|
+
# params do
|
|
116
|
+
# required(:name).filled(:string)
|
|
117
|
+
# end
|
|
118
|
+
# end
|
|
119
|
+
#
|
|
120
|
+
# my_contract.call(name: "Jane")
|
|
121
|
+
#
|
|
122
|
+
# @return [Contract]
|
|
123
|
+
#
|
|
124
|
+
# @api public
|
|
125
|
+
def build(options = EMPTY_HASH, &block)
|
|
126
|
+
Class.new(self, &block).new(**options)
|
|
127
|
+
end
|
|
128
|
+
|
|
129
|
+
# @api private
|
|
130
|
+
def __schema__
|
|
131
|
+
@__schema__ if defined?(@__schema__)
|
|
132
|
+
end
|
|
133
|
+
|
|
134
|
+
# Return rules defined in this class
|
|
135
|
+
#
|
|
136
|
+
# @return [Array<Rule>]
|
|
137
|
+
#
|
|
138
|
+
# @api private
|
|
139
|
+
def rules
|
|
140
|
+
@rules ||= EMPTY_ARRAY
|
|
141
|
+
.dup
|
|
142
|
+
.concat(superclass.respond_to?(:rules) ? superclass.rules : EMPTY_ARRAY)
|
|
143
|
+
end
|
|
144
|
+
|
|
145
|
+
# Return messages configured for this class
|
|
146
|
+
#
|
|
147
|
+
# @return [Dry::Schema::Messages]
|
|
148
|
+
#
|
|
149
|
+
# @api private
|
|
150
|
+
def messages
|
|
151
|
+
@messages ||= Schema::Messages.setup(config.messages)
|
|
152
|
+
end
|
|
153
|
+
|
|
154
|
+
private
|
|
155
|
+
|
|
156
|
+
# @api private
|
|
157
|
+
# rubocop:disable Metrics/AbcSize
|
|
158
|
+
def ensure_valid_keys(*keys)
|
|
159
|
+
valid_paths = key_map.to_dot_notation.map { |value| Schema::Path[value] }
|
|
160
|
+
|
|
161
|
+
invalid_keys = keys
|
|
162
|
+
.map { |key|
|
|
163
|
+
[key, Schema::Path[key]]
|
|
164
|
+
}
|
|
165
|
+
.flat_map { |(key, path)|
|
|
166
|
+
if (last = path.last).is_a?(Array)
|
|
167
|
+
last.map { |last_key|
|
|
168
|
+
path_key = [*path.to_a[0..-2], last_key]
|
|
169
|
+
[path_key, Schema::Path[path_key]]
|
|
170
|
+
}
|
|
171
|
+
else
|
|
172
|
+
[[key, path]]
|
|
173
|
+
end
|
|
174
|
+
}
|
|
175
|
+
.reject { |(_, path)|
|
|
176
|
+
valid_paths.any? { |valid_path| valid_path.include?(path) }
|
|
177
|
+
}
|
|
178
|
+
.map(&:first)
|
|
179
|
+
|
|
180
|
+
return if invalid_keys.empty?
|
|
181
|
+
|
|
182
|
+
raise InvalidKeysError, <<~STR.strip
|
|
183
|
+
#{name}.rule specifies keys that are not defined by the schema: #{invalid_keys.inspect}
|
|
184
|
+
STR
|
|
185
|
+
end
|
|
186
|
+
# rubocop:enable Metrics/AbcSize
|
|
187
|
+
|
|
188
|
+
# @api private
|
|
189
|
+
def key_map
|
|
190
|
+
__schema__.key_map
|
|
191
|
+
end
|
|
192
|
+
|
|
193
|
+
# @api private
|
|
194
|
+
def core_schema_opts
|
|
195
|
+
{ parent: superclass&.__schema__, config: config }
|
|
196
|
+
end
|
|
197
|
+
|
|
198
|
+
# @api private
|
|
199
|
+
def define(method_name, external_schemas, &block)
|
|
200
|
+
return __schema__ if external_schemas.empty? && block.nil?
|
|
201
|
+
|
|
202
|
+
unless __schema__.nil?
|
|
203
|
+
raise ::Dry::Validation::DuplicateSchemaError, 'Schema has already been defined'
|
|
204
|
+
end
|
|
205
|
+
|
|
206
|
+
schema_opts = core_schema_opts
|
|
207
|
+
|
|
208
|
+
schema_opts.update(parent: external_schemas) if external_schemas.any?
|
|
209
|
+
|
|
210
|
+
case method_name
|
|
211
|
+
when :schema
|
|
212
|
+
@__schema__ = Schema.define(**schema_opts, &block)
|
|
213
|
+
when :Params
|
|
214
|
+
@__schema__ = Schema.Params(**schema_opts, &block)
|
|
215
|
+
when :JSON
|
|
216
|
+
@__schema__ = Schema.JSON(**schema_opts, &block)
|
|
217
|
+
end
|
|
218
|
+
end
|
|
219
|
+
end
|
|
220
|
+
end
|
|
221
|
+
end
|
|
222
|
+
end
|
|
@@ -0,0 +1,198 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'dry/initializer'
|
|
4
|
+
|
|
5
|
+
require 'dry/validation/constants'
|
|
6
|
+
require 'dry/validation/failures'
|
|
7
|
+
|
|
8
|
+
module Dry
|
|
9
|
+
module Validation
|
|
10
|
+
# Evaluator is the execution context for rules
|
|
11
|
+
#
|
|
12
|
+
# Evaluators expose an API for setting failure messages and forward
|
|
13
|
+
# method calls to the contracts, so that you can use your contract
|
|
14
|
+
# methods within rule blocks
|
|
15
|
+
#
|
|
16
|
+
# @api public
|
|
17
|
+
class Evaluator
|
|
18
|
+
extend Dry::Initializer
|
|
19
|
+
|
|
20
|
+
# @!attribute [r] _contract
|
|
21
|
+
# @return [Contract]
|
|
22
|
+
# @api private
|
|
23
|
+
param :_contract
|
|
24
|
+
|
|
25
|
+
# @!attribute [r] result
|
|
26
|
+
# @return [Result]
|
|
27
|
+
# @api private
|
|
28
|
+
option :result
|
|
29
|
+
|
|
30
|
+
# @!attribute [r] keys
|
|
31
|
+
# @return [Array<String, Symbol, Hash>]
|
|
32
|
+
# @api private
|
|
33
|
+
option :keys
|
|
34
|
+
|
|
35
|
+
# @!attribute [r] macros
|
|
36
|
+
# @return [Array<Symbol>]
|
|
37
|
+
# @api private
|
|
38
|
+
option :macros, optional: true, default: proc { EMPTY_ARRAY.dup }
|
|
39
|
+
|
|
40
|
+
# @!attribute [r] _context
|
|
41
|
+
# @return [Concurrent::Map]
|
|
42
|
+
# @api private
|
|
43
|
+
option :_context
|
|
44
|
+
|
|
45
|
+
# @!attribute [r] path
|
|
46
|
+
# @return [Dry::Schema::Path]
|
|
47
|
+
# @api private
|
|
48
|
+
option :path, default: proc { Dry::Schema::Path[(key = keys.first) ? key : ROOT_PATH] }
|
|
49
|
+
|
|
50
|
+
# @!attribute [r] values
|
|
51
|
+
# @return [Object]
|
|
52
|
+
# @api private
|
|
53
|
+
option :values
|
|
54
|
+
|
|
55
|
+
# @!attribute [r] block_options
|
|
56
|
+
# @return [Hash<Symbol=>Symbol>]
|
|
57
|
+
# @api private
|
|
58
|
+
option :block_options, default: proc { EMPTY_HASH }
|
|
59
|
+
|
|
60
|
+
# @return [Hash]
|
|
61
|
+
attr_reader :_options
|
|
62
|
+
|
|
63
|
+
# Initialize a new evaluator
|
|
64
|
+
#
|
|
65
|
+
# @api private
|
|
66
|
+
def initialize(contract, **options, &block)
|
|
67
|
+
super(contract, **options)
|
|
68
|
+
|
|
69
|
+
@_options = options
|
|
70
|
+
|
|
71
|
+
if block
|
|
72
|
+
exec_opts = block_options.map { |key, value| [key, _options[value]] }.to_h
|
|
73
|
+
instance_exec(**exec_opts, &block)
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
macros.each do |args|
|
|
77
|
+
macro = macro(*args.flatten(1))
|
|
78
|
+
instance_exec(**macro.extract_block_options(_options.merge(macro: macro)), ¯o.block)
|
|
79
|
+
end
|
|
80
|
+
end
|
|
81
|
+
|
|
82
|
+
# Get `Failures` object for the default or provided path
|
|
83
|
+
#
|
|
84
|
+
# @param [Symbol,String,Hash,Array<Symbol>] path
|
|
85
|
+
#
|
|
86
|
+
# @return [Failures]
|
|
87
|
+
#
|
|
88
|
+
# @see Failures#failure
|
|
89
|
+
#
|
|
90
|
+
# @api public
|
|
91
|
+
def key(path = self.path)
|
|
92
|
+
(@key ||= EMPTY_HASH.dup)[path] ||= Failures.new(path)
|
|
93
|
+
end
|
|
94
|
+
|
|
95
|
+
# Get `Failures` object for base errors
|
|
96
|
+
#
|
|
97
|
+
# @return [Failures]
|
|
98
|
+
#
|
|
99
|
+
# @see Failures#failure
|
|
100
|
+
#
|
|
101
|
+
# @api public
|
|
102
|
+
def base
|
|
103
|
+
@base ||= Failures.new
|
|
104
|
+
end
|
|
105
|
+
|
|
106
|
+
# Return aggregated failures
|
|
107
|
+
#
|
|
108
|
+
# @return [Array<Hash>]
|
|
109
|
+
#
|
|
110
|
+
# @api private
|
|
111
|
+
def failures
|
|
112
|
+
@failures ||= []
|
|
113
|
+
@failures += @base.opts if defined?(@base)
|
|
114
|
+
@failures.concat(@key.values.flat_map(&:opts)) if defined?(@key)
|
|
115
|
+
@failures
|
|
116
|
+
end
|
|
117
|
+
|
|
118
|
+
# @api private
|
|
119
|
+
def with(new_opts, &block)
|
|
120
|
+
self.class.new(_contract, **_options, **new_opts, &block)
|
|
121
|
+
end
|
|
122
|
+
|
|
123
|
+
# Return default (first) key name
|
|
124
|
+
#
|
|
125
|
+
# @return [Symbol]
|
|
126
|
+
#
|
|
127
|
+
# @api public
|
|
128
|
+
def key_name
|
|
129
|
+
@key_name ||= keys.first
|
|
130
|
+
end
|
|
131
|
+
|
|
132
|
+
# Return the value found under the first specified key
|
|
133
|
+
#
|
|
134
|
+
# This is a convenient method that can be used in all the common cases
|
|
135
|
+
# where a rule depends on just one key and you want a quick access to
|
|
136
|
+
# the value
|
|
137
|
+
#
|
|
138
|
+
# @example
|
|
139
|
+
# rule(:age) do
|
|
140
|
+
# key.failure(:invalid) if value < 18
|
|
141
|
+
# end
|
|
142
|
+
#
|
|
143
|
+
# @return [Object]
|
|
144
|
+
#
|
|
145
|
+
# @public
|
|
146
|
+
def value
|
|
147
|
+
values[key_name]
|
|
148
|
+
end
|
|
149
|
+
|
|
150
|
+
# Return if the value under the default key is available
|
|
151
|
+
#
|
|
152
|
+
# This is useful when dealing with rules for optional keys
|
|
153
|
+
#
|
|
154
|
+
# @example
|
|
155
|
+
# rule(:age) do
|
|
156
|
+
# key.failure(:invalid) if key? && value < 18
|
|
157
|
+
# end
|
|
158
|
+
#
|
|
159
|
+
# @return [Boolean]
|
|
160
|
+
#
|
|
161
|
+
# @api public
|
|
162
|
+
def key?
|
|
163
|
+
values.key?(key_name)
|
|
164
|
+
end
|
|
165
|
+
|
|
166
|
+
# Check if there are any errors under the provided path
|
|
167
|
+
#
|
|
168
|
+
# @param [Symbol, String, Array] A Path-compatible spec
|
|
169
|
+
#
|
|
170
|
+
# @return [Boolean]
|
|
171
|
+
#
|
|
172
|
+
# @api public
|
|
173
|
+
def error?(path)
|
|
174
|
+
result.error?(path)
|
|
175
|
+
end
|
|
176
|
+
|
|
177
|
+
# @api private
|
|
178
|
+
def respond_to_missing?(meth, include_private = false)
|
|
179
|
+
super || _contract.respond_to?(meth, true)
|
|
180
|
+
end
|
|
181
|
+
|
|
182
|
+
private
|
|
183
|
+
|
|
184
|
+
# Forward to the underlying contract
|
|
185
|
+
#
|
|
186
|
+
# @api private
|
|
187
|
+
def method_missing(meth, *args, &block)
|
|
188
|
+
# yes, we do want to delegate to private methods too
|
|
189
|
+
if _contract.respond_to?(meth, true)
|
|
190
|
+
_contract.__send__(meth, *args, &block)
|
|
191
|
+
else
|
|
192
|
+
super
|
|
193
|
+
end
|
|
194
|
+
end
|
|
195
|
+
ruby2_keywords(:method_missing) if respond_to?(:ruby2_keywords, true)
|
|
196
|
+
end
|
|
197
|
+
end
|
|
198
|
+
end
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'dry/monads/result'
|
|
4
|
+
|
|
5
|
+
module Dry
|
|
6
|
+
module Validation
|
|
7
|
+
# Hints extension
|
|
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
|
+
# Hints extensions for Result
|
|
27
|
+
#
|
|
28
|
+
# @api public
|
|
29
|
+
module ResultExtensions
|
|
30
|
+
# Return error messages excluding hints
|
|
31
|
+
#
|
|
32
|
+
# @macro errors-options
|
|
33
|
+
# @return [MessageSet]
|
|
34
|
+
#
|
|
35
|
+
# @api public
|
|
36
|
+
def errors(new_options = EMPTY_HASH)
|
|
37
|
+
opts = new_options.merge(hints: false)
|
|
38
|
+
@errors.with(schema_errors(opts), opts)
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
# Return errors and hints
|
|
42
|
+
#
|
|
43
|
+
# @macro errors-options
|
|
44
|
+
#
|
|
45
|
+
# @return [MessageSet]
|
|
46
|
+
#
|
|
47
|
+
# @api public
|
|
48
|
+
def messages(new_options = EMPTY_HASH)
|
|
49
|
+
errors.with(hints.to_a, options.merge(**new_options))
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
# Return hint messages
|
|
53
|
+
#
|
|
54
|
+
# @macro errors-options
|
|
55
|
+
#
|
|
56
|
+
# @return [MessageSet]
|
|
57
|
+
#
|
|
58
|
+
# @api public
|
|
59
|
+
def hints(new_options = EMPTY_HASH)
|
|
60
|
+
schema_result.hints(new_options)
|
|
61
|
+
end
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
Dry::Schema.load_extensions(:hints)
|
|
65
|
+
|
|
66
|
+
Result.prepend(ResultExtensions)
|
|
67
|
+
end
|
|
68
|
+
end
|
|
69
|
+
end
|