dry-validation 0.12.0 → 1.6.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +696 -239
- data/LICENSE +1 -1
- data/README.md +15 -12
- data/config/errors.yml +3 -88
- data/dry-validation.gemspec +31 -17
- data/lib/dry-validation.rb +3 -1
- data/lib/dry/validation.rb +45 -28
- data/lib/dry/validation/config.rb +24 -0
- data/lib/dry/validation/constants.rb +43 -0
- data/lib/dry/validation/contract.rb +171 -0
- data/lib/dry/validation/contract/class_interface.rb +230 -0
- data/lib/dry/validation/evaluator.rb +224 -0
- data/lib/dry/validation/extensions/hints.rb +67 -0
- data/lib/dry/validation/extensions/monads.rb +24 -8
- data/lib/dry/validation/extensions/predicates_as_macros.rb +75 -0
- data/lib/dry/validation/failures.rb +70 -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 +80 -80
- data/lib/dry/validation/message_set.rb +80 -105
- data/lib/dry/validation/messages/resolver.rb +131 -0
- data/lib/dry/validation/result.rb +183 -41
- data/lib/dry/validation/rule.rb +135 -0
- data/lib/dry/validation/schema_ext.rb +19 -0
- data/lib/dry/validation/values.rb +104 -0
- data/lib/dry/validation/version.rb +3 -1
- metadata +47 -336
- data/.codeclimate.yml +0 -17
- data/.gitignore +0 -9
- data/.rspec +0 -3
- data/.travis.yml +0 -26
- data/CONTRIBUTING.md +0 -31
- data/Gemfile +0 -33
- 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 -135
- 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 -192
- 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 -112
- data/lib/dry/validation/messages/i18n.rb +0 -35
- data/lib/dry/validation/messages/namespaced.rb +0 -32
- data/lib/dry/validation/messages/yaml.rb +0 -54
- data/lib/dry/validation/predicate_registry.rb +0 -115
- 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 -189
- 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/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 -6
- data/spec/fixtures/locales/pl.yml +0 -18
- data/spec/integration/custom_error_messages_spec.rb +0 -48
- 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 -94
- 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 -58
- 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,230 @@
|
|
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
|
+
def ensure_valid_keys(*keys)
|
158
|
+
valid_paths = key_map.to_dot_notation
|
159
|
+
key_paths = key_paths(keys)
|
160
|
+
|
161
|
+
invalid_keys = key_paths.map { |(key, path)|
|
162
|
+
unless valid_paths.any? { |vp| vp.include?(path) || vp.include?("#{path}[]") }
|
163
|
+
key
|
164
|
+
end
|
165
|
+
}.compact.uniq
|
166
|
+
|
167
|
+
return if invalid_keys.empty?
|
168
|
+
|
169
|
+
raise InvalidKeysError, <<~STR.strip
|
170
|
+
#{name}.rule specifies keys that are not defined by the schema: #{invalid_keys.inspect}
|
171
|
+
STR
|
172
|
+
end
|
173
|
+
|
174
|
+
# @api private
|
175
|
+
def key_paths(keys)
|
176
|
+
keys.map { |key|
|
177
|
+
case key
|
178
|
+
when Hash
|
179
|
+
path = Schema::Path[key]
|
180
|
+
if path.multi_value?
|
181
|
+
*head, tail = Array(path)
|
182
|
+
[key].product(
|
183
|
+
tail.map { |el| [*head, *el] }.map { |parts| parts.join(DOT) }
|
184
|
+
)
|
185
|
+
else
|
186
|
+
[[key, path.to_a.join(DOT)]]
|
187
|
+
end
|
188
|
+
when Array
|
189
|
+
[[key, Schema::Path[key].to_a.join(DOT)]]
|
190
|
+
else
|
191
|
+
[[key, key.to_s]]
|
192
|
+
end
|
193
|
+
}.flatten(1)
|
194
|
+
end
|
195
|
+
|
196
|
+
# @api private
|
197
|
+
def key_map
|
198
|
+
__schema__.key_map
|
199
|
+
end
|
200
|
+
|
201
|
+
# @api private
|
202
|
+
def core_schema_opts
|
203
|
+
{parent: superclass&.__schema__, config: config}
|
204
|
+
end
|
205
|
+
|
206
|
+
# @api private
|
207
|
+
def define(method_name, external_schemas, &block)
|
208
|
+
return __schema__ if external_schemas.empty? && block.nil?
|
209
|
+
|
210
|
+
unless __schema__.nil?
|
211
|
+
raise ::Dry::Validation::DuplicateSchemaError, "Schema has already been defined"
|
212
|
+
end
|
213
|
+
|
214
|
+
schema_opts = core_schema_opts
|
215
|
+
|
216
|
+
schema_opts.update(parent: external_schemas) if external_schemas.any?
|
217
|
+
|
218
|
+
case method_name
|
219
|
+
when :schema
|
220
|
+
@__schema__ = Schema.define(**schema_opts, &block)
|
221
|
+
when :Params
|
222
|
+
@__schema__ = Schema.Params(**schema_opts, &block)
|
223
|
+
when :JSON
|
224
|
+
@__schema__ = Schema.JSON(**schema_opts, &block)
|
225
|
+
end
|
226
|
+
end
|
227
|
+
end
|
228
|
+
end
|
229
|
+
end
|
230
|
+
end
|
@@ -0,0 +1,224 @@
|
|
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)), ¯o.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 use the default key name
|
159
|
+
# rule(:age) do
|
160
|
+
# key.failure(:invalid) if key? && value < 18
|
161
|
+
# end
|
162
|
+
#
|
163
|
+
# @example specify the key name
|
164
|
+
# rule(:start_date, :end_date) do
|
165
|
+
# if key?(:start_date) && !key?(:end_date)
|
166
|
+
# key(:end_date).failure("must provide an end_date with start_date")
|
167
|
+
# end
|
168
|
+
# end
|
169
|
+
#
|
170
|
+
# @return [Boolean]
|
171
|
+
#
|
172
|
+
# @api public
|
173
|
+
def key?(name = key_name)
|
174
|
+
values.key?(name)
|
175
|
+
end
|
176
|
+
|
177
|
+
# Check if there are any errors on the schema under the provided path
|
178
|
+
#
|
179
|
+
# @param path [Symbol, String, Array] A Path-compatible spec
|
180
|
+
#
|
181
|
+
# @return [Boolean]
|
182
|
+
#
|
183
|
+
# @api public
|
184
|
+
def schema_error?(path)
|
185
|
+
result.schema_error?(path)
|
186
|
+
end
|
187
|
+
|
188
|
+
# Check if there are any errors on the current rule
|
189
|
+
#
|
190
|
+
# @param path [Symbol, String, Array] A Path-compatible spec
|
191
|
+
#
|
192
|
+
# @return [Boolean]
|
193
|
+
#
|
194
|
+
# @api public
|
195
|
+
def rule_error?(path = nil)
|
196
|
+
if path.nil?
|
197
|
+
!key(self.path).empty?
|
198
|
+
else
|
199
|
+
result.rule_error?(path)
|
200
|
+
end
|
201
|
+
end
|
202
|
+
|
203
|
+
# @api private
|
204
|
+
def respond_to_missing?(meth, include_private = false)
|
205
|
+
super || _contract.respond_to?(meth, true)
|
206
|
+
end
|
207
|
+
|
208
|
+
private
|
209
|
+
|
210
|
+
# Forward to the underlying contract
|
211
|
+
#
|
212
|
+
# @api private
|
213
|
+
def method_missing(meth, *args, &block)
|
214
|
+
# yes, we do want to delegate to private methods too
|
215
|
+
if _contract.respond_to?(meth, true)
|
216
|
+
_contract.__send__(meth, *args, &block)
|
217
|
+
else
|
218
|
+
super
|
219
|
+
end
|
220
|
+
end
|
221
|
+
ruby2_keywords(:method_missing) if respond_to?(:ruby2_keywords, true)
|
222
|
+
end
|
223
|
+
end
|
224
|
+
end
|