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
@@ -1,45 +0,0 @@
|
|
1
|
-
module Dry
|
2
|
-
module Validation
|
3
|
-
class InputProcessorCompiler::JSON < InputProcessorCompiler
|
4
|
-
PREDICATE_MAP = {
|
5
|
-
default: 'string',
|
6
|
-
none?: 'json.nil',
|
7
|
-
bool?: 'bool',
|
8
|
-
str?: 'string',
|
9
|
-
int?: 'integer',
|
10
|
-
float?: 'float',
|
11
|
-
decimal?: 'json.decimal',
|
12
|
-
date?: 'json.date',
|
13
|
-
date_time?: 'json.date_time',
|
14
|
-
time?: 'json.time'
|
15
|
-
}.freeze
|
16
|
-
|
17
|
-
CONST_MAP = {
|
18
|
-
NilClass => 'nil',
|
19
|
-
String => 'string',
|
20
|
-
Integer => 'integer',
|
21
|
-
Float => 'float',
|
22
|
-
BigDecimal => 'json.decimal',
|
23
|
-
Array => 'json.array',
|
24
|
-
Hash => 'json.hash',
|
25
|
-
Date => 'json.date',
|
26
|
-
DateTime => 'json.date_time',
|
27
|
-
Time => 'json.time',
|
28
|
-
TrueClass => 'true',
|
29
|
-
FalseClass => 'false'
|
30
|
-
}.freeze
|
31
|
-
|
32
|
-
def identifier
|
33
|
-
:json
|
34
|
-
end
|
35
|
-
|
36
|
-
def hash_node(schema)
|
37
|
-
[:json_hash, [schema, {}]]
|
38
|
-
end
|
39
|
-
|
40
|
-
def array_node(members)
|
41
|
-
[:json_array, [members, {}]]
|
42
|
-
end
|
43
|
-
end
|
44
|
-
end
|
45
|
-
end
|
@@ -1,49 +0,0 @@
|
|
1
|
-
module Dry
|
2
|
-
module Validation
|
3
|
-
class InputProcessorCompiler::Params < InputProcessorCompiler
|
4
|
-
PREDICATE_MAP = {
|
5
|
-
default: 'string',
|
6
|
-
none?: 'params.nil',
|
7
|
-
bool?: 'params.bool',
|
8
|
-
true?: 'params.true',
|
9
|
-
false?: 'params.false',
|
10
|
-
str?: 'string',
|
11
|
-
int?: 'params.integer',
|
12
|
-
float?: 'params.float',
|
13
|
-
decimal?: 'params.decimal',
|
14
|
-
date?: 'params.date',
|
15
|
-
date_time?: 'params.date_time',
|
16
|
-
time?: 'params.time',
|
17
|
-
hash?: 'params.hash',
|
18
|
-
array?: 'params.array'
|
19
|
-
}.freeze
|
20
|
-
|
21
|
-
CONST_MAP = {
|
22
|
-
NilClass => 'params.nil',
|
23
|
-
String => 'string',
|
24
|
-
Integer => 'params.integer',
|
25
|
-
Float => 'params.float',
|
26
|
-
BigDecimal => 'params.decimal',
|
27
|
-
Array => 'params.array',
|
28
|
-
Hash => 'params.hash',
|
29
|
-
Date => 'params.date',
|
30
|
-
DateTime => 'params.date_time',
|
31
|
-
Time => 'params.time',
|
32
|
-
TrueClass => 'params.true',
|
33
|
-
FalseClass => 'params.false'
|
34
|
-
}.freeze
|
35
|
-
|
36
|
-
def identifier
|
37
|
-
:params
|
38
|
-
end
|
39
|
-
|
40
|
-
def hash_node(schema)
|
41
|
-
[:params_hash, [schema, {}]]
|
42
|
-
end
|
43
|
-
|
44
|
-
def array_node(members)
|
45
|
-
[:params_array, [members, {}]]
|
46
|
-
end
|
47
|
-
end
|
48
|
-
end
|
49
|
-
end
|
@@ -1,47 +0,0 @@
|
|
1
|
-
module Dry
|
2
|
-
module Validation
|
3
|
-
class InputProcessorCompiler::Sanitizer < InputProcessorCompiler
|
4
|
-
PREDICATE_MAP = {
|
5
|
-
default: 'string',
|
6
|
-
none?: 'nil',
|
7
|
-
bool?: 'bool',
|
8
|
-
str?: 'string',
|
9
|
-
int?: 'integer',
|
10
|
-
float?: 'float',
|
11
|
-
decimal?: 'decimal',
|
12
|
-
date?: 'date',
|
13
|
-
date_time?: 'date_time',
|
14
|
-
time?: 'time',
|
15
|
-
hash?: 'hash',
|
16
|
-
array?: 'array'
|
17
|
-
}.freeze
|
18
|
-
|
19
|
-
CONST_MAP = {
|
20
|
-
NilClass => 'nil',
|
21
|
-
String => 'string',
|
22
|
-
Integer => 'integer',
|
23
|
-
Float => 'float',
|
24
|
-
BigDecimal => 'decimal',
|
25
|
-
Array => 'array',
|
26
|
-
Hash => 'hash',
|
27
|
-
Date => 'date',
|
28
|
-
DateTime => 'date_time',
|
29
|
-
Time => 'time',
|
30
|
-
TrueClass => 'true',
|
31
|
-
FalseClass => 'false'
|
32
|
-
}.freeze
|
33
|
-
|
34
|
-
def identifier
|
35
|
-
:sanitizer
|
36
|
-
end
|
37
|
-
|
38
|
-
def hash_node(schema)
|
39
|
-
[:hash, [:weak, schema, {}]]
|
40
|
-
end
|
41
|
-
|
42
|
-
def array_node(members)
|
43
|
-
[:array, [members, {}]]
|
44
|
-
end
|
45
|
-
end
|
46
|
-
end
|
47
|
-
end
|
@@ -1,188 +0,0 @@
|
|
1
|
-
require 'dry/validation/message'
|
2
|
-
require 'dry/validation/message_set'
|
3
|
-
require 'dry/validation/message_compiler/visitor_opts'
|
4
|
-
|
5
|
-
module Dry
|
6
|
-
module Validation
|
7
|
-
class MessageCompiler
|
8
|
-
attr_reader :messages, :options, :locale, :default_lookup_options
|
9
|
-
|
10
|
-
EMPTY_OPTS = VisitorOpts.new
|
11
|
-
LIST_SEPARATOR = ', '.freeze
|
12
|
-
|
13
|
-
def initialize(messages, options = {})
|
14
|
-
@messages = messages
|
15
|
-
@options = options
|
16
|
-
@full = @options.fetch(:full, false)
|
17
|
-
@hints = @options.fetch(:hints, true)
|
18
|
-
@locale = @options.fetch(:locale, messages.default_locale)
|
19
|
-
@default_lookup_options = { locale: locale }
|
20
|
-
end
|
21
|
-
|
22
|
-
def full?
|
23
|
-
@full
|
24
|
-
end
|
25
|
-
|
26
|
-
def hints?
|
27
|
-
@hints
|
28
|
-
end
|
29
|
-
|
30
|
-
def with(new_options)
|
31
|
-
return self if new_options.empty?
|
32
|
-
self.class.new(messages, options.merge(new_options))
|
33
|
-
end
|
34
|
-
|
35
|
-
def call(ast)
|
36
|
-
MessageSet[ast.map { |node| visit(node) }, failures: options.fetch(:failures, true)]
|
37
|
-
end
|
38
|
-
|
39
|
-
def visit(node, *args)
|
40
|
-
__send__(:"visit_#{node[0]}", node[1], *args)
|
41
|
-
end
|
42
|
-
|
43
|
-
def visit_failure(node, opts = EMPTY_OPTS)
|
44
|
-
rule, other = node
|
45
|
-
visit(other, opts.(rule: rule))
|
46
|
-
end
|
47
|
-
|
48
|
-
def visit_hint(node, opts = EMPTY_OPTS)
|
49
|
-
if hints?
|
50
|
-
visit(node, opts.(message_type: :hint))
|
51
|
-
end
|
52
|
-
end
|
53
|
-
|
54
|
-
def visit_each(node, opts = EMPTY_OPTS)
|
55
|
-
# TODO: we can still generate a hint for elements here!
|
56
|
-
[]
|
57
|
-
end
|
58
|
-
|
59
|
-
def visit_not(node, opts = EMPTY_OPTS)
|
60
|
-
visit(node, opts.(not: true))
|
61
|
-
end
|
62
|
-
|
63
|
-
def visit_check(node, opts = EMPTY_OPTS)
|
64
|
-
keys, other = node
|
65
|
-
visit(other, opts.(path: keys.last, check: true))
|
66
|
-
end
|
67
|
-
|
68
|
-
def visit_rule(node, opts = EMPTY_OPTS)
|
69
|
-
name, other = node
|
70
|
-
visit(other, opts.(rule: name))
|
71
|
-
end
|
72
|
-
|
73
|
-
def visit_schema(node, opts = EMPTY_OPTS)
|
74
|
-
node.rule_ast.map { |rule| visit(rule, opts) }
|
75
|
-
end
|
76
|
-
|
77
|
-
def visit_and(node, opts = EMPTY_OPTS)
|
78
|
-
left, right = node.map { |n| visit(n, opts) }
|
79
|
-
|
80
|
-
if right
|
81
|
-
[left, right]
|
82
|
-
else
|
83
|
-
left
|
84
|
-
end
|
85
|
-
end
|
86
|
-
|
87
|
-
def visit_or(node, opts = EMPTY_OPTS)
|
88
|
-
left, right = node.map { |n| visit(n, opts) }
|
89
|
-
|
90
|
-
if [left, right].flatten.map(&:path).uniq.size == 1
|
91
|
-
Message::Or.new(left, right, -> k { messages[k, default_lookup_options] })
|
92
|
-
elsif right.is_a?(Array)
|
93
|
-
right
|
94
|
-
else
|
95
|
-
[left, right]
|
96
|
-
end
|
97
|
-
end
|
98
|
-
|
99
|
-
def visit_predicate(node, base_opts = EMPTY_OPTS)
|
100
|
-
predicate, args = node
|
101
|
-
|
102
|
-
*arg_vals, val = args.map(&:last)
|
103
|
-
tokens = message_tokens(args)
|
104
|
-
|
105
|
-
input = val != Undefined ? val : nil
|
106
|
-
|
107
|
-
options = base_opts.update(lookup_options(arg_vals: arg_vals, input: input))
|
108
|
-
msg_opts = options.update(tokens)
|
109
|
-
|
110
|
-
rule = msg_opts[:rule]
|
111
|
-
path = msg_opts[:path]
|
112
|
-
|
113
|
-
template = messages[rule] || messages[predicate, msg_opts]
|
114
|
-
|
115
|
-
unless template
|
116
|
-
raise MissingMessageError, "message for #{predicate} was not found"
|
117
|
-
end
|
118
|
-
|
119
|
-
text = message_text(rule, template, tokens, options)
|
120
|
-
|
121
|
-
message_class = options[:message_type] == :hint ? Hint : Message
|
122
|
-
|
123
|
-
message_class[
|
124
|
-
predicate, path, text,
|
125
|
-
args: arg_vals,
|
126
|
-
input: input,
|
127
|
-
rule: rule,
|
128
|
-
check: base_opts[:check]
|
129
|
-
]
|
130
|
-
end
|
131
|
-
|
132
|
-
def visit_key(node, opts = EMPTY_OPTS)
|
133
|
-
name, other = node
|
134
|
-
visit(other, opts.(path: name))
|
135
|
-
end
|
136
|
-
|
137
|
-
def visit_set(node, opts = EMPTY_OPTS)
|
138
|
-
node.map { |el| visit(el, opts) }
|
139
|
-
end
|
140
|
-
|
141
|
-
def visit_implication(node, *args)
|
142
|
-
_, right = node
|
143
|
-
visit(right, *args)
|
144
|
-
end
|
145
|
-
|
146
|
-
def visit_xor(node, opts = EMPTY_OPTS)
|
147
|
-
left, right = node
|
148
|
-
[visit(left, opts), visit(right, opts)].uniq
|
149
|
-
end
|
150
|
-
|
151
|
-
def visit_type(node, opts = EMPTY_OPTS)
|
152
|
-
visit(node.rule.to_ast, opts)
|
153
|
-
end
|
154
|
-
|
155
|
-
def lookup_options(arg_vals: [], input: nil)
|
156
|
-
default_lookup_options.merge(
|
157
|
-
arg_type: arg_vals.size == 1 && arg_vals[0].class,
|
158
|
-
val_type: input.class
|
159
|
-
)
|
160
|
-
end
|
161
|
-
|
162
|
-
def message_text(rule, template, tokens, opts)
|
163
|
-
text = template[template.data(tokens)]
|
164
|
-
|
165
|
-
if full?
|
166
|
-
rule_name = messages.rule(rule, opts) || rule
|
167
|
-
"#{rule_name} #{text}"
|
168
|
-
else
|
169
|
-
text
|
170
|
-
end
|
171
|
-
end
|
172
|
-
|
173
|
-
def message_tokens(args)
|
174
|
-
args.each_with_object({}) { |arg, hash|
|
175
|
-
case arg[1]
|
176
|
-
when Array
|
177
|
-
hash[arg[0]] = arg[1].join(LIST_SEPARATOR)
|
178
|
-
when Range
|
179
|
-
hash["#{arg[0]}_left".to_sym] = arg[1].first
|
180
|
-
hash["#{arg[0]}_right".to_sym] = arg[1].last
|
181
|
-
else
|
182
|
-
hash[arg[0]] = arg[1]
|
183
|
-
end
|
184
|
-
}
|
185
|
-
end
|
186
|
-
end
|
187
|
-
end
|
188
|
-
end
|
@@ -1,37 +0,0 @@
|
|
1
|
-
module Dry
|
2
|
-
module Validation
|
3
|
-
class MessageCompiler
|
4
|
-
class VisitorOpts < Hash
|
5
|
-
def self.new
|
6
|
-
opts = super
|
7
|
-
opts[:path] = EMPTY_ARRAY
|
8
|
-
opts[:rule] = nil
|
9
|
-
opts[:message_type] = :failure
|
10
|
-
opts
|
11
|
-
end
|
12
|
-
|
13
|
-
def path?
|
14
|
-
! path.empty?
|
15
|
-
end
|
16
|
-
|
17
|
-
def path
|
18
|
-
self[:path]
|
19
|
-
end
|
20
|
-
|
21
|
-
def rule
|
22
|
-
self[:rule]
|
23
|
-
end
|
24
|
-
|
25
|
-
def with_rule(new_rule, **other)
|
26
|
-
opts = dup
|
27
|
-
opts[:rule] = new_rule unless opts.rule
|
28
|
-
opts.(other)
|
29
|
-
end
|
30
|
-
|
31
|
-
def call(other)
|
32
|
-
merge(other.update(path: [*path, *other[:path]]))
|
33
|
-
end
|
34
|
-
end
|
35
|
-
end
|
36
|
-
end
|
37
|
-
end
|
@@ -1,14 +0,0 @@
|
|
1
|
-
module Dry
|
2
|
-
module Validation
|
3
|
-
module Messages
|
4
|
-
def self.default
|
5
|
-
Messages::YAML.load
|
6
|
-
end
|
7
|
-
end
|
8
|
-
end
|
9
|
-
end
|
10
|
-
|
11
|
-
require 'dry/validation/messages/abstract'
|
12
|
-
require 'dry/validation/messages/namespaced'
|
13
|
-
require 'dry/validation/messages/yaml'
|
14
|
-
require 'dry/validation/messages/i18n' if defined?(I18n)
|
@@ -1,119 +0,0 @@
|
|
1
|
-
require 'pathname'
|
2
|
-
require 'concurrent/map'
|
3
|
-
|
4
|
-
require 'dry/equalizer'
|
5
|
-
require 'dry/configurable'
|
6
|
-
|
7
|
-
require 'dry/validation/template'
|
8
|
-
|
9
|
-
module Dry
|
10
|
-
module Validation
|
11
|
-
module Messages
|
12
|
-
class Abstract
|
13
|
-
extend Dry::Configurable
|
14
|
-
include Dry::Equalizer(:config)
|
15
|
-
|
16
|
-
DEFAULT_PATH = Pathname(__dir__).join('../../../../config/errors.yml').realpath.freeze
|
17
|
-
|
18
|
-
setting :paths, [DEFAULT_PATH]
|
19
|
-
setting :root, 'errors'.freeze
|
20
|
-
setting :lookup_options, [:root, :predicate, :rule, :val_type, :arg_type].freeze
|
21
|
-
|
22
|
-
setting :lookup_paths, %w(
|
23
|
-
%{root}.rules.%{rule}.%{predicate}.arg.%{arg_type}
|
24
|
-
%{root}.rules.%{rule}.%{predicate}
|
25
|
-
%{root}.%{predicate}.%{message_type}
|
26
|
-
%{root}.%{predicate}.value.%{rule}.arg.%{arg_type}
|
27
|
-
%{root}.%{predicate}.value.%{rule}
|
28
|
-
%{root}.%{predicate}.value.%{val_type}.arg.%{arg_type}
|
29
|
-
%{root}.%{predicate}.value.%{val_type}
|
30
|
-
%{root}.%{predicate}.arg.%{arg_type}
|
31
|
-
%{root}.%{predicate}
|
32
|
-
).freeze
|
33
|
-
|
34
|
-
setting :arg_type_default, 'default'.freeze
|
35
|
-
setting :val_type_default, 'default'.freeze
|
36
|
-
|
37
|
-
setting :arg_types, Hash.new { |*| config.arg_type_default }.update(
|
38
|
-
Range => 'range'
|
39
|
-
)
|
40
|
-
|
41
|
-
setting :val_types, Hash.new { |*| config.val_type_default }.update(
|
42
|
-
Range => 'range',
|
43
|
-
String => 'string'
|
44
|
-
)
|
45
|
-
|
46
|
-
CACHE_KEYS = %i[path message_type val_type arg_type locale].freeze
|
47
|
-
|
48
|
-
def self.cache
|
49
|
-
@cache ||= Concurrent::Map.new { |h, k| h[k] = Concurrent::Map.new }
|
50
|
-
end
|
51
|
-
|
52
|
-
attr_reader :config
|
53
|
-
|
54
|
-
def initialize
|
55
|
-
@config = self.class.config
|
56
|
-
end
|
57
|
-
|
58
|
-
def hash
|
59
|
-
@hash ||= config.hash
|
60
|
-
end
|
61
|
-
|
62
|
-
def rule(name, options = {})
|
63
|
-
path = "%{locale}.rules.#{name}"
|
64
|
-
get(path, options) if key?(path, options)
|
65
|
-
end
|
66
|
-
|
67
|
-
def call(predicate, options = EMPTY_HASH)
|
68
|
-
cache.fetch_or_store([predicate, options.reject { |k,| k.equal?(:input) }]) do
|
69
|
-
path, opts = lookup(predicate, options)
|
70
|
-
return unless path
|
71
|
-
text = yield(path, opts)
|
72
|
-
Template[text]
|
73
|
-
end
|
74
|
-
end
|
75
|
-
alias_method :[], :call
|
76
|
-
|
77
|
-
def lookup(predicate, options = {})
|
78
|
-
tokens = options.merge(
|
79
|
-
root: root,
|
80
|
-
predicate: predicate,
|
81
|
-
arg_type: config.arg_types[options[:arg_type]],
|
82
|
-
val_type: config.val_types[options[:val_type]],
|
83
|
-
message_type: options[:message_type] || :failure
|
84
|
-
)
|
85
|
-
|
86
|
-
tokens[:rule] = predicate unless tokens.key?(:rule)
|
87
|
-
|
88
|
-
opts = options.select { |k, _| !config.lookup_options.include?(k) }
|
89
|
-
|
90
|
-
path = lookup_paths(tokens).detect do |key|
|
91
|
-
key?(key, opts) && get(key, opts).is_a?(String)
|
92
|
-
end
|
93
|
-
|
94
|
-
[path, opts]
|
95
|
-
end
|
96
|
-
|
97
|
-
def lookup_paths(tokens)
|
98
|
-
config.lookup_paths.map { |path| path % tokens }
|
99
|
-
end
|
100
|
-
|
101
|
-
def namespaced(namespace)
|
102
|
-
Messages::Namespaced.new(namespace, self)
|
103
|
-
end
|
104
|
-
|
105
|
-
def root
|
106
|
-
config.root
|
107
|
-
end
|
108
|
-
|
109
|
-
def cache
|
110
|
-
@cache ||= self.class.cache[self]
|
111
|
-
end
|
112
|
-
|
113
|
-
def default_locale
|
114
|
-
:en
|
115
|
-
end
|
116
|
-
end
|
117
|
-
end
|
118
|
-
end
|
119
|
-
end
|