dry-validation 0.13.3 → 1.3.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +233 -12
- data/LICENSE +1 -1
- data/README.md +13 -9
- data/config/errors.yml +3 -88
- 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 +223 -0
- data/lib/dry/validation/evaluator.rb +197 -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 +58 -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 +108 -88
- data/lib/dry/validation/messages/resolver.rb +79 -0
- data/lib/dry/validation/result.rb +154 -42
- data/lib/dry/validation/rule.rb +129 -0
- data/lib/dry/validation/schema_ext.rb +46 -0
- data/lib/dry/validation/values.rb +94 -0
- data/lib/dry/validation/version.rb +3 -1
- metadata +41 -336
- 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/dry-validation.gemspec +0 -28
- 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,38 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'dry/validation/constants'
|
4
|
+
require 'dry/validation/function'
|
5
|
+
|
6
|
+
module Dry
|
7
|
+
module Validation
|
8
|
+
# A wrapper for macro validation blocks
|
9
|
+
#
|
10
|
+
# @api public
|
11
|
+
class Macro < Function
|
12
|
+
# @!attribute [r] name
|
13
|
+
# @return [Symbol]
|
14
|
+
# @api public
|
15
|
+
param :name
|
16
|
+
|
17
|
+
# @!attribute [r] args
|
18
|
+
# @return [Array]
|
19
|
+
# @api public
|
20
|
+
option :args
|
21
|
+
|
22
|
+
# @!attribute [r] block
|
23
|
+
# @return [Proc]
|
24
|
+
# @api private
|
25
|
+
option :block
|
26
|
+
|
27
|
+
# @api private
|
28
|
+
def with(args)
|
29
|
+
self.class.new(name, args: args, block: block)
|
30
|
+
end
|
31
|
+
|
32
|
+
# @api private
|
33
|
+
def extract_block_options(options)
|
34
|
+
block_options.map { |key, value| [key, options[value]] }.to_h
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
@@ -0,0 +1,104 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'dry/container'
|
4
|
+
require 'dry/validation/macro'
|
5
|
+
|
6
|
+
module Dry
|
7
|
+
module Validation
|
8
|
+
# API for registering and accessing Rule macros
|
9
|
+
#
|
10
|
+
# @api public
|
11
|
+
module Macros
|
12
|
+
module Registrar
|
13
|
+
# Register a macro
|
14
|
+
#
|
15
|
+
# @example register a global macro
|
16
|
+
# Dry::Validation.register_macro(:even_numbers) do
|
17
|
+
# key.failure('all numbers must be even') unless values[key_name].all?(&:even?)
|
18
|
+
# end
|
19
|
+
#
|
20
|
+
# @example register a contract macro
|
21
|
+
# class MyContract < Dry::Validation::Contract
|
22
|
+
# register_macro(:even_numbers) do
|
23
|
+
# key.failure('all numbers must be even') unless values[key_name].all?(&:even?)
|
24
|
+
# end
|
25
|
+
# end
|
26
|
+
#
|
27
|
+
# @param [Symbol] name The name of the macro
|
28
|
+
# @param [Array] *args Optional default arguments for the macro
|
29
|
+
#
|
30
|
+
# @return [self]
|
31
|
+
#
|
32
|
+
# @see Macro
|
33
|
+
#
|
34
|
+
# @api public
|
35
|
+
def register_macro(name, *args, &block)
|
36
|
+
macros.register(name, *args, &block)
|
37
|
+
self
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
# Registry for macros
|
42
|
+
#
|
43
|
+
# @api public
|
44
|
+
class Container
|
45
|
+
include Dry::Container::Mixin
|
46
|
+
|
47
|
+
# Register a new macro
|
48
|
+
#
|
49
|
+
# @example in a contract class
|
50
|
+
# class MyContract < Dry::Validation::Contract
|
51
|
+
# register_macro(:even_numbers) do
|
52
|
+
# key.failure('all numbers must be even') unless values[key_name].all?(&:even?)
|
53
|
+
# end
|
54
|
+
# end
|
55
|
+
#
|
56
|
+
# @param [Symbol] name The name of the macro
|
57
|
+
#
|
58
|
+
# @return [self]
|
59
|
+
#
|
60
|
+
# @api public
|
61
|
+
def register(name, *args, &block)
|
62
|
+
macro = Macro.new(name, args: args, block: block)
|
63
|
+
super(name, macro, call: false, &nil)
|
64
|
+
self
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
# Return a registered macro
|
69
|
+
#
|
70
|
+
# @param [Symbol] name The name of the macro
|
71
|
+
#
|
72
|
+
# @return [Proc]
|
73
|
+
#
|
74
|
+
# @api public
|
75
|
+
def self.[](name)
|
76
|
+
container[name]
|
77
|
+
end
|
78
|
+
|
79
|
+
# Register a global macro
|
80
|
+
#
|
81
|
+
# @see Container#register
|
82
|
+
#
|
83
|
+
# @return [Macros]
|
84
|
+
#
|
85
|
+
# @api public
|
86
|
+
def self.register(name, *args, &block)
|
87
|
+
container.register(name, *args, &block)
|
88
|
+
self
|
89
|
+
end
|
90
|
+
|
91
|
+
# @api private
|
92
|
+
def self.container
|
93
|
+
@container ||= Container.new
|
94
|
+
end
|
95
|
+
end
|
96
|
+
|
97
|
+
# Acceptance macro
|
98
|
+
#
|
99
|
+
# @api public
|
100
|
+
Macros.register(:acceptance) do
|
101
|
+
key.failure(:acceptance, key: key_name) unless values[key_name].equal?(true)
|
102
|
+
end
|
103
|
+
end
|
104
|
+
end
|
@@ -1,98 +1,98 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require 'dry/equalizer'
|
2
4
|
|
5
|
+
require 'dry/schema/constants'
|
6
|
+
require 'dry/schema/message'
|
7
|
+
|
3
8
|
module Dry
|
4
9
|
module Validation
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
10
|
+
# Message message
|
11
|
+
#
|
12
|
+
# @api public
|
13
|
+
class Message < Schema::Message
|
14
|
+
include Dry::Equalizer(:text, :path, :meta)
|
15
|
+
|
16
|
+
# The error message text
|
17
|
+
#
|
18
|
+
# @return [String] text
|
19
|
+
#
|
20
|
+
# @api public
|
21
|
+
attr_reader :text
|
22
|
+
|
23
|
+
# The path to the value with the error
|
24
|
+
#
|
25
|
+
# @return [Array<Symbol, Integer>]
|
26
|
+
#
|
27
|
+
# @api public
|
28
|
+
attr_reader :path
|
29
|
+
|
30
|
+
# Optional hash with meta-data
|
31
|
+
#
|
32
|
+
# @return [Hash]
|
33
|
+
#
|
34
|
+
# @api public
|
35
|
+
attr_reader :meta
|
36
|
+
|
37
|
+
# A localized message type
|
38
|
+
#
|
39
|
+
# Localized messsages can be translated to other languages at run-time
|
40
|
+
#
|
41
|
+
# @api public
|
42
|
+
class Localized < Message
|
43
|
+
# Evaluate message text using provided locale
|
44
|
+
#
|
45
|
+
# @example
|
46
|
+
# result.errors[:email].evaluate(locale: :en, full: true)
|
47
|
+
# # "email is invalid"
|
48
|
+
#
|
49
|
+
# @param [Hash] opts
|
50
|
+
# @option opts [Symbol] :locale Which locale to use
|
51
|
+
# @option opts [Boolean] :full Whether message text should include the key name
|
52
|
+
#
|
53
|
+
# @api public
|
54
|
+
def evaluate(**opts)
|
55
|
+
evaluated_text, rest = text.(opts)
|
56
|
+
Message.new(evaluated_text, path: path, meta: rest.merge(meta))
|
36
57
|
end
|
37
58
|
end
|
38
59
|
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
60
|
+
# Build an error
|
61
|
+
#
|
62
|
+
# @return [Message, Message::Localized]
|
63
|
+
#
|
64
|
+
# @api private
|
65
|
+
def self.[](text, path, meta)
|
66
|
+
klass = text.respond_to?(:call) ? Localized : Message
|
67
|
+
klass.new(text, path: path, meta: meta)
|
44
68
|
end
|
45
69
|
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
Message.new(predicate, path, text, options)
|
51
|
-
end
|
52
|
-
end
|
53
|
-
|
54
|
-
def initialize(predicate, path, text, options)
|
55
|
-
@predicate = predicate
|
56
|
-
@path = path
|
70
|
+
# Initialize a new error object
|
71
|
+
#
|
72
|
+
# @api private
|
73
|
+
def initialize(text, path:, meta: EMPTY_HASH)
|
57
74
|
@text = text
|
58
|
-
@
|
59
|
-
@
|
60
|
-
|
75
|
+
@path = Array(path)
|
76
|
+
@meta = meta
|
77
|
+
end
|
61
78
|
|
62
|
-
|
63
|
-
|
64
|
-
|
79
|
+
# Check if this is a base error not associated with any key
|
80
|
+
#
|
81
|
+
# @return [Boolean]
|
82
|
+
#
|
83
|
+
# @api public
|
84
|
+
def base?
|
85
|
+
@base ||= path.compact.empty?
|
65
86
|
end
|
66
87
|
|
88
|
+
# Dump error to a string
|
89
|
+
#
|
90
|
+
# @return [String]
|
91
|
+
#
|
92
|
+
# @api public
|
67
93
|
def to_s
|
68
94
|
text
|
69
95
|
end
|
70
|
-
|
71
|
-
def signature
|
72
|
-
@signature ||= [predicate, args, path].hash
|
73
|
-
end
|
74
|
-
|
75
|
-
def hint?
|
76
|
-
false
|
77
|
-
end
|
78
|
-
|
79
|
-
def root?
|
80
|
-
path.empty?
|
81
|
-
end
|
82
|
-
|
83
|
-
def eql?(other)
|
84
|
-
other.is_a?(String) ? text == other : super
|
85
|
-
end
|
86
|
-
end
|
87
|
-
|
88
|
-
class Hint < Message
|
89
|
-
def self.[](predicate, path, text, options)
|
90
|
-
Hint.new(predicate, path, text, options)
|
91
|
-
end
|
92
|
-
|
93
|
-
def hint?
|
94
|
-
true
|
95
|
-
end
|
96
96
|
end
|
97
97
|
end
|
98
98
|
end
|
@@ -1,122 +1,142 @@
|
|
1
|
-
|
2
|
-
module Validation
|
3
|
-
class MessageSet
|
4
|
-
include Enumerable
|
1
|
+
# frozen_string_literal: true
|
5
2
|
|
6
|
-
|
7
|
-
key? filled? none? bool?
|
8
|
-
str? int? float? decimal?
|
9
|
-
date? date_time? time? hash?
|
10
|
-
array? format?
|
11
|
-
).freeze
|
3
|
+
require 'dry/schema/message_set'
|
12
4
|
|
13
|
-
|
14
|
-
|
15
|
-
def self.[](messages, options = EMPTY_HASH)
|
16
|
-
new(messages.flatten, options)
|
17
|
-
end
|
5
|
+
require 'dry/validation/constants'
|
6
|
+
require 'dry/validation/message'
|
18
7
|
|
8
|
+
module Dry
|
9
|
+
module Validation
|
10
|
+
# MessageSet is a specialized message set for handling validation messages
|
11
|
+
#
|
12
|
+
# @api public
|
13
|
+
class MessageSet < Schema::MessageSet
|
14
|
+
# Return the source set of messages used to produce final evaluated messages
|
15
|
+
#
|
16
|
+
# @return [Array<Message, Message::Localized, Schema::Message>]
|
17
|
+
#
|
18
|
+
# @api private
|
19
|
+
attr_reader :source_messages
|
20
|
+
|
21
|
+
# Configured locale
|
22
|
+
#
|
23
|
+
# @return [Symbol]
|
24
|
+
#
|
25
|
+
# @api public
|
26
|
+
attr_reader :locale
|
27
|
+
|
28
|
+
# @api private
|
19
29
|
def initialize(messages, options = EMPTY_HASH)
|
20
|
-
@
|
21
|
-
@
|
22
|
-
|
23
|
-
@paths = failures.map(&:path).uniq
|
24
|
-
@options = options
|
25
|
-
|
26
|
-
initialize_hints!
|
27
|
-
initialize_placeholders!
|
28
|
-
end
|
29
|
-
|
30
|
-
def dump
|
31
|
-
root? ? to_a : to_h
|
30
|
+
@locale = options[:locale]
|
31
|
+
@source_messages = options.fetch(:source) { messages.dup }
|
32
|
+
super
|
32
33
|
end
|
33
34
|
|
34
|
-
|
35
|
-
|
35
|
+
# Return a new message set using updated options
|
36
|
+
#
|
37
|
+
# @return [MessageSet]
|
38
|
+
#
|
39
|
+
# @api private
|
40
|
+
def with(other, new_options = EMPTY_HASH)
|
41
|
+
return self if new_options.empty? && other.eql?(messages)
|
42
|
+
|
43
|
+
self.class.new(
|
44
|
+
(other + select { |err| err.is_a?(Message) }).uniq,
|
45
|
+
options.merge(source: source_messages, **new_options)
|
46
|
+
).freeze
|
36
47
|
end
|
37
48
|
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
49
|
+
# Add a new message
|
50
|
+
#
|
51
|
+
# This is used when result is being prepared
|
52
|
+
#
|
53
|
+
# @return [MessageSet]
|
54
|
+
#
|
55
|
+
# @api private
|
56
|
+
def add(message)
|
57
|
+
source_messages << message
|
58
|
+
messages << message
|
59
|
+
initialize_placeholders!
|
60
|
+
self
|
44
61
|
end
|
45
62
|
|
46
|
-
|
47
|
-
|
48
|
-
|
63
|
+
# Filter message set using provided predicates
|
64
|
+
#
|
65
|
+
# This method is open to any predicate because messages can be anything that
|
66
|
+
# implements Message API, thus they can implement whatever predicates you
|
67
|
+
# may need.
|
68
|
+
#
|
69
|
+
# @example get a list of base messages
|
70
|
+
# message_set = contract.(input).errors
|
71
|
+
# message_set.filter(:base?)
|
72
|
+
#
|
73
|
+
# @param [Array<Symbol>] predicates
|
74
|
+
#
|
75
|
+
# @return [MessageSet]
|
76
|
+
#
|
77
|
+
# @api public
|
78
|
+
def filter(*predicates)
|
79
|
+
messages = select { |msg|
|
80
|
+
predicates.all? { |predicate| msg.respond_to?(predicate) && msg.public_send(predicate) }
|
81
|
+
}
|
82
|
+
self.class.new(messages)
|
49
83
|
end
|
50
84
|
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
85
|
+
# @api private
|
86
|
+
def freeze
|
87
|
+
source_messages.select { |err| err.respond_to?(:evaluate) }.each do |err|
|
88
|
+
idx = source_messages.index(err)
|
89
|
+
msg = err.evaluate(locale: locale, full: options[:full])
|
90
|
+
messages[idx] = msg
|
56
91
|
end
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
def to_a
|
61
|
-
to_h.values.flatten
|
92
|
+
to_h
|
93
|
+
self
|
62
94
|
end
|
63
95
|
|
64
96
|
private
|
65
97
|
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
msgs.each do |msg|
|
71
|
-
node << msg
|
72
|
-
|
73
|
-
msg_hints = hint_groups[msg.path]
|
74
|
-
node.concat(msg_hints) if msg_hints
|
75
|
-
end
|
76
|
-
|
77
|
-
node.map!(&:to_s)
|
78
|
-
|
79
|
-
hash
|
80
|
-
end
|
98
|
+
# @api private
|
99
|
+
def unique_paths
|
100
|
+
source_messages.uniq(&:path).map(&:path)
|
81
101
|
end
|
82
102
|
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
node << msg
|
89
|
-
end
|
90
|
-
|
91
|
-
node.map!(&:to_s)
|
92
|
-
|
103
|
+
# @api private
|
104
|
+
def messages_map
|
105
|
+
@messages_map ||= reduce(placeholders) { |hash, msg|
|
106
|
+
node = msg.path.reduce(hash) { |a, e| a.is_a?(Hash) ? a[e] : a.last[e] }
|
107
|
+
(node[0].is_a?(::Array) ? node[0] : node) << msg.dump
|
93
108
|
hash
|
94
|
-
|
95
|
-
end
|
96
|
-
|
97
|
-
def hint_groups
|
98
|
-
@hint_groups ||= hints.group_by(&:path)
|
99
|
-
end
|
100
|
-
|
101
|
-
def initialize_hints!
|
102
|
-
hints.reject! { |hint| HINT_EXCLUSION.include?(hint.predicate) }
|
109
|
+
}
|
103
110
|
end
|
104
111
|
|
112
|
+
# @api private
|
113
|
+
#
|
114
|
+
# rubocop:disable Metrics/AbcSize
|
115
|
+
# rubocop:disable Metrics/PerceivedComplexity
|
105
116
|
def initialize_placeholders!
|
106
|
-
@placeholders =
|
117
|
+
@placeholders = unique_paths.each_with_object(EMPTY_HASH.dup) { |path, hash|
|
107
118
|
curr_idx = 0
|
108
119
|
last_idx = path.size - 1
|
109
120
|
node = hash
|
110
121
|
|
111
|
-
while curr_idx <= last_idx
|
122
|
+
while curr_idx <= last_idx
|
112
123
|
key = path[curr_idx]
|
113
|
-
|
124
|
+
|
125
|
+
next_node =
|
126
|
+
if node.is_a?(Array) && key.is_a?(Symbol)
|
127
|
+
node_hash = (node << [] << {}).last
|
128
|
+
node_hash[key] || (node_hash[key] = curr_idx < last_idx ? {} : [])
|
129
|
+
else
|
130
|
+
node[key] || (node[key] = curr_idx < last_idx ? {} : [])
|
131
|
+
end
|
132
|
+
|
133
|
+
node = next_node
|
114
134
|
curr_idx += 1
|
115
135
|
end
|
116
|
-
|
117
|
-
hash
|
118
|
-
end
|
136
|
+
}
|
119
137
|
end
|
138
|
+
# rubocop:enable Metrics/AbcSize
|
139
|
+
# rubocop:enable Metrics/PerceivedComplexity
|
120
140
|
end
|
121
141
|
end
|
122
142
|
end
|