dry-validation 0.6.0 → 0.7.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (83) hide show
  1. checksums.yaml +4 -4
  2. data/.codeclimate.yml +1 -0
  3. data/.travis.yml +3 -2
  4. data/CHANGELOG.md +42 -0
  5. data/Gemfile +8 -1
  6. data/README.md +13 -89
  7. data/config/errors.yml +35 -29
  8. data/dry-validation.gemspec +2 -2
  9. data/examples/basic.rb +3 -7
  10. data/examples/each.rb +3 -8
  11. data/examples/form.rb +3 -6
  12. data/examples/nested.rb +7 -15
  13. data/lib/dry/validation.rb +33 -5
  14. data/lib/dry/validation/error.rb +10 -26
  15. data/lib/dry/validation/error_compiler.rb +69 -99
  16. data/lib/dry/validation/error_compiler/input.rb +148 -0
  17. data/lib/dry/validation/hint_compiler.rb +83 -33
  18. data/lib/dry/validation/input_processor_compiler.rb +98 -0
  19. data/lib/dry/validation/input_processor_compiler/form.rb +46 -0
  20. data/lib/dry/validation/input_processor_compiler/sanitizer.rb +46 -0
  21. data/lib/dry/validation/messages/abstract.rb +30 -10
  22. data/lib/dry/validation/messages/i18n.rb +2 -1
  23. data/lib/dry/validation/messages/namespaced.rb +1 -0
  24. data/lib/dry/validation/messages/yaml.rb +8 -5
  25. data/lib/dry/validation/result.rb +33 -25
  26. data/lib/dry/validation/schema.rb +168 -61
  27. data/lib/dry/validation/schema/attr.rb +5 -27
  28. data/lib/dry/validation/schema/check.rb +24 -0
  29. data/lib/dry/validation/schema/dsl.rb +97 -0
  30. data/lib/dry/validation/schema/form.rb +2 -26
  31. data/lib/dry/validation/schema/key.rb +32 -28
  32. data/lib/dry/validation/schema/rule.rb +88 -32
  33. data/lib/dry/validation/schema/value.rb +77 -27
  34. data/lib/dry/validation/schema_compiler.rb +38 -0
  35. data/lib/dry/validation/version.rb +1 -1
  36. data/spec/fixtures/locales/pl.yml +1 -1
  37. data/spec/integration/attr_spec.rb +122 -0
  38. data/spec/integration/custom_error_messages_spec.rb +9 -11
  39. data/spec/integration/custom_predicates_spec.rb +68 -18
  40. data/spec/integration/error_compiler_spec.rb +259 -65
  41. data/spec/integration/hints_spec.rb +28 -9
  42. data/spec/integration/injecting_rules_spec.rb +11 -12
  43. data/spec/integration/localized_error_messages_spec.rb +16 -16
  44. data/spec/integration/messages/i18n_spec.rb +9 -5
  45. data/spec/integration/optional_keys_spec.rb +9 -11
  46. data/spec/integration/schema/array_schema_spec.rb +23 -0
  47. data/spec/integration/schema/check_rules_spec.rb +39 -31
  48. data/spec/integration/schema/check_with_nth_el_spec.rb +25 -0
  49. data/spec/integration/schema/each_with_set_spec.rb +23 -24
  50. data/spec/integration/schema/form_spec.rb +122 -0
  51. data/spec/integration/schema/inheriting_schema_spec.rb +31 -0
  52. data/spec/integration/schema/input_processor_spec.rb +46 -0
  53. data/spec/integration/schema/macros/confirmation_spec.rb +33 -0
  54. data/spec/integration/schema/macros/maybe_spec.rb +32 -0
  55. data/spec/integration/schema/macros/required_spec.rb +59 -0
  56. data/spec/integration/schema/macros/when_spec.rb +65 -0
  57. data/spec/integration/schema/nested_values_spec.rb +41 -0
  58. data/spec/integration/schema/not_spec.rb +14 -14
  59. data/spec/integration/schema/option_with_default_spec.rb +30 -0
  60. data/spec/integration/schema/reusing_schema_spec.rb +33 -0
  61. data/spec/integration/schema/using_types_spec.rb +29 -0
  62. data/spec/integration/schema/xor_spec.rb +17 -14
  63. data/spec/integration/schema_spec.rb +75 -245
  64. data/spec/shared/rule_compiler.rb +8 -0
  65. data/spec/spec_helper.rb +13 -0
  66. data/spec/unit/hint_compiler_spec.rb +10 -10
  67. data/spec/unit/{input_type_compiler_spec.rb → input_processor_compiler/form_spec.rb} +88 -73
  68. data/spec/unit/schema/key_spec.rb +33 -0
  69. data/spec/unit/schema/rule_spec.rb +7 -6
  70. data/spec/unit/schema/value_spec.rb +187 -54
  71. metadata +53 -31
  72. data/.rubocop.yml +0 -16
  73. data/.rubocop_todo.yml +0 -7
  74. data/lib/dry/validation/input_type_compiler.rb +0 -83
  75. data/lib/dry/validation/schema/definition.rb +0 -74
  76. data/lib/dry/validation/schema/result.rb +0 -68
  77. data/rakelib/rubocop.rake +0 -18
  78. data/spec/integration/rule_groups_spec.rb +0 -94
  79. data/spec/integration/schema/attrs_spec.rb +0 -38
  80. data/spec/integration/schema/default_key_behavior_spec.rb +0 -23
  81. data/spec/integration/schema/grouped_rules_spec.rb +0 -57
  82. data/spec/integration/schema/nested_spec.rb +0 -31
  83. data/spec/integration/schema_form_spec.rb +0 -97
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.6.0
4
+ version: 0.7.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-01-20 00:00:00.000000000 Z
12
+ date: 2016-03-16 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: dry-configurable
@@ -71,40 +71,40 @@ dependencies:
71
71
  requirements:
72
72
  - - "~>"
73
73
  - !ruby/object:Gem::Version
74
- version: '0.1'
74
+ version: '0.2'
75
75
  - - ">="
76
76
  - !ruby/object:Gem::Version
77
- version: 0.1.2
77
+ version: 0.2.0
78
78
  type: :runtime
79
79
  prerelease: false
80
80
  version_requirements: !ruby/object:Gem::Requirement
81
81
  requirements:
82
82
  - - "~>"
83
83
  - !ruby/object:Gem::Version
84
- version: '0.1'
84
+ version: '0.2'
85
85
  - - ">="
86
86
  - !ruby/object:Gem::Version
87
- version: 0.1.2
87
+ version: 0.2.0
88
88
  - !ruby/object:Gem::Dependency
89
- name: dry-data
89
+ name: dry-types
90
90
  requirement: !ruby/object:Gem::Requirement
91
91
  requirements:
92
92
  - - "~>"
93
93
  - !ruby/object:Gem::Version
94
- version: '0.5'
94
+ version: '0.6'
95
95
  - - ">="
96
96
  - !ruby/object:Gem::Version
97
- version: 0.5.0
97
+ version: 0.6.0
98
98
  type: :runtime
99
99
  prerelease: false
100
100
  version_requirements: !ruby/object:Gem::Requirement
101
101
  requirements:
102
102
  - - "~>"
103
103
  - !ruby/object:Gem::Version
104
- version: '0.5'
104
+ version: '0.6'
105
105
  - - ">="
106
106
  - !ruby/object:Gem::Version
107
- version: 0.5.0
107
+ version: 0.6.0
108
108
  - !ruby/object:Gem::Dependency
109
109
  name: bundler
110
110
  requirement: !ruby/object:Gem::Requirement
@@ -158,8 +158,6 @@ files:
158
158
  - ".codeclimate.yml"
159
159
  - ".gitignore"
160
160
  - ".rspec"
161
- - ".rubocop.yml"
162
- - ".rubocop_todo.yml"
163
161
  - ".travis.yml"
164
162
  - CHANGELOG.md
165
163
  - Gemfile
@@ -177,8 +175,11 @@ files:
177
175
  - lib/dry/validation.rb
178
176
  - lib/dry/validation/error.rb
179
177
  - lib/dry/validation/error_compiler.rb
178
+ - lib/dry/validation/error_compiler/input.rb
180
179
  - lib/dry/validation/hint_compiler.rb
181
- - lib/dry/validation/input_type_compiler.rb
180
+ - lib/dry/validation/input_processor_compiler.rb
181
+ - lib/dry/validation/input_processor_compiler/form.rb
182
+ - lib/dry/validation/input_processor_compiler/sanitizer.rb
182
183
  - lib/dry/validation/messages.rb
183
184
  - lib/dry/validation/messages/abstract.rb
184
185
  - lib/dry/validation/messages/i18n.rb
@@ -187,16 +188,17 @@ files:
187
188
  - lib/dry/validation/result.rb
188
189
  - lib/dry/validation/schema.rb
189
190
  - lib/dry/validation/schema/attr.rb
190
- - lib/dry/validation/schema/definition.rb
191
+ - lib/dry/validation/schema/check.rb
192
+ - lib/dry/validation/schema/dsl.rb
191
193
  - lib/dry/validation/schema/form.rb
192
194
  - lib/dry/validation/schema/key.rb
193
- - lib/dry/validation/schema/result.rb
194
195
  - lib/dry/validation/schema/rule.rb
195
196
  - lib/dry/validation/schema/value.rb
197
+ - lib/dry/validation/schema_compiler.rb
196
198
  - lib/dry/validation/version.rb
197
- - rakelib/rubocop.rake
198
199
  - spec/fixtures/locales/en.yml
199
200
  - spec/fixtures/locales/pl.yml
201
+ - spec/integration/attr_spec.rb
200
202
  - spec/integration/custom_error_messages_spec.rb
201
203
  - spec/integration/custom_predicates_spec.rb
202
204
  - spec/integration/error_compiler_spec.rb
@@ -205,22 +207,31 @@ files:
205
207
  - spec/integration/localized_error_messages_spec.rb
206
208
  - spec/integration/messages/i18n_spec.rb
207
209
  - spec/integration/optional_keys_spec.rb
208
- - spec/integration/rule_groups_spec.rb
209
- - spec/integration/schema/attrs_spec.rb
210
+ - spec/integration/schema/array_schema_spec.rb
210
211
  - spec/integration/schema/check_rules_spec.rb
211
- - spec/integration/schema/default_key_behavior_spec.rb
212
+ - spec/integration/schema/check_with_nth_el_spec.rb
212
213
  - spec/integration/schema/each_with_set_spec.rb
213
- - spec/integration/schema/grouped_rules_spec.rb
214
- - spec/integration/schema/nested_spec.rb
214
+ - spec/integration/schema/form_spec.rb
215
+ - spec/integration/schema/inheriting_schema_spec.rb
216
+ - spec/integration/schema/input_processor_spec.rb
217
+ - spec/integration/schema/macros/confirmation_spec.rb
218
+ - spec/integration/schema/macros/maybe_spec.rb
219
+ - spec/integration/schema/macros/required_spec.rb
220
+ - spec/integration/schema/macros/when_spec.rb
221
+ - spec/integration/schema/nested_values_spec.rb
215
222
  - spec/integration/schema/not_spec.rb
223
+ - spec/integration/schema/option_with_default_spec.rb
224
+ - spec/integration/schema/reusing_schema_spec.rb
225
+ - spec/integration/schema/using_types_spec.rb
216
226
  - spec/integration/schema/xor_spec.rb
217
- - spec/integration/schema_form_spec.rb
218
227
  - spec/integration/schema_spec.rb
228
+ - spec/shared/rule_compiler.rb
219
229
  - spec/spec_helper.rb
220
230
  - spec/support/define_struct.rb
221
231
  - spec/unit/error_compiler_spec.rb
222
232
  - spec/unit/hint_compiler_spec.rb
223
- - spec/unit/input_type_compiler_spec.rb
233
+ - spec/unit/input_processor_compiler/form_spec.rb
234
+ - spec/unit/schema/key_spec.rb
224
235
  - spec/unit/schema/rule_spec.rb
225
236
  - spec/unit/schema/value_spec.rb
226
237
  - spec/unit/schema_spec.rb
@@ -251,6 +262,7 @@ summary: A simple validation library
251
262
  test_files:
252
263
  - spec/fixtures/locales/en.yml
253
264
  - spec/fixtures/locales/pl.yml
265
+ - spec/integration/attr_spec.rb
254
266
  - spec/integration/custom_error_messages_spec.rb
255
267
  - spec/integration/custom_predicates_spec.rb
256
268
  - spec/integration/error_compiler_spec.rb
@@ -259,22 +271,32 @@ test_files:
259
271
  - spec/integration/localized_error_messages_spec.rb
260
272
  - spec/integration/messages/i18n_spec.rb
261
273
  - spec/integration/optional_keys_spec.rb
262
- - spec/integration/rule_groups_spec.rb
263
- - spec/integration/schema/attrs_spec.rb
274
+ - spec/integration/schema/array_schema_spec.rb
264
275
  - spec/integration/schema/check_rules_spec.rb
265
- - spec/integration/schema/default_key_behavior_spec.rb
276
+ - spec/integration/schema/check_with_nth_el_spec.rb
266
277
  - spec/integration/schema/each_with_set_spec.rb
267
- - spec/integration/schema/grouped_rules_spec.rb
268
- - spec/integration/schema/nested_spec.rb
278
+ - spec/integration/schema/form_spec.rb
279
+ - spec/integration/schema/inheriting_schema_spec.rb
280
+ - spec/integration/schema/input_processor_spec.rb
281
+ - spec/integration/schema/macros/confirmation_spec.rb
282
+ - spec/integration/schema/macros/maybe_spec.rb
283
+ - spec/integration/schema/macros/required_spec.rb
284
+ - spec/integration/schema/macros/when_spec.rb
285
+ - spec/integration/schema/nested_values_spec.rb
269
286
  - spec/integration/schema/not_spec.rb
287
+ - spec/integration/schema/option_with_default_spec.rb
288
+ - spec/integration/schema/reusing_schema_spec.rb
289
+ - spec/integration/schema/using_types_spec.rb
270
290
  - spec/integration/schema/xor_spec.rb
271
- - spec/integration/schema_form_spec.rb
272
291
  - spec/integration/schema_spec.rb
292
+ - spec/shared/rule_compiler.rb
273
293
  - spec/spec_helper.rb
274
294
  - spec/support/define_struct.rb
275
295
  - spec/unit/error_compiler_spec.rb
276
296
  - spec/unit/hint_compiler_spec.rb
277
- - spec/unit/input_type_compiler_spec.rb
297
+ - spec/unit/input_processor_compiler/form_spec.rb
298
+ - spec/unit/schema/key_spec.rb
278
299
  - spec/unit/schema/rule_spec.rb
279
300
  - spec/unit/schema/value_spec.rb
280
301
  - spec/unit/schema_spec.rb
302
+ has_rdoc:
@@ -1,16 +0,0 @@
1
- # Generated by `rubocop --auto-gen-config`
2
- inherit_from: .rubocop_todo.yml
3
-
4
- Metrics/LineLength:
5
- Max: 100
6
-
7
- Style/Documentation:
8
- Enabled: false
9
-
10
- Lint/HandleExceptions:
11
- Exclude:
12
- - rakelib/*.rake
13
-
14
- Style/FileName:
15
- Exclude:
16
- - lib/dry-validation.rb
@@ -1,7 +0,0 @@
1
- # This configuration was generated by
2
- # `rubocop --auto-gen-config`
3
- # on 2015-10-30 01:32:46 +0000 using RuboCop version 0.34.2.
4
- # The point is for the user to remove these configuration records
5
- # one by one as the offenses are removed from the code base.
6
- # Note that changes in the inspected code, or installation of new
7
- # versions of RuboCop, may require this file to be generated again.
@@ -1,83 +0,0 @@
1
- require 'dry/data'
2
- require 'dry/data/compiler'
3
-
4
- module Dry
5
- module Validation
6
- class InputTypeCompiler
7
- attr_reader :type_compiler
8
-
9
- TYPES = {
10
- default: 'string',
11
- none?: 'form.nil',
12
- bool?: 'form.bool',
13
- str?: 'string',
14
- int?: 'form.int',
15
- float?: 'form.float',
16
- decimal?: 'form.decimal',
17
- date?: 'form.date',
18
- date_time?: 'form.date_time',
19
- time?: 'form.time'
20
- }.freeze
21
-
22
- DEFAULT_TYPE_NODE = [[:type, 'string']].freeze
23
-
24
- def initialize
25
- @type_compiler = Dry::Data::Compiler.new(Dry::Data)
26
- end
27
-
28
- def call(ast)
29
- schema = ast.map { |node| visit(node) }
30
- type_compiler.([:type, ['hash', [:symbolized, schema]]])
31
- end
32
-
33
- def visit(node, *args)
34
- send(:"visit_#{node[0]}", node[1], *args)
35
- end
36
-
37
- def visit_or(node, *args)
38
- left, right = node
39
- [:sum, [visit(left, *args), visit(right, *args)]]
40
- end
41
-
42
- def visit_and(node, first = true)
43
- if first
44
- name, type = node.map { |n| visit(n, false) }.uniq
45
- [:key, [name, type]]
46
- else
47
- result = node.map { |n| visit(n, first) }.uniq
48
-
49
- if result.size == 1
50
- result.first
51
- else
52
- (result - DEFAULT_TYPE_NODE).first
53
- end
54
- end
55
- end
56
-
57
- def visit_implication(node)
58
- key, types = node
59
- [:key, [visit(key), visit(types, false)]]
60
- end
61
-
62
- def visit_key(node, *args)
63
- node[0].to_s
64
- end
65
-
66
- def visit_val(node, *args)
67
- visit(node[1], false)
68
- end
69
-
70
- def visit_set(node, *args)
71
- [:type, ['form.hash', [:symbolized, node[1].map { |n| visit(n) }]]]
72
- end
73
-
74
- def visit_each(node, *args)
75
- [:type, ['form.array', visit(node[1], *args)]]
76
- end
77
-
78
- def visit_predicate(node, *args)
79
- [:type, TYPES[node[0]] || TYPES[:default]]
80
- end
81
- end
82
- end
83
- end
@@ -1,74 +0,0 @@
1
- module Dry
2
- module Validation
3
- class Schema
4
- module Definition
5
- def schema(name, &block)
6
- schema = Class.new(superclass)
7
- schema.key(name, &block)
8
- schemas << schema
9
- self
10
- end
11
-
12
- def key(name, &block)
13
- Key.new(name, rules).key?(&block)
14
- end
15
-
16
- def attr(name, &block)
17
- Attr.new(name, rules).attr?(&block)
18
- end
19
-
20
- def optional(name, &block)
21
- Key.new(name, rules).optional(&block)
22
- end
23
-
24
- def value(name)
25
- Schema::Rule::Result.new(name, [])
26
- end
27
-
28
- def rule(name, **options, &block)
29
- if options.any?
30
- predicate, rule_names = options.to_a.first
31
- identifier = { name => rule_names }
32
-
33
- groups << [:group, [identifier, [:predicate, predicate]]]
34
- else
35
- if block
36
- checks << Schema::Rule.new(name, [:check, [name, yield.to_ary]])
37
- else
38
- rule_by_name(name).to_check
39
- end
40
- end
41
- end
42
-
43
- def confirmation(name, options = {})
44
- conf_name = :"#{name}_confirmation"
45
-
46
- unless rule_by_name(name)
47
- if options.any?
48
- key(name) do |value|
49
- options.map { |p, args| value.__send__(:"#{p}?", *args) }.reduce(:&)
50
- end
51
- else
52
- key(name, &:filled?)
53
- end
54
- end
55
-
56
- key(conf_name, &:filled?)
57
-
58
- rule(conf_name, eql?: [name, conf_name])
59
- end
60
-
61
- private
62
-
63
- def rule_by_name(name)
64
- rules.detect { |rule| rule.name == name }
65
- end
66
- end
67
- end
68
- end
69
- end
70
-
71
- require 'dry/validation/schema/rule'
72
- require 'dry/validation/schema/value'
73
- require 'dry/validation/schema/key'
74
- require 'dry/validation/schema/attr'
@@ -1,68 +0,0 @@
1
- module Dry
2
- module Validation
3
- class Schema::Result
4
- include Dry::Equalizer(:params, :messages)
5
- include Enumerable
6
-
7
- attr_reader :params
8
-
9
- attr_reader :result
10
-
11
- attr_reader :errors
12
-
13
- attr_reader :error_compiler
14
-
15
- attr_reader :hint_compiler
16
-
17
- def initialize(params, result, errors, error_compiler, hint_compiler)
18
- @params = params
19
- @result = result
20
- @errors = errors
21
- @error_compiler = error_compiler
22
- @hint_compiler = hint_compiler
23
- end
24
-
25
- def each(&block)
26
- failures.each(&block)
27
- end
28
-
29
- def empty?
30
- errors.empty?
31
- end
32
-
33
- def to_ary
34
- errors.map(&:to_ary)
35
- end
36
-
37
- def messages(options = {})
38
- @messages ||=
39
- begin
40
- all_msgs = error_compiler.with(options).(errors.map(&:to_ary))
41
- hints = hint_compiler.with(options).call
42
-
43
- msgs_data = all_msgs.map do |(name, data)|
44
- msgs, input =
45
- if data.is_a?(Hash)
46
- values = data.values
47
- [values.map(&:first).flatten.concat(hints[name]).uniq, values[0][1]]
48
- else
49
- [data[0].concat(hints[name]).uniq.flatten, data[1]]
50
- end
51
-
52
- [name, [msgs, input]]
53
- end
54
-
55
- Hash[msgs_data]
56
- end
57
- end
58
-
59
- def successes
60
- result.successes
61
- end
62
-
63
- def failures
64
- result.failures
65
- end
66
- end
67
- end
68
- end
@@ -1,18 +0,0 @@
1
- begin
2
- require 'rubocop/rake_task'
3
-
4
- Rake::Task[:default].enhance [:rubocop]
5
-
6
- RuboCop::RakeTask.new do |task|
7
- task.options << '--display-cop-names'
8
- end
9
-
10
- namespace :rubocop do
11
- desc 'Generate a configuration file acting as a TODO list.'
12
- task :auto_gen_config do
13
- exec 'bundle exec rubocop --auto-gen-config'
14
- end
15
- end
16
-
17
- rescue LoadError
18
- end