dry-validation 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (65) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +8 -0
  3. data/.rspec +3 -0
  4. data/.rubocop.yml +16 -0
  5. data/.rubocop_todo.yml +7 -0
  6. data/.travis.yml +29 -0
  7. data/CHANGELOG.md +3 -0
  8. data/Gemfile +11 -0
  9. data/LICENSE +20 -0
  10. data/README.md +297 -0
  11. data/Rakefile +12 -0
  12. data/config/errors.yml +35 -0
  13. data/dry-validation.gemspec +25 -0
  14. data/examples/basic.rb +21 -0
  15. data/examples/nested.rb +30 -0
  16. data/examples/rule_ast.rb +33 -0
  17. data/lib/dry-validation.rb +1 -0
  18. data/lib/dry/validation.rb +12 -0
  19. data/lib/dry/validation/error.rb +43 -0
  20. data/lib/dry/validation/error_compiler.rb +116 -0
  21. data/lib/dry/validation/messages.rb +71 -0
  22. data/lib/dry/validation/predicate.rb +39 -0
  23. data/lib/dry/validation/predicate_set.rb +22 -0
  24. data/lib/dry/validation/predicates.rb +88 -0
  25. data/lib/dry/validation/result.rb +64 -0
  26. data/lib/dry/validation/rule.rb +125 -0
  27. data/lib/dry/validation/rule_compiler.rb +57 -0
  28. data/lib/dry/validation/schema.rb +74 -0
  29. data/lib/dry/validation/schema/definition.rb +15 -0
  30. data/lib/dry/validation/schema/key.rb +39 -0
  31. data/lib/dry/validation/schema/rule.rb +28 -0
  32. data/lib/dry/validation/schema/value.rb +31 -0
  33. data/lib/dry/validation/version.rb +5 -0
  34. data/rakelib/rubocop.rake +18 -0
  35. data/spec/fixtures/errors.yml +4 -0
  36. data/spec/integration/custom_error_messages_spec.rb +35 -0
  37. data/spec/integration/custom_predicates_spec.rb +57 -0
  38. data/spec/integration/validation_spec.rb +118 -0
  39. data/spec/shared/predicates.rb +31 -0
  40. data/spec/spec_helper.rb +18 -0
  41. data/spec/unit/error_compiler_spec.rb +165 -0
  42. data/spec/unit/predicate_spec.rb +37 -0
  43. data/spec/unit/predicates/empty_spec.rb +38 -0
  44. data/spec/unit/predicates/eql_spec.rb +21 -0
  45. data/spec/unit/predicates/exclusion_spec.rb +35 -0
  46. data/spec/unit/predicates/filled_spec.rb +38 -0
  47. data/spec/unit/predicates/format_spec.rb +21 -0
  48. data/spec/unit/predicates/gt_spec.rb +40 -0
  49. data/spec/unit/predicates/gteq_spec.rb +40 -0
  50. data/spec/unit/predicates/inclusion_spec.rb +35 -0
  51. data/spec/unit/predicates/int_spec.rb +34 -0
  52. data/spec/unit/predicates/key_spec.rb +29 -0
  53. data/spec/unit/predicates/lt_spec.rb +40 -0
  54. data/spec/unit/predicates/lteq_spec.rb +40 -0
  55. data/spec/unit/predicates/max_size_spec.rb +49 -0
  56. data/spec/unit/predicates/min_size_spec.rb +49 -0
  57. data/spec/unit/predicates/nil_spec.rb +28 -0
  58. data/spec/unit/predicates/size_spec.rb +49 -0
  59. data/spec/unit/predicates/str_spec.rb +32 -0
  60. data/spec/unit/rule/each_spec.rb +20 -0
  61. data/spec/unit/rule/key_spec.rb +27 -0
  62. data/spec/unit/rule/set_spec.rb +32 -0
  63. data/spec/unit/rule/value_spec.rb +42 -0
  64. data/spec/unit/rule_compiler_spec.rb +86 -0
  65. metadata +230 -0
@@ -0,0 +1,37 @@
1
+ require 'dry/validation/predicate'
2
+
3
+ RSpec.describe Dry::Validation::Predicate do
4
+ describe '#call' do
5
+ it 'returns result of the predicate function' do
6
+ is_empty = Dry::Validation::Predicate.new(:is_empty) { |str| str.empty? }
7
+
8
+ expect(is_empty.('')).to be(true)
9
+
10
+ expect(is_empty.('filled')).to be(false)
11
+ end
12
+ end
13
+
14
+ describe '#negation' do
15
+ it 'returns a negated version of a predicate' do
16
+ is_empty = Dry::Validation::Predicate.new(:is_empty) { |str| str.empty? }
17
+ is_filled = is_empty.negation
18
+
19
+ expect(is_filled.('')).to be(false)
20
+ expect(is_filled.('filled')).to be(true)
21
+ end
22
+ end
23
+
24
+ describe '#curry' do
25
+ it 'returns curried predicate' do
26
+ min_age = Dry::Validation::Predicate.new(:min_age) { |age, input| input >= age }
27
+
28
+ min_age_18 = min_age.curry(18)
29
+
30
+ expect(min_age_18.args).to eql([18])
31
+
32
+ expect(min_age_18.(18)).to be(true)
33
+ expect(min_age_18.(19)).to be(true)
34
+ expect(min_age_18.(17)).to be(false)
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,38 @@
1
+ require 'dry/validation/predicates'
2
+
3
+ RSpec.describe Dry::Validation::Predicates do
4
+ describe '#empty?' do
5
+ let(:predicate_name) { :empty? }
6
+
7
+ context 'when value is empty' do
8
+ let(:arguments_list) do
9
+ [
10
+ [''],
11
+ [[]],
12
+ [{}],
13
+ [nil]
14
+ ]
15
+ end
16
+
17
+ it_behaves_like 'a passing predicate'
18
+ end
19
+
20
+ context 'with value is not empty' do
21
+ let(:arguments_list) do
22
+ [
23
+ ['Jill'],
24
+ [[1, 2, 3]],
25
+ [{ name: 'John' }],
26
+ [true],
27
+ [false],
28
+ ['1'],
29
+ ['0'],
30
+ [:symbol],
31
+ [String]
32
+ ]
33
+ end
34
+
35
+ it_behaves_like 'a failing predicate'
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,21 @@
1
+ require 'dry/validation/predicates'
2
+
3
+ RSpec.describe Dry::Validation::Predicates, '#eql?' do
4
+ let(:predicate_name) { :eql? }
5
+
6
+ context 'when value is equal to the arg' do
7
+ let(:arguments_list) do
8
+ [['Foo', 'Foo']]
9
+ end
10
+
11
+ it_behaves_like 'a passing predicate'
12
+ end
13
+
14
+ context 'with value is not empty' do
15
+ let(:arguments_list) do
16
+ [['Bar', 'Foo']]
17
+ end
18
+
19
+ it_behaves_like 'a failing predicate'
20
+ end
21
+ end
@@ -0,0 +1,35 @@
1
+ require 'dry/validation/predicates'
2
+
3
+ RSpec.describe Dry::Validation::Predicates do
4
+ describe '#exclusion?' do
5
+ let(:predicate_name) { :exclusion? }
6
+
7
+ context 'when value is not present in list' do
8
+ let(:arguments_list) do
9
+ [
10
+ [['Jill', 'John'], 'Jack'],
11
+ [1..2, 0],
12
+ [1..2, 3],
13
+ [[nil, false], true]
14
+ ]
15
+ end
16
+
17
+ it_behaves_like 'a passing predicate'
18
+ end
19
+
20
+ context 'with value is present in list' do
21
+ let(:arguments_list) do
22
+ [
23
+ [['Jill', 'John'], 'Jill'],
24
+ [['Jill', 'John'], 'John'],
25
+ [1..2, 1],
26
+ [1..2, 2],
27
+ [[nil, false], nil],
28
+ [[nil, false], false]
29
+ ]
30
+ end
31
+
32
+ it_behaves_like 'a failing predicate'
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,38 @@
1
+ require 'dry/validation/predicates'
2
+
3
+ RSpec.describe Dry::Validation::Predicates do
4
+ describe '#filled?' do
5
+ let(:predicate_name) { :filled? }
6
+
7
+ context 'when value is filled' do
8
+ let(:arguments_list) do
9
+ [
10
+ ['Jill'],
11
+ [[1, 2, 3]],
12
+ [{ name: 'John' }],
13
+ [true],
14
+ [false],
15
+ ['1'],
16
+ ['0'],
17
+ [:symbol],
18
+ [String]
19
+ ]
20
+ end
21
+
22
+ it_behaves_like 'a passing predicate'
23
+ end
24
+
25
+ context 'with value is not filled' do
26
+ let(:arguments_list) do
27
+ [
28
+ [''],
29
+ [[]],
30
+ [{}],
31
+ [nil]
32
+ ]
33
+ end
34
+
35
+ it_behaves_like 'a failing predicate'
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,21 @@
1
+ require 'dry/validation/predicates'
2
+
3
+ RSpec.describe Dry::Validation::Predicates, '#format?' do
4
+ let(:predicate_name) { :format? }
5
+
6
+ context 'when value matches provided regexp' do
7
+ let(:arguments_list) do
8
+ [['Foo', /^F/]]
9
+ end
10
+
11
+ it_behaves_like 'a passing predicate'
12
+ end
13
+
14
+ context 'when value does not match provided regexp' do
15
+ let(:arguments_list) do
16
+ [['Bar', /^F/]]
17
+ end
18
+
19
+ it_behaves_like 'a failing predicate'
20
+ end
21
+ end
@@ -0,0 +1,40 @@
1
+ require 'dry/validation/predicates'
2
+
3
+ RSpec.describe Dry::Validation::Predicates do
4
+ describe '#gt?' do
5
+ let(:predicate_name) { :gt? }
6
+
7
+ context 'when value is greater than n' do
8
+ let(:arguments_list) do
9
+ [
10
+ [13, 14],
11
+ [13.37, 13.38]
12
+ ]
13
+ end
14
+
15
+ it_behaves_like 'a passing predicate'
16
+ end
17
+
18
+ context 'when value is equal to n' do
19
+ let(:arguments_list) do
20
+ [
21
+ [13, 13],
22
+ [13.37, 13.37]
23
+ ]
24
+ end
25
+
26
+ it_behaves_like 'a failing predicate'
27
+ end
28
+
29
+ context 'with value is less than n' do
30
+ let(:arguments_list) do
31
+ [
32
+ [13, 12],
33
+ [13.37, 13.36]
34
+ ]
35
+ end
36
+
37
+ it_behaves_like 'a failing predicate'
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,40 @@
1
+ require 'dry/validation/predicates'
2
+
3
+ RSpec.describe Dry::Validation::Predicates do
4
+ describe '#gteq?' do
5
+ let(:predicate_name) { :gteq? }
6
+
7
+ context 'when value is greater than n' do
8
+ let(:arguments_list) do
9
+ [
10
+ [13, 14],
11
+ [13.37, 13.38]
12
+ ]
13
+ end
14
+
15
+ it_behaves_like 'a passing predicate'
16
+ end
17
+
18
+ context 'when value is equal to n' do
19
+ let(:arguments_list) do
20
+ [
21
+ [13, 13],
22
+ [13.37, 13.37]
23
+ ]
24
+ end
25
+
26
+ it_behaves_like 'a passing predicate'
27
+ end
28
+
29
+ context 'with value is less than n' do
30
+ let(:arguments_list) do
31
+ [
32
+ [13, 12],
33
+ [13.37, 13.36]
34
+ ]
35
+ end
36
+
37
+ it_behaves_like 'a failing predicate'
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,35 @@
1
+ require 'dry/validation/predicates'
2
+
3
+ RSpec.describe Dry::Validation::Predicates do
4
+ describe '#inclusion?' do
5
+ let(:predicate_name) { :inclusion? }
6
+
7
+ context 'when value is present in list' do
8
+ let(:arguments_list) do
9
+ [
10
+ [['Jill', 'John'], 'Jill'],
11
+ [['Jill', 'John'], 'John'],
12
+ [1..2, 1],
13
+ [1..2, 2],
14
+ [[nil, false], nil],
15
+ [[nil, false], false]
16
+ ]
17
+ end
18
+
19
+ it_behaves_like 'a passing predicate'
20
+ end
21
+
22
+ context 'with value is not present in list' do
23
+ let(:arguments_list) do
24
+ [
25
+ [['Jill', 'John'], 'Jack'],
26
+ [1..2, 0],
27
+ [1..2, 3],
28
+ [[nil, false], true]
29
+ ]
30
+ end
31
+
32
+ it_behaves_like 'a failing predicate'
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,34 @@
1
+ require 'dry/validation/predicates'
2
+
3
+ RSpec.describe Dry::Validation::Predicates do
4
+ describe '#int?' do
5
+ let(:predicate_name) { :int? }
6
+
7
+ context 'when value is an integer' do
8
+ let(:arguments_list) do
9
+ [
10
+ [1],
11
+ [33],
12
+ [7]
13
+ ]
14
+ end
15
+
16
+ it_behaves_like 'a passing predicate'
17
+ end
18
+
19
+ context 'with value is not an integer' do
20
+ let(:arguments_list) do
21
+ [
22
+ [''],
23
+ [[]],
24
+ [{}],
25
+ [nil],
26
+ [:symbol],
27
+ [String]
28
+ ]
29
+ end
30
+
31
+ it_behaves_like 'a failing predicate'
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,29 @@
1
+ require 'dry/validation/predicates'
2
+
3
+ RSpec.describe Dry::Validation::Predicates do
4
+ describe '#key?' do
5
+ let(:predicate_name) { :key? }
6
+
7
+ context 'when key is present in value' do
8
+ let(:arguments_list) do
9
+ [
10
+ [:name, { name: 'John' }],
11
+ [:age, { age: 18 }]
12
+ ]
13
+ end
14
+
15
+ it_behaves_like 'a passing predicate'
16
+ end
17
+
18
+ context 'with key is not present in value' do
19
+ let(:arguments_list) do
20
+ [
21
+ [:name, { age: 18 }],
22
+ [:age, { name: 'Jill' }]
23
+ ]
24
+ end
25
+
26
+ it_behaves_like 'a failing predicate'
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,40 @@
1
+ require 'dry/validation/predicates'
2
+
3
+ RSpec.describe Dry::Validation::Predicates do
4
+ describe '#lt?' do
5
+ let(:predicate_name) { :lt? }
6
+
7
+ context 'when value is less than n' do
8
+ let(:arguments_list) do
9
+ [
10
+ [13, 12],
11
+ [13.37, 13.36],
12
+ ]
13
+ end
14
+
15
+ it_behaves_like 'a passing predicate'
16
+ end
17
+
18
+ context 'when value is equal to n' do
19
+ let(:arguments_list) do
20
+ [
21
+ [13, 13],
22
+ [13.37, 13.37]
23
+ ]
24
+ end
25
+
26
+ it_behaves_like 'a failing predicate'
27
+ end
28
+
29
+ context 'with value is greater than n' do
30
+ let(:arguments_list) do
31
+ [
32
+ [13, 14],
33
+ [13.37, 13.38]
34
+ ]
35
+ end
36
+
37
+ it_behaves_like 'a failing predicate'
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,40 @@
1
+ require 'dry/validation/predicates'
2
+
3
+ RSpec.describe Dry::Validation::Predicates do
4
+ describe '#lteq?' do
5
+ let(:predicate_name) { :lteq? }
6
+
7
+ context 'when value is less than n' do
8
+ let(:arguments_list) do
9
+ [
10
+ [13, 12],
11
+ [13.37, 13.36]
12
+ ]
13
+ end
14
+
15
+ it_behaves_like 'a passing predicate'
16
+ end
17
+
18
+ context 'when value is equal to n' do
19
+ let(:arguments_list) do
20
+ [
21
+ [13, 13],
22
+ [13.37, 13.37]
23
+ ]
24
+ end
25
+
26
+ it_behaves_like 'a passing predicate'
27
+ end
28
+
29
+ context 'with value is greater than n' do
30
+ let(:arguments_list) do
31
+ [
32
+ [13, 14],
33
+ [13.37, 13.38]
34
+ ]
35
+ end
36
+
37
+ it_behaves_like 'a failing predicate'
38
+ end
39
+ end
40
+ end