dry-validation 0.8.0 → 0.9.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (53) hide show
  1. checksums.yaml +4 -4
  2. data/.travis.yml +1 -1
  3. data/CHANGELOG.md +39 -1
  4. data/benchmarks/benchmark_schema_invalid_huge.rb +52 -0
  5. data/benchmarks/profile_schema_huge_invalid.rb +30 -0
  6. data/config/errors.yml +3 -2
  7. data/dry-validation.gemspec +2 -2
  8. data/lib/dry/validation.rb +20 -32
  9. data/lib/dry/validation/constants.rb +6 -0
  10. data/lib/dry/validation/error.rb +5 -2
  11. data/lib/dry/validation/error_compiler.rb +46 -116
  12. data/lib/dry/validation/executor.rb +105 -0
  13. data/lib/dry/validation/hint_compiler.rb +36 -68
  14. data/lib/dry/validation/message.rb +86 -0
  15. data/lib/dry/validation/message_compiler.rb +141 -0
  16. data/lib/dry/validation/message_set.rb +70 -0
  17. data/lib/dry/validation/messages/abstract.rb +1 -1
  18. data/lib/dry/validation/messages/i18n.rb +5 -0
  19. data/lib/dry/validation/predicate_registry.rb +8 -3
  20. data/lib/dry/validation/result.rb +6 -7
  21. data/lib/dry/validation/schema.rb +21 -227
  22. data/lib/dry/validation/schema/check.rb +1 -1
  23. data/lib/dry/validation/schema/class_interface.rb +193 -0
  24. data/lib/dry/validation/schema/deprecated.rb +1 -2
  25. data/lib/dry/validation/schema/key.rb +4 -0
  26. data/lib/dry/validation/schema/value.rb +12 -7
  27. data/lib/dry/validation/schema_compiler.rb +20 -1
  28. data/lib/dry/validation/type_specs.rb +70 -0
  29. data/lib/dry/validation/version.rb +1 -1
  30. data/spec/fixtures/locales/pl.yml +1 -1
  31. data/spec/integration/custom_predicates_spec.rb +37 -0
  32. data/spec/integration/error_compiler_spec.rb +39 -39
  33. data/spec/integration/form/predicates/key_spec.rb +10 -18
  34. data/spec/integration/form/predicates/size/fixed_spec.rb +8 -12
  35. data/spec/integration/form/predicates/size/range_spec.rb +7 -7
  36. data/spec/integration/hints_spec.rb +17 -0
  37. data/spec/integration/messages/i18n_spec.rb +2 -2
  38. data/spec/integration/schema/check_rules_spec.rb +2 -2
  39. data/spec/integration/schema/defining_base_schema_spec.rb +38 -0
  40. data/spec/integration/schema/dynamic_predicate_args_spec.rb +18 -0
  41. data/spec/integration/schema/macros/each_spec.rb +2 -2
  42. data/spec/integration/schema/macros/input_spec.rb +102 -10
  43. data/spec/integration/schema/macros/maybe_spec.rb +30 -0
  44. data/spec/integration/schema/nested_schemas_spec.rb +200 -0
  45. data/spec/integration/schema/nested_values_spec.rb +3 -1
  46. data/spec/integration/schema/option_with_default_spec.rb +54 -20
  47. data/spec/integration/schema/predicates/size/fixed_spec.rb +10 -10
  48. data/spec/integration/schema/predicates/size/range_spec.rb +8 -10
  49. data/spec/unit/error_compiler_spec.rb +1 -1
  50. data/spec/unit/hint_compiler_spec.rb +2 -2
  51. metadata +18 -7
  52. data/examples/rule_ast.rb +0 -25
  53. data/lib/dry/validation/error_compiler/input.rb +0 -135
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dry-validation
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.8.0
4
+ version: 0.9.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andy Holland
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2016-07-01 00:00:00.000000000 Z
12
+ date: 2016-07-08 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: concurrent-ruby
@@ -165,8 +165,7 @@ description:
165
165
  email:
166
166
  - andyholland1991@aol.com
167
167
  - piotr.solnica@gmail.com
168
- executables:
169
- - console
168
+ executables: []
170
169
  extensions: []
171
170
  extra_rdoc_files: []
172
171
  files:
@@ -181,12 +180,13 @@ files:
181
180
  - Rakefile
182
181
  - benchmarks/benchmark_form_invalid.rb
183
182
  - benchmarks/benchmark_form_valid.rb
183
+ - benchmarks/benchmark_schema_invalid_huge.rb
184
184
  - benchmarks/profile_schema_call_invalid.rb
185
185
  - benchmarks/profile_schema_call_valid.rb
186
186
  - benchmarks/profile_schema_definition.rb
187
+ - benchmarks/profile_schema_huge_invalid.rb
187
188
  - benchmarks/profile_schema_messages_invalid.rb
188
189
  - benchmarks/suite.rb
189
- - bin/console
190
190
  - config/errors.yml
191
191
  - dry-validation.gemspec
192
192
  - examples/basic.rb
@@ -194,18 +194,21 @@ files:
194
194
  - examples/form.rb
195
195
  - examples/json.rb
196
196
  - examples/nested.rb
197
- - examples/rule_ast.rb
198
197
  - lib/dry-validation.rb
199
198
  - lib/dry/validation.rb
199
+ - lib/dry/validation/constants.rb
200
200
  - lib/dry/validation/deprecations.rb
201
201
  - lib/dry/validation/error.rb
202
202
  - lib/dry/validation/error_compiler.rb
203
- - lib/dry/validation/error_compiler/input.rb
203
+ - lib/dry/validation/executor.rb
204
204
  - lib/dry/validation/hint_compiler.rb
205
205
  - lib/dry/validation/input_processor_compiler.rb
206
206
  - lib/dry/validation/input_processor_compiler/form.rb
207
207
  - lib/dry/validation/input_processor_compiler/json.rb
208
208
  - lib/dry/validation/input_processor_compiler/sanitizer.rb
209
+ - lib/dry/validation/message.rb
210
+ - lib/dry/validation/message_compiler.rb
211
+ - lib/dry/validation/message_set.rb
209
212
  - lib/dry/validation/messages.rb
210
213
  - lib/dry/validation/messages/abstract.rb
211
214
  - lib/dry/validation/messages/i18n.rb
@@ -215,6 +218,7 @@ files:
215
218
  - lib/dry/validation/result.rb
216
219
  - lib/dry/validation/schema.rb
217
220
  - lib/dry/validation/schema/check.rb
221
+ - lib/dry/validation/schema/class_interface.rb
218
222
  - lib/dry/validation/schema/deprecated.rb
219
223
  - lib/dry/validation/schema/dsl.rb
220
224
  - lib/dry/validation/schema/form.rb
@@ -223,6 +227,7 @@ files:
223
227
  - lib/dry/validation/schema/rule.rb
224
228
  - lib/dry/validation/schema/value.rb
225
229
  - lib/dry/validation/schema_compiler.rb
230
+ - lib/dry/validation/type_specs.rb
226
231
  - lib/dry/validation/version.rb
227
232
  - log/.gitkeep
228
233
  - spec/fixtures/locales/en.yml
@@ -264,6 +269,8 @@ files:
264
269
  - spec/integration/schema/check_rules_spec.rb
265
270
  - spec/integration/schema/check_with_nested_el_spec.rb
266
271
  - spec/integration/schema/check_with_nth_el_spec.rb
272
+ - spec/integration/schema/defining_base_schema_spec.rb
273
+ - spec/integration/schema/dynamic_predicate_args_spec.rb
267
274
  - spec/integration/schema/each_with_set_spec.rb
268
275
  - spec/integration/schema/extending_dsl_spec.rb
269
276
  - spec/integration/schema/form/explicit_types_spec.rb
@@ -280,6 +287,7 @@ files:
280
287
  - spec/integration/schema/macros/maybe_spec.rb
281
288
  - spec/integration/schema/macros/value_spec.rb
282
289
  - spec/integration/schema/macros/when_spec.rb
290
+ - spec/integration/schema/nested_schemas_spec.rb
283
291
  - spec/integration/schema/nested_values_spec.rb
284
292
  - spec/integration/schema/not_spec.rb
285
293
  - spec/integration/schema/numbers_spec.rb
@@ -394,6 +402,8 @@ test_files:
394
402
  - spec/integration/schema/check_rules_spec.rb
395
403
  - spec/integration/schema/check_with_nested_el_spec.rb
396
404
  - spec/integration/schema/check_with_nth_el_spec.rb
405
+ - spec/integration/schema/defining_base_schema_spec.rb
406
+ - spec/integration/schema/dynamic_predicate_args_spec.rb
397
407
  - spec/integration/schema/each_with_set_spec.rb
398
408
  - spec/integration/schema/extending_dsl_spec.rb
399
409
  - spec/integration/schema/form/explicit_types_spec.rb
@@ -410,6 +420,7 @@ test_files:
410
420
  - spec/integration/schema/macros/maybe_spec.rb
411
421
  - spec/integration/schema/macros/value_spec.rb
412
422
  - spec/integration/schema/macros/when_spec.rb
423
+ - spec/integration/schema/nested_schemas_spec.rb
413
424
  - spec/integration/schema/nested_values_spec.rb
414
425
  - spec/integration/schema/not_spec.rb
415
426
  - spec/integration/schema/numbers_spec.rb
@@ -1,25 +0,0 @@
1
- require 'dry-validation'
2
-
3
- ast = [
4
- [
5
- :and,
6
- [
7
- [:key, [:age, [:predicate, [:key?, []]]]],
8
- [
9
- :and,
10
- [
11
- [:val, [:age, [:predicate, [:filled?, []]]]],
12
- [:val, [:age, [:predicate, [:gt?, [18]]]]]
13
- ]
14
- ]
15
- ]
16
- ]
17
- ]
18
-
19
- compiler = Dry::Validation::RuleCompiler.new(Dry::Validation::Predicates)
20
-
21
- rules = compiler.call(ast)
22
-
23
- puts rules.inspect
24
-
25
- puts rules.map(&:to_ary).inspect
@@ -1,135 +0,0 @@
1
- require 'dry/validation/deprecations'
2
-
3
- module Dry
4
- module Validation
5
- Message = Struct.new(:rule, :predicate, :text) do
6
- def to_s
7
- text
8
- end
9
-
10
- def signature
11
- @signature ||= [rule, predicate].hash
12
- end
13
-
14
- def eql?(other)
15
- other.is_a?(String) ? text == other : super
16
- end
17
- end
18
-
19
- class ErrorCompiler::Input < ErrorCompiler
20
- include Deprecations
21
-
22
- attr_reader :name, :input, :rule, :val_type
23
-
24
- def initialize(messages, options)
25
- super
26
- @name = options.fetch(:name)
27
- @input = options.fetch(:input)
28
- @rule = Array(name).last
29
- @val_type = input.class
30
- end
31
-
32
- def visit_each(node)
33
- node.map { |el| visit(el) }
34
- end
35
-
36
- def visit_set(node, *)
37
- result = node.map do |input|
38
- visit(input)
39
- end
40
- merge(result)
41
- end
42
-
43
- def visit_el(node)
44
- idx, el = node
45
- path = [*Array(name), idx]
46
- input_visitor(path, input[idx]).visit(el)
47
- end
48
-
49
- def visit_check(node)
50
- _, other = node
51
- visit(other)
52
- end
53
-
54
- def visit_predicate(node)
55
- predicate, args = node
56
-
57
- lookup_options = options.merge(
58
- rule: rule, val_type: val_type, arg_type: args.size > 0 && args[0][1].class
59
- )
60
-
61
- tokens = options_for(predicate, args)
62
- template = messages[predicate, lookup_options.merge(tokens)]
63
-
64
- unless template
65
- raise MissingMessageError.new("message for #{predicate} was not found")
66
- end
67
-
68
- rule_name =
69
- if rule.is_a?(Symbol)
70
- messages.rule(rule, lookup_options) || rule
71
- else
72
- rule
73
- end
74
-
75
- text =
76
- if full?
77
- "#{rule_name || tokens[:name]} #{template % tokens}"
78
- else
79
- template % tokens
80
- end
81
-
82
- *arg_vals, _ = args.map(&:last)
83
- message = Message.new(rule, [predicate, arg_vals], text)
84
- path = [[message], *[tokens[:name], *Array(name).reverse].uniq]
85
-
86
- path.reduce { |a, e| { e => a } }
87
- end
88
-
89
- def options_for_inclusion?(args)
90
- warn 'inclusion is deprecated - use included_in instead.'
91
- options_for_included_in?(args)
92
- end
93
-
94
- def options_for_exclusion?(args)
95
- warn 'exclusion is deprecated - use excluded_from instead.'
96
- options_for_excluded_from?(args)
97
- end
98
-
99
- def options_for_excluded_from?(args)
100
- { list: args[:list].join(', ') }
101
- end
102
-
103
- def options_for_included_in?(args)
104
- { list: args[:list].join(', ') }
105
- end
106
-
107
- def options_for_size?(args)
108
- size = args[:size]
109
-
110
- if size.is_a?(Range)
111
- { left: size.first, right: size.last }
112
- else
113
- args
114
- end
115
- end
116
-
117
- def options_for(predicate, args)
118
- meth = :"options_for_#{predicate}"
119
-
120
- args_map = Hash[args]
121
- defaults = { name: rule, rule: rule, value: input }.update(args_map)
122
-
123
- if respond_to?(meth)
124
- defaults.merge!(__send__(meth, args_map))
125
- end
126
-
127
- defaults
128
- end
129
-
130
- def input_visitor(new_name, value)
131
- self.class.new(messages, options.merge(name: [*name, *new_name].uniq, input: value))
132
- end
133
- end
134
- end
135
- end