dry-validation 0.7.0 → 0.7.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 8cb2b0cdd55dfea0aeab8060f8628f55b304c584
4
- data.tar.gz: ab8689f62590fe6029e427a9563e9da5a1eafe7d
3
+ metadata.gz: 02681fc0853b4f66dc7951e078dd5ef37559cbe6
4
+ data.tar.gz: 214d78242cec3b96d1ad065b403596c6876491c8
5
5
  SHA512:
6
- metadata.gz: 35a95e790addbea7fbabd6d60c0ac130db75f30248c0ff7f51583bb893d7422c8fd2b60e6dcb462d5fb7a242e0980dede8cdc6df1076667e60110e69babcc081
7
- data.tar.gz: 55cd8947019a5326256afca381c58538c6c272874ecf34ae942809dc5af3ba5d1cb294864701b0ce415b23d2a82ae7765242e8c1bcd79b8b1c20233d0ba670a7
6
+ metadata.gz: 1d2b0bd2955cec709349b1060913eb6bfee39e55d3b9939ddff63def27a5aaa6360a1599f1afb8442dfe0b2a4326a61adcd2af6b2e4e91c2c928d5f419a7cd9f
7
+ data.tar.gz: f17b2305e70319088bf4028354640eb18c37b1d88ab2ad2d29cbf2b504704a7cec594ee0115ba334b96ccfcc02b36e9db466498d6934ece8ff743884582885d8
@@ -1,3 +1,21 @@
1
+ # v0.7.1 2016-03-21
2
+
3
+ ### Added
4
+
5
+ * You can use `schema` inside `each` macro (solnic)
6
+
7
+ ### Fixed
8
+
9
+ * `confirmation` macro defines an optional key with maybe value with `_confirmation` suffix (solnic)
10
+ * `each` macro works correctly when its inner rule specify just one key (solnic)
11
+ * error messages for `each` rules where input is equal are now correctly generated (solnic)
12
+
13
+ ### Changed
14
+
15
+ * Now depends on `dry-logic` >= `0.2.1` (solnic)
16
+
17
+ [Compare v0.7.0...v0.7.1](https://github.com/dryrb/dry-validation/compare/v0.7.0...v0.7.1)
18
+
1
19
  # v0.7.0 2016-03-16
2
20
 
3
21
  ### Added
@@ -18,7 +18,7 @@ Gem::Specification.new do |spec|
18
18
  spec.add_runtime_dependency 'dry-configurable', '~> 0.1', '>= 0.1.3'
19
19
  spec.add_runtime_dependency 'dry-container', '~> 0.2', '>= 0.2.8'
20
20
  spec.add_runtime_dependency 'dry-equalizer', '~> 0.2'
21
- spec.add_runtime_dependency 'dry-logic', '~> 0.2', '>= 0.2.0'
21
+ spec.add_runtime_dependency 'dry-logic', '~> 0.2', '>= 0.2.1'
22
22
  spec.add_runtime_dependency 'dry-types', '~> 0.6', '>= 0.6.0'
23
23
 
24
24
  spec.add_development_dependency 'bundler'
@@ -45,6 +45,20 @@ module Dry
45
45
  super(rules, default_options.merge(options))
46
46
  end
47
47
 
48
+ def self.create_class(target, other = nil, &block)
49
+ klass =
50
+ if other
51
+ Class.new(other.class)
52
+ else
53
+ Validation.Schema(target.schema_class, parent: target, build: false, &block)
54
+ end
55
+
56
+ klass.config.path = [target.name] if other
57
+ klass.config.input_processor = :noop
58
+
59
+ klass
60
+ end
61
+
48
62
  def self.option(name, default = nil)
49
63
  attr_reader(*name)
50
64
  options.update(name => default)
@@ -55,6 +55,10 @@ module Dry
55
55
  create_rule(to_ast)
56
56
  end
57
57
 
58
+ def rule_ast
59
+ rules.map(&:to_ast)
60
+ end
61
+
58
62
  def path
59
63
  items = [parent && parent.path, name].flatten.compact.uniq
60
64
  items.size == 1 ? items[0] : items
@@ -14,15 +14,8 @@ module Dry
14
14
  end
15
15
 
16
16
  def schema(other = nil, &block)
17
- schema = other ? ::Class.new(other.class) : Validation.Schema(
18
- target.schema_class, parent: target, build: false, &block
19
- )
20
-
21
- schema.config.path = [name] if other
22
- schema.config.input_processor = :noop
23
-
17
+ schema = Schema.create_class(target, other, &block)
24
18
  rule = __send__(type, key(:hash?).and(key(schema)))
25
-
26
19
  add_rule(rule)
27
20
  end
28
21
 
@@ -60,6 +53,10 @@ module Dry
60
53
  target.checks
61
54
  end
62
55
 
56
+ def rule_ast
57
+ rules.size > 0 ? target.rule_ast : [to_ast]
58
+ end
59
+
63
60
  def to_ast
64
61
  if deps.empty?
65
62
  node
@@ -26,16 +26,27 @@ module Dry
26
26
  Value
27
27
  end
28
28
 
29
+ def schema(other = nil, &block)
30
+ schema = Schema.create_class(self, other, &block)
31
+ hash?.and(schema)
32
+ end
33
+
29
34
  def each(*predicates, &block)
30
- val =
35
+ left = array?
36
+
37
+ right =
31
38
  if predicates.size > 0
32
- predicates
39
+ inferred = predicates
33
40
  .reduce(Value.new) { |a, e| a.__send__(*::Kernel.Array(e)) }
41
+
42
+ create_rule([:each, inferred.to_ast])
34
43
  else
35
- Value[name].instance_eval(&block)
44
+ val = Value[name].instance_eval(&block)
45
+
46
+ create_rule([:each, [:set, val.rule_ast]])
36
47
  end
37
48
 
38
- rule = array?.and(create_rule([:each, val.to_ast]))
49
+ rule = left.and(right)
39
50
 
40
51
  add_rule(rule) if root?
41
52
 
@@ -68,11 +79,11 @@ module Dry
68
79
  end
69
80
 
70
81
  def confirmation
71
- rule = check(:"#{name}_confirmation")
72
- .eql?(check(name))
73
- .with(deps: [name])
82
+ conf = :"#{name}_confirmation"
83
+
84
+ parent.optional(conf).maybe
74
85
 
75
- add_check(rule)
86
+ rule(conf => [conf, name]) { |left, right| left.eql?(right) }
76
87
  end
77
88
 
78
89
  def value(name)
@@ -1,5 +1,5 @@
1
1
  module Dry
2
2
  module Validation
3
- VERSION = '0.7.0'.freeze
3
+ VERSION = '0.7.1'.freeze
4
4
  end
5
5
  end
@@ -3,6 +3,8 @@ RSpec.describe 'Macros #confirmation' do
3
3
  subject(:schema) do
4
4
  Dry::Validation.Schema do
5
5
  configure do
6
+ config.input_processor = :sanitizer
7
+
6
8
  def self.messages
7
9
  Messages.default.merge(
8
10
  en: { errors: { password_confirmation: 'does not match' } }
@@ -19,7 +21,7 @@ RSpec.describe 'Macros #confirmation' do
19
21
  end
20
22
 
21
23
  it 'fails when source value is invalid' do
22
- expect(schema.(password: 'fo', password_confirmation: '').messages).to eql(
24
+ expect(schema.(password: 'fo', password_confirmation: nil).messages).to eql(
23
25
  password: ['size cannot be less than 3']
24
26
  )
25
27
  end
@@ -0,0 +1,106 @@
1
+ RSpec.describe 'Macros #each' do
2
+ context 'with a block' do
3
+ context 'with a nested key' do
4
+ subject(:schema) do
5
+ Dry::Validation.Schema do
6
+ key(:songs).each do
7
+ key(:title).required
8
+ end
9
+ end
10
+ end
11
+
12
+ it 'passes when all elements are valid' do
13
+ songs = [{ title: 'Hello' }, { title: 'World' }]
14
+
15
+ expect(schema.(songs: songs)).to be_success
16
+ end
17
+
18
+ it 'fails when value is not an array' do
19
+ expect(schema.(songs: 'oops').messages).to eql(songs: ['must be an array'])
20
+ end
21
+
22
+ it 'fails when not all elements are valid' do
23
+ songs = [{ title: 'Hello' }, { title: nil }, { title: nil }]
24
+
25
+ expect(schema.(songs: songs).messages).to eql(
26
+ songs: {
27
+ 1 => { title: ['must be filled'] },
28
+ 2 => { title: ['must be filled'] }
29
+ }
30
+ )
31
+ end
32
+ end
33
+
34
+ context 'with a nested schema' do
35
+ subject(:schema) do
36
+ Dry::Validation.Schema do
37
+ key(:songs).each do
38
+ schema do
39
+ key(:title).required
40
+ key(:author).required
41
+ end
42
+ end
43
+ end
44
+ end
45
+
46
+ it 'passes when all elements are valid' do
47
+ songs = [
48
+ { title: 'Hello', author: 'Jane' },
49
+ { title: 'World', author: 'Joe' }
50
+ ]
51
+
52
+ expect(schema.(songs: songs)).to be_success
53
+ end
54
+
55
+ it 'fails when value is not an array' do
56
+ expect(schema.(songs: 'oops').messages).to eql(songs: ['must be an array'])
57
+ end
58
+
59
+ it 'fails when not all elements are valid' do
60
+ songs = [
61
+ { title: 'Hello', author: 'Jane' },
62
+ { title: nil, author: 'Joe' },
63
+ { title: 'World', author: nil },
64
+ { title: nil, author: nil }
65
+ ]
66
+
67
+ expect(schema.(songs: songs).messages).to eql(
68
+ songs: {
69
+ 1 => { title: ['must be filled'] },
70
+ 2 => { author: ['must be filled'] },
71
+ 3 => { title: ['must be filled'], author: ['must be filled'] }
72
+ }
73
+ )
74
+ end
75
+ end
76
+ end
77
+
78
+ context 'with inferred predicates and a form schema' do
79
+ subject(:schema) do
80
+ Dry::Validation.Form do
81
+ key(:songs).each(:str?)
82
+ end
83
+ end
84
+
85
+ it 'passes when all elements are valid' do
86
+ songs = %w(hello world)
87
+
88
+ expect(schema.(songs: songs)).to be_success
89
+ end
90
+
91
+ it 'fails when value is not an array' do
92
+ expect(schema.(songs: 'oops').messages).to eql(songs: ['must be an array'])
93
+ end
94
+
95
+ it 'fails when not all elements are valid' do
96
+ songs = ['hello', nil, 2]
97
+
98
+ expect(schema.(songs: songs).messages).to eql(
99
+ songs: {
100
+ 1 => ['must be a string'],
101
+ 2 => ['must be a string']
102
+ }
103
+ )
104
+ end
105
+ end
106
+ end
@@ -119,7 +119,7 @@ RSpec.describe Schema::Value do
119
119
  expect(rule.to_ast).to eql(
120
120
  [:and, [
121
121
  [:val, [:predicate, [:array?, []]]],
122
- [:each, [:val, [:predicate, [:key?, [:method]]]]]
122
+ [:each, [:set, [[:val, [:predicate, [:key?, [:method]]]]]]]
123
123
  ]]
124
124
  )
125
125
  end
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.7.0
4
+ version: 0.7.1
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-03-16 00:00:00.000000000 Z
12
+ date: 2016-03-21 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: dry-configurable
@@ -74,7 +74,7 @@ dependencies:
74
74
  version: '0.2'
75
75
  - - ">="
76
76
  - !ruby/object:Gem::Version
77
- version: 0.2.0
77
+ version: 0.2.1
78
78
  type: :runtime
79
79
  prerelease: false
80
80
  version_requirements: !ruby/object:Gem::Requirement
@@ -84,7 +84,7 @@ dependencies:
84
84
  version: '0.2'
85
85
  - - ">="
86
86
  - !ruby/object:Gem::Version
87
- version: 0.2.0
87
+ version: 0.2.1
88
88
  - !ruby/object:Gem::Dependency
89
89
  name: dry-types
90
90
  requirement: !ruby/object:Gem::Requirement
@@ -215,6 +215,7 @@ files:
215
215
  - spec/integration/schema/inheriting_schema_spec.rb
216
216
  - spec/integration/schema/input_processor_spec.rb
217
217
  - spec/integration/schema/macros/confirmation_spec.rb
218
+ - spec/integration/schema/macros/each_spec.rb
218
219
  - spec/integration/schema/macros/maybe_spec.rb
219
220
  - spec/integration/schema/macros/required_spec.rb
220
221
  - spec/integration/schema/macros/when_spec.rb
@@ -255,7 +256,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
255
256
  version: '0'
256
257
  requirements: []
257
258
  rubyforge_project:
258
- rubygems_version: 2.5.1
259
+ rubygems_version: 2.4.5.1
259
260
  signing_key:
260
261
  specification_version: 4
261
262
  summary: A simple validation library
@@ -279,6 +280,7 @@ test_files:
279
280
  - spec/integration/schema/inheriting_schema_spec.rb
280
281
  - spec/integration/schema/input_processor_spec.rb
281
282
  - spec/integration/schema/macros/confirmation_spec.rb
283
+ - spec/integration/schema/macros/each_spec.rb
282
284
  - spec/integration/schema/macros/maybe_spec.rb
283
285
  - spec/integration/schema/macros/required_spec.rb
284
286
  - spec/integration/schema/macros/when_spec.rb