dry-validation 0.4.1 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (62) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +13 -0
  3. data/LICENSE +1 -1
  4. data/README.md +9 -9
  5. data/dry-validation.gemspec +2 -1
  6. data/lib/dry/validation.rb +1 -0
  7. data/lib/dry/validation/input_type_compiler.rb +2 -2
  8. data/lib/dry/validation/schema.rb +7 -6
  9. data/lib/dry/validation/schema/key.rb +0 -2
  10. data/lib/dry/validation/version.rb +1 -1
  11. data/spec/integration/custom_predicates_spec.rb +1 -1
  12. data/spec/integration/schema_form_spec.rb +0 -2
  13. data/spec/unit/input_type_compiler_spec.rb +57 -0
  14. metadata +20 -89
  15. data/lib/dry/validation/predicate.rb +0 -35
  16. data/lib/dry/validation/predicate_set.rb +0 -22
  17. data/lib/dry/validation/predicates.rb +0 -125
  18. data/lib/dry/validation/rule.rb +0 -90
  19. data/lib/dry/validation/rule/check.rb +0 -15
  20. data/lib/dry/validation/rule/composite.rb +0 -63
  21. data/lib/dry/validation/rule/each.rb +0 -13
  22. data/lib/dry/validation/rule/group.rb +0 -21
  23. data/lib/dry/validation/rule/key.rb +0 -17
  24. data/lib/dry/validation/rule/result.rb +0 -119
  25. data/lib/dry/validation/rule/set.rb +0 -22
  26. data/lib/dry/validation/rule/value.rb +0 -13
  27. data/lib/dry/validation/rule_compiler.rb +0 -81
  28. data/spec/shared/predicates.rb +0 -35
  29. data/spec/unit/predicate_spec.rb +0 -27
  30. data/spec/unit/predicates/bool_spec.rb +0 -34
  31. data/spec/unit/predicates/date_spec.rb +0 -31
  32. data/spec/unit/predicates/date_time_spec.rb +0 -31
  33. data/spec/unit/predicates/decimal_spec.rb +0 -32
  34. data/spec/unit/predicates/empty_spec.rb +0 -38
  35. data/spec/unit/predicates/eql_spec.rb +0 -21
  36. data/spec/unit/predicates/exclusion_spec.rb +0 -35
  37. data/spec/unit/predicates/filled_spec.rb +0 -38
  38. data/spec/unit/predicates/float_spec.rb +0 -31
  39. data/spec/unit/predicates/format_spec.rb +0 -21
  40. data/spec/unit/predicates/gt_spec.rb +0 -40
  41. data/spec/unit/predicates/gteq_spec.rb +0 -40
  42. data/spec/unit/predicates/inclusion_spec.rb +0 -35
  43. data/spec/unit/predicates/int_spec.rb +0 -34
  44. data/spec/unit/predicates/key_spec.rb +0 -29
  45. data/spec/unit/predicates/lt_spec.rb +0 -40
  46. data/spec/unit/predicates/lteq_spec.rb +0 -40
  47. data/spec/unit/predicates/max_size_spec.rb +0 -49
  48. data/spec/unit/predicates/min_size_spec.rb +0 -49
  49. data/spec/unit/predicates/none_spec.rb +0 -28
  50. data/spec/unit/predicates/size_spec.rb +0 -55
  51. data/spec/unit/predicates/str_spec.rb +0 -32
  52. data/spec/unit/predicates/time_spec.rb +0 -31
  53. data/spec/unit/rule/check_spec.rb +0 -29
  54. data/spec/unit/rule/conjunction_spec.rb +0 -28
  55. data/spec/unit/rule/disjunction_spec.rb +0 -36
  56. data/spec/unit/rule/each_spec.rb +0 -20
  57. data/spec/unit/rule/group_spec.rb +0 -12
  58. data/spec/unit/rule/implication_spec.rb +0 -14
  59. data/spec/unit/rule/key_spec.rb +0 -27
  60. data/spec/unit/rule/set_spec.rb +0 -32
  61. data/spec/unit/rule/value_spec.rb +0 -42
  62. data/spec/unit/rule_compiler_spec.rb +0 -123
@@ -1,55 +0,0 @@
1
- require 'dry/validation/predicates'
2
-
3
- RSpec.describe Dry::Validation::Predicates do
4
- describe '#size?' do
5
- let(:predicate_name) { :size? }
6
-
7
- context 'when value size is equal to n' do
8
- let(:arguments_list) do
9
- [
10
- [[8], 2],
11
- [4, 'Jill'],
12
- [2, { 1 => 'st', 2 => 'nd' }],
13
- [8, 8],
14
- [1..8, 5]
15
- ]
16
- end
17
-
18
- it_behaves_like 'a passing predicate'
19
- end
20
-
21
- context 'when value size is greater than n' do
22
- let(:arguments_list) do
23
- [
24
- [[1, 2], 3],
25
- [5, 'Jill'],
26
- [3, { 1 => 'st', 2 => 'nd' }],
27
- [1, 9],
28
- [1..5, 6]
29
- ]
30
- end
31
-
32
- it_behaves_like 'a failing predicate'
33
- end
34
-
35
- context 'with value size is less than n' do
36
- let(:arguments_list) do
37
- [
38
- [[1, 2], 1],
39
- [3, 'Jill'],
40
- [1, { 1 => 'st', 2 => 'nd' }],
41
- [1, 7],
42
- [1..5, 4]
43
- ]
44
- end
45
-
46
- it_behaves_like 'a failing predicate'
47
- end
48
-
49
- context 'with an unsupported size' do
50
- it 'raises an error' do
51
- expect { Predicates[:size?].call('oops', 1) }.to raise_error(ArgumentError, /oops/)
52
- end
53
- end
54
- end
55
- end
@@ -1,32 +0,0 @@
1
- require 'dry/validation/predicates'
2
-
3
- RSpec.describe Dry::Validation::Predicates do
4
- describe '#str?' do
5
- let(:predicate_name) { :str? }
6
-
7
- context 'when value is a string' do
8
- let(:arguments_list) do
9
- [
10
- [''],
11
- ['John']
12
- ]
13
- end
14
-
15
- it_behaves_like 'a passing predicate'
16
- end
17
-
18
- context 'with value is not a string' do
19
- let(:arguments_list) do
20
- [
21
- [[]],
22
- [{}],
23
- [nil],
24
- [:symbol],
25
- [String]
26
- ]
27
- end
28
-
29
- it_behaves_like 'a failing predicate'
30
- end
31
- end
32
- end
@@ -1,31 +0,0 @@
1
- require 'dry/validation/predicates'
2
-
3
- RSpec.describe Dry::Validation::Predicates do
4
- describe '#time?' do
5
- let(:predicate_name) { :time? }
6
-
7
- context 'when value is a date' do
8
- let(:arguments_list) do
9
- [[Time.now]]
10
- end
11
-
12
- it_behaves_like 'a passing predicate'
13
- end
14
-
15
- context 'with value is not an integer' do
16
- let(:arguments_list) do
17
- [
18
- [''],
19
- [[]],
20
- [{}],
21
- [nil],
22
- [:symbol],
23
- [String],
24
- [1]
25
- ]
26
- end
27
-
28
- it_behaves_like 'a failing predicate'
29
- end
30
- end
31
- end
@@ -1,29 +0,0 @@
1
- RSpec.describe Rule::Check do
2
- subject(:rule) { Rule::Check.new(:name, other.(input).curry(predicate)) }
3
-
4
- include_context 'predicates'
5
-
6
- let(:other) do
7
- Rule::Value.new(:name, none?).or(Rule::Value.new(:name, filled?))
8
- end
9
-
10
- describe '#call' do
11
- context 'when a given predicate passed' do
12
- let(:input) { 'Jane' }
13
- let(:predicate) { :filled? }
14
-
15
- it 'returns a success' do
16
- expect(rule.()).to be_success
17
- end
18
- end
19
-
20
- context 'when a given predicate did not pass' do
21
- let(:input) { nil }
22
- let(:predicate) { :filled? }
23
-
24
- it 'returns a failure' do
25
- expect(rule.()).to be_failure
26
- end
27
- end
28
- end
29
- end
@@ -1,28 +0,0 @@
1
- RSpec.describe Rule::Composite::Conjunction do
2
- subject(:rule) { Rule::Composite::Conjunction.new(left, right) }
3
-
4
- let(:left) { Rule::Value.new(:age, Predicates[:int?]) }
5
- let(:right) { Rule::Value.new(:age, Predicates[:gt?].curry(18)) }
6
-
7
- describe '#call' do
8
- it 'calls left and right' do
9
- expect(rule.(18)).to be_failure
10
- end
11
- end
12
-
13
- describe '#and' do
14
- let(:other) { Rule::Value.new(:age, Predicates[:lt?].curry(30)) }
15
-
16
- it 'creates conjunction with the other' do
17
- expect(rule.and(other).(31)).to be_failure
18
- end
19
- end
20
-
21
- describe '#or' do
22
- let(:other) { Rule::Value.new(:age, Predicates[:lt?].curry(14)) }
23
-
24
- it 'creates disjunction with the other' do
25
- expect(rule.or(other).(13)).to be_success
26
- end
27
- end
28
- end
@@ -1,36 +0,0 @@
1
- RSpec.describe Rule::Composite::Disjunction do
2
- subject(:rule) { Rule::Composite::Disjunction.new(left, right) }
3
-
4
- let(:left) { Rule::Value.new(:age, Predicates[:none?]) }
5
- let(:right) { Rule::Value.new(:age, Predicates[:gt?].curry(18)) }
6
-
7
- let(:other) do
8
- Rule::Value.new(:age, Predicates[:int?]) & Rule::Value.new(:age, Predicates[:lt?].curry(14))
9
- end
10
-
11
- describe '#call' do
12
- it 'calls left and right' do
13
- expect(rule.(nil)).to be_success
14
- expect(rule.(19)).to be_success
15
- expect(rule.(18)).to be_failure
16
- end
17
- end
18
-
19
- describe '#and' do
20
- it 'creates conjunction with the other' do
21
- expect(rule.and(other).(nil)).to be_failure
22
- expect(rule.and(other).(19)).to be_failure
23
- expect(rule.and(other).(13)).to be_failure
24
- expect(rule.and(other).(14)).to be_failure
25
- end
26
- end
27
-
28
- describe '#or' do
29
- it 'creates disjunction with the other' do
30
- expect(rule.or(other).(nil)).to be_success
31
- expect(rule.or(other).(19)).to be_success
32
- expect(rule.or(other).(13)).to be_success
33
- expect(rule.or(other).(14)).to be_failure
34
- end
35
- end
36
- end
@@ -1,20 +0,0 @@
1
- require 'dry/validation/rule'
2
-
3
- RSpec.describe Dry::Validation::Rule::Each do
4
- include_context 'predicates'
5
-
6
- subject(:address_rule) do
7
- Dry::Validation::Rule::Each.new(:name, is_string)
8
- end
9
-
10
- let(:is_string) { Dry::Validation::Rule::Value.new(:name, str?) }
11
-
12
- describe '#call' do
13
- it 'applies its rules to all elements in the input' do
14
- expect(address_rule.(['Address'])).to be_success
15
-
16
- expect(address_rule.([nil, 'Address'])).to be_failure
17
- expect(address_rule.([:Address, 'Address'])).to be_failure
18
- end
19
- end
20
- end
@@ -1,12 +0,0 @@
1
- RSpec.describe Rule::Group do
2
- include_context 'predicates'
3
-
4
- subject(:rule) { Rule::Group.new([:pass, :pass_confirm], eql?) }
5
-
6
- describe '#call' do
7
- it 'calls predicate with result values' do
8
- expect(rule.('foo', 'foo')).to be_success
9
- expect(rule.('foo', 'bar')).to be_failure
10
- end
11
- end
12
- end
@@ -1,14 +0,0 @@
1
- RSpec.describe Rule::Composite::Implication do
2
- subject(:rule) { Rule::Composite::Implication.new(left, right) }
3
-
4
- let(:left) { Rule::Value.new(:age, Predicates[:int?]) }
5
- let(:right) { Rule::Value.new(:age, Predicates[:gt?].curry(18)) }
6
-
7
- describe '#call' do
8
- it 'calls left and right' do
9
- expect(rule.('19')).to be_success
10
- expect(rule.(19)).to be_success
11
- expect(rule.(18)).to be_failure
12
- end
13
- end
14
- end
@@ -1,27 +0,0 @@
1
- require 'dry/validation/rule'
2
-
3
- RSpec.describe Dry::Validation::Rule::Key do
4
- include_context 'predicates'
5
-
6
- subject(:rule) { Dry::Validation::Rule::Key.new(:name, key?) }
7
-
8
- describe '#call' do
9
- it 'applies predicate to the value' do
10
- expect(rule.(name: 'Jane')).to be_success
11
- expect(rule.({})).to be_failure
12
- end
13
- end
14
-
15
- describe '#and' do
16
- let(:other) { Dry::Validation::Rule::Value.new(:name, str?) }
17
-
18
- it 'returns conjunction rule where value is passed to the right' do
19
- present_and_string = rule.and(other)
20
-
21
- expect(present_and_string.(name: 'Jane')).to be_success
22
-
23
- expect(present_and_string.({})).to be_failure
24
- expect(present_and_string.(name: 1)).to be_failure
25
- end
26
- end
27
- end
@@ -1,32 +0,0 @@
1
- require 'dry/validation/rule'
2
-
3
- RSpec.describe Dry::Validation::Rule::Set do
4
- include_context 'predicates'
5
-
6
- subject(:rule) do
7
- Dry::Validation::Rule::Set.new(:address, [is_string, min_size.curry(6)])
8
- end
9
-
10
- let(:is_string) { Dry::Validation::Rule::Value.new(:name, str?) }
11
- let(:min_size) { Dry::Validation::Rule::Value.new(:name, min_size?) }
12
-
13
- describe '#call' do
14
- it 'applies its rules to the input' do
15
- expect(rule.('Address')).to be_success
16
- expect(rule.('Addr')).to be_failure
17
- end
18
- end
19
-
20
- describe '#to_ary' do
21
- it 'returns an array representation' do
22
- expect(rule).to match_array([
23
- :set, [
24
- :address, [
25
- [:val, [:name, [:predicate, [:str?, []]]]],
26
- [:val, [:name, [:predicate, [:min_size?, [6]]]]]
27
- ]
28
- ]
29
- ])
30
- end
31
- end
32
- end
@@ -1,42 +0,0 @@
1
- require 'dry/validation/rule'
2
-
3
- RSpec.describe Dry::Validation::Rule::Value do
4
- include_context 'predicates'
5
-
6
- let(:is_nil) { Dry::Validation::Rule::Value.new(:name, none?) }
7
-
8
- let(:is_string) { Dry::Validation::Rule::Value.new(:name, str?) }
9
-
10
- let(:min_size) { Dry::Validation::Rule::Value.new(:name, min_size?) }
11
-
12
- describe '#call' do
13
- it 'returns result of a predicate' do
14
- expect(is_string.(1)).to be_failure
15
- expect(is_string.('1')).to be_success
16
- end
17
- end
18
-
19
- describe '#and' do
20
- it 'returns a conjunction' do
21
- string_and_min_size = is_string.and(min_size.curry(3))
22
-
23
- expect(string_and_min_size.('abc')).to be_success
24
- expect(string_and_min_size.('abcd')).to be_success
25
-
26
- expect(string_and_min_size.(1)).to be_failure
27
- expect(string_and_min_size.('ab')).to be_failure
28
- end
29
- end
30
-
31
- describe '#or' do
32
- it 'returns a disjunction' do
33
- nil_or_string = is_nil.or(is_string)
34
-
35
- expect(nil_or_string.(nil)).to be_success
36
- expect(nil_or_string.('abcd')).to be_success
37
-
38
- expect(nil_or_string.(true)).to be_failure
39
- expect(nil_or_string.(1)).to be_failure
40
- end
41
- end
42
- end
@@ -1,123 +0,0 @@
1
- require 'dry/validation/rule_compiler'
2
-
3
- RSpec.describe Dry::Validation::RuleCompiler, '#call' do
4
- subject(:compiler) { RuleCompiler.new(predicates) }
5
-
6
- let(:predicates) {
7
- { key?: predicate,
8
- filled?: predicate,
9
- email: val_rule.('email').curry(:filled?) }
10
- }
11
-
12
- let(:predicate) { double(:predicate).as_null_object }
13
-
14
- let(:key_rule) { Rule::Key.new(:email, predicate) }
15
- let(:not_key_rule) { Rule::Key.new(:email, predicate).negation }
16
- let(:val_rule) { Rule::Value.new(:email, predicate) }
17
- let(:check_rule) { Rule::Check.new(:email, predicates[:email]) }
18
- let(:and_rule) { key_rule & val_rule }
19
- let(:or_rule) { key_rule | val_rule }
20
- let(:xor_rule) { key_rule ^ val_rule }
21
- let(:set_rule) { Rule::Set.new(:email, [val_rule]) }
22
- let(:each_rule) { Rule::Each.new(:email, val_rule) }
23
-
24
- it 'compiles key rules' do
25
- ast = [[:key, [:email, [:predicate, [:key?, predicate]]]]]
26
-
27
- rules = compiler.(ast)
28
-
29
- expect(rules).to eql([key_rule])
30
- end
31
-
32
- it 'compiles check rules' do
33
- ast = [[:check, [:email, [:predicate, [:email, [:filled?]]]]]]
34
-
35
- rules = compiler.(ast)
36
-
37
- expect(rules).to eql([check_rule])
38
- end
39
-
40
-
41
- it 'compiles negated rules' do
42
- ast = [[:not, [:key, [:email, [:predicate, [:key?, predicate]]]]]]
43
-
44
- rules = compiler.(ast)
45
-
46
- expect(rules).to eql([not_key_rule])
47
- end
48
-
49
- it 'compiles conjunction rules' do
50
- ast = [
51
- [
52
- :and, [
53
- [:key, [:email, [:predicate, [:key?, []]]]],
54
- [:val, [:email, [:predicate, [:filled?, []]]]]
55
- ]
56
- ]
57
- ]
58
-
59
- rules = compiler.(ast)
60
-
61
- expect(rules).to eql([and_rule])
62
- end
63
-
64
- it 'compiles disjunction rules' do
65
- ast = [
66
- [
67
- :or, [
68
- [:key, [:email, [:predicate, [:key?, []]]]],
69
- [:val, [:email, [:predicate, [:filled?, []]]]]
70
- ]
71
- ]
72
- ]
73
-
74
- rules = compiler.(ast)
75
-
76
- expect(rules).to eql([or_rule])
77
- end
78
-
79
- it 'compiles exclusive disjunction rules' do
80
- ast = [
81
- [
82
- :xor, [
83
- [:key, [:email, [:predicate, [:key?, []]]]],
84
- [:val, [:email, [:predicate, [:filled?, []]]]]
85
- ]
86
- ]
87
- ]
88
-
89
- rules = compiler.(ast)
90
-
91
- expect(rules).to eql([xor_rule])
92
- end
93
-
94
- it 'compiles set rules' do
95
- ast = [
96
- [
97
- :set, [
98
- :email, [
99
- [:val, [:email, [:predicate, [:filled?, []]]]]
100
- ]
101
- ]
102
- ]
103
- ]
104
-
105
- rules = compiler.(ast)
106
-
107
- expect(rules).to eql([set_rule])
108
- end
109
-
110
- it 'compiles each rules' do
111
- ast = [
112
- [
113
- :each, [
114
- :email, [:val, [:email, [:predicate, [:filled?, []]]]]
115
- ]
116
- ]
117
- ]
118
-
119
- rules = compiler.(ast)
120
-
121
- expect(rules).to eql([each_rule])
122
- end
123
- end