easy_json_matcher 0.2.2 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (72) hide show
  1. checksums.yaml +4 -4
  2. data/lib/easy_json_matcher/array_content_validator.rb +22 -0
  3. data/lib/easy_json_matcher/array_generator.rb +41 -0
  4. data/lib/easy_json_matcher/array_validator.rb +11 -76
  5. data/lib/easy_json_matcher/attribute_generator.rb +37 -0
  6. data/lib/easy_json_matcher/attribute_type_methods.rb +29 -0
  7. data/lib/easy_json_matcher/coercion_error.rb +10 -0
  8. data/lib/easy_json_matcher/content_wrapper.rb +2 -4
  9. data/lib/easy_json_matcher/{validation_error.rb → easy_json_matcher_error.rb} +2 -1
  10. data/lib/easy_json_matcher/json_coercer.rb +34 -0
  11. data/lib/easy_json_matcher/node.rb +10 -114
  12. data/lib/easy_json_matcher/node_generator.rb +58 -0
  13. data/lib/easy_json_matcher/schema_generator.rb +47 -71
  14. data/lib/easy_json_matcher/schema_library.rb +8 -11
  15. data/lib/easy_json_matcher/unknown_validation_step_error.rb +10 -0
  16. data/lib/easy_json_matcher/validation_chain_factory.rb +48 -0
  17. data/lib/easy_json_matcher/validation_rules.rb +59 -0
  18. data/lib/easy_json_matcher/validation_step.rb +36 -0
  19. data/lib/easy_json_matcher/validator.rb +12 -87
  20. data/lib/easy_json_matcher/validator_set.rb +31 -0
  21. data/lib/easy_json_matcher/version.rb +1 -1
  22. data/lib/easy_json_matcher.rb +6 -2
  23. data/lib/easy_json_matcher.rb~ +5 -0
  24. data/test/array_content_validator_test.rb +17 -0
  25. data/test/custom_validations_test.rb +17 -17
  26. data/test/global_validation_options_test.rb +39 -44
  27. data/test/json_coercer_test.rb +25 -0
  28. data/test/managing_schemas_test.rb +53 -52
  29. data/test/node_test.rb +28 -0
  30. data/test/primitives_boolean_test.rb +27 -0
  31. data/test/primitives_date_test.rb +28 -0
  32. data/test/primitives_number_test.rb +27 -0
  33. data/test/primitives_object_test.rb +27 -0
  34. data/test/primitives_string_test.rb +27 -0
  35. data/test/primtives_value_test.rb +23 -0
  36. data/test/required_validation_test.rb +7 -7
  37. data/test/schema_generator_test.rb +23 -0
  38. data/test/strict_mode_test.rb +25 -54
  39. data/test/test_helper.rb +8 -6
  40. data/test/validating_arrays_test.rb +40 -132
  41. data/test/validation_chain_factory_test.rb +45 -0
  42. data/test/validation_chain_test_helper.rb +13 -0
  43. data/test/validation_step_array_test.rb +27 -0
  44. data/test/validation_step_boolean_test.rb +19 -0
  45. data/test/validation_step_date_test.rb +20 -0
  46. data/test/validation_step_not_required_test.rb +35 -0
  47. data/test/validation_step_number_test.rb +23 -0
  48. data/test/validation_step_object_test.rb +20 -0
  49. data/test/validation_step_required_test.rb +19 -0
  50. data/test/validation_step_string_test.rb +20 -0
  51. data/test/validation_step_test.rb +55 -0
  52. data/test/validation_step_value_test.rb +15 -0
  53. data/test/validator_set_test.rb +49 -0
  54. data/test/validator_test.rb +44 -0
  55. metadata +64 -26
  56. data/lib/easy_json_matcher/boolean_validator.rb +0 -14
  57. data/lib/easy_json_matcher/date_validator.rb +0 -44
  58. data/lib/easy_json_matcher/node.rb~ +0 -121
  59. data/lib/easy_json_matcher/number_validator.rb +0 -22
  60. data/lib/easy_json_matcher/object_validator.rb +0 -12
  61. data/lib/easy_json_matcher/schema_generator.rb~ +0 -104
  62. data/lib/easy_json_matcher/string_validator.rb +0 -16
  63. data/lib/easy_json_matcher/validator.rb~ +0 -99
  64. data/lib/easy_json_matcher/validator_factory.rb +0 -76
  65. data/lib/easy_json_matcher/value_validator.rb +0 -10
  66. data/lib/easy_json_matcher/version.rb~ +0 -3
  67. data/test/custom_validations_test.rb~ +0 -25
  68. data/test/easy_json_matcher_test.rb +0 -262
  69. data/test/easy_json_matcher_test.rb~ +0 -262
  70. data/test/error_messages_test.rb +0 -148
  71. data/test/reset_test.rb +0 -32
  72. data/test/reset_test.rb~ +0 -32
data/test/node_test.rb ADDED
@@ -0,0 +1,28 @@
1
+ require "test_helper"
2
+ require "easy_json_matcher/validation_chain_factory"
3
+
4
+ module EasyJSONMatcher
5
+
6
+ describe Node do
7
+
8
+ describe "#add_validator" do
9
+
10
+ it "should respond to #check" do
11
+ Node.new(validators: nil).must_respond_to :check
12
+ end
13
+
14
+ it "should send call to its own validator" do
15
+ test_value = { a: 1, b: 2, c: 3 }
16
+ validators = (:a..:c).each_with_object({}) do |n, h|
17
+ h[n] = ValidationChainFactory.get_chain(steps: [:string])
18
+ end
19
+ node = Node.new(opts: [:required], validators: validators)
20
+ expected = [{a: ["1 is not a String"],
21
+ b: ["2 is not a String"],
22
+ c: ["3 is not a String"]
23
+ }]
24
+ node.check(value: test_value).must_be :==, expected
25
+ end
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,27 @@
1
+ require 'test_helper'
2
+ require 'json'
3
+
4
+ # This test suite covers the basic concept of validating that a value is a
5
+ # certain type
6
+
7
+ module EasyJSONMatcher
8
+
9
+ describe "Basic Type Validations" do
10
+
11
+ before do
12
+ @test_schema = SchemaGenerator.new { |g|
13
+ g.has_boolean key: :boolean
14
+ }.generate_schema
15
+ end
16
+
17
+ it "should validate booleans" do
18
+ candidate = { boolean: true}.to_json
19
+ @test_schema.validate(candidate: candidate).must_be :empty?
20
+ end
21
+
22
+ it "should return errors for non-booleans" do
23
+ candidate = { boolean: 1 }.to_json
24
+ @test_schema.validate(candidate: candidate).wont_be :empty?
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,28 @@
1
+
2
+ require 'test_helper'
3
+ require 'json'
4
+
5
+ # This test suite covers the basic concept of validating that a value is a
6
+ # certain type
7
+
8
+ module EasyJSONMatcher
9
+
10
+ describe "Date primitive" do
11
+
12
+ before do
13
+ @test_schema = SchemaGenerator.new { |g|
14
+ g.has_date key: :date
15
+ }.generate_schema
16
+ end
17
+
18
+ it "should validate dates" do
19
+ candidate = { date: "2015-04-01"}.to_json
20
+ @test_schema.validate(candidate: candidate).must_be :empty?
21
+ end
22
+
23
+ it "should return errors for non-dates" do
24
+ candidate = { date: "hello" }.to_json
25
+ @test_schema.validate(candidate: candidate).wont_be :empty?
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,27 @@
1
+ require 'test_helper'
2
+ require 'json'
3
+
4
+ # This test suite covers the basic concept of validating that a value is a
5
+ # certain type
6
+
7
+ module EasyJSONMatcher
8
+
9
+ describe "Number Validations" do
10
+
11
+ before do
12
+ @test_schema = SchemaGenerator.new { |g|
13
+ g.has_number key: :number
14
+ }.generate_schema
15
+ end
16
+
17
+ it "should validate Strings" do
18
+ candidate = { number: 1 }.to_json
19
+ @test_schema.validate(candidate: candidate).must_be :empty?
20
+ end
21
+
22
+ it "should return errors for non-strings" do
23
+ candidate = { number: "hello" }.to_json
24
+ @test_schema.validate(candidate: candidate).wont_be :empty?
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,27 @@
1
+ require 'test_helper'
2
+ require 'json'
3
+
4
+ # This test suite covers the basic concept of validating that a value is a
5
+ # certain type
6
+
7
+ module EasyJSONMatcher
8
+
9
+ describe "Object primitive test" do
10
+
11
+ before do
12
+ @test_schema = SchemaGenerator.new { |g|
13
+ g.has_object key: :object
14
+ }.generate_schema
15
+ end
16
+
17
+ it "should validate booleans" do
18
+ candidate = { object: { a: 1}}.to_json
19
+ @test_schema.validate(candidate: candidate).must_be :empty?
20
+ end
21
+
22
+ it "should return errors for non-booleans" do
23
+ candidate = { object: 1 }.to_json
24
+ @test_schema.validate(candidate: candidate).wont_be :empty?
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,27 @@
1
+ require 'test_helper'
2
+ require 'json'
3
+
4
+ # This test suite covers the basic concept of validating that a value is a
5
+ # certain type
6
+
7
+ module EasyJSONMatcher
8
+
9
+ describe "Basic Type Validations" do
10
+
11
+ before do
12
+ @test_schema = SchemaGenerator.new { |g|
13
+ g.has_string key: :string
14
+ }.generate_schema
15
+ end
16
+
17
+ it "should validate Strings" do
18
+ candidate = { string: "hello" }.to_json
19
+ @test_schema.validate(candidate: candidate).must_be :empty?
20
+ end
21
+
22
+ it "should return errors for non-strings" do
23
+ candidate = { string: 1 }.to_json
24
+ @test_schema.validate(candidate: candidate).wont_be :empty?
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,23 @@
1
+ require 'test_helper'
2
+ require 'json'
3
+
4
+ # This test suite covers the basic concept of validating that a value is a
5
+ # certain type
6
+
7
+ module EasyJSONMatcher
8
+
9
+ describe "Value primitive test" do
10
+
11
+ before do
12
+ @test_schema = SchemaGenerator.new { |g|
13
+ g.has_boolean key: :value
14
+ }.generate_schema
15
+ end
16
+
17
+ it "should validate any explicitly set value" do
18
+ candidate = { value: nil}.to_json
19
+ @test_schema.validate(candidate: candidate).must_be :empty?
20
+ end
21
+
22
+ end
23
+ end
@@ -4,8 +4,8 @@ class RequireValidationTest < ActiveSupport::TestCase
4
4
 
5
5
  test "As a user I want to insist that a value is present" do
6
6
  astronaut_schema = EasyJSONMatcher::SchemaGenerator.new { |s|
7
- s.has_attribute key: :has_oxygen, opts: {type: :boolean, required: true}
8
- s.has_attribute key: :name, opts: {type: :string}
7
+ s.has_attribute key: :has_oxygen, opts: [:boolean, :required]
8
+ s.has_attribute key: :name, opts: [:string]
9
9
  }.generate_schema
10
10
 
11
11
  valid_astronaut = {
@@ -13,25 +13,25 @@ class RequireValidationTest < ActiveSupport::TestCase
13
13
  name: 'Buzz Aldrin'
14
14
  }.to_json
15
15
 
16
- assert(astronaut_schema.valid?(valid_astronaut), "#{valid_astronaut} should be a valid object")
16
+ assert(astronaut_schema.valid?(candidate: valid_astronaut), "#{valid_astronaut} should be a valid object")
17
17
 
18
18
  invalid_astronaut = {
19
19
  name: 'Neil Armstrong'
20
20
  }.to_json
21
21
 
22
- assert_not(astronaut_schema.valid?(invalid_astronaut), "#{invalid_astronaut} should not be a valid object")
22
+ assert_not(astronaut_schema.valid?(candidate: invalid_astronaut), "#{invalid_astronaut} should not be a valid object")
23
23
  end
24
24
 
25
25
  test "As a user I want validations to pass if the value is not present and I have not required it to be there" do
26
26
  astronaut_schema = EasyJSONMatcher::SchemaGenerator.new { |s|
27
- s.has_attribute key: :has_oxygen, opts: {type: :boolean}
28
- s.has_attribute key: :name, opts: {type: :string}
27
+ s.has_attribute key: :has_oxygen, opts: [:boolean]
28
+ s.has_attribute key: :name, opts: [:string]
29
29
  }.generate_schema
30
30
 
31
31
  valid_astronaut = {
32
32
  name: 'Buzz Aldrin'
33
33
  }.to_json
34
34
 
35
- assert(astronaut_schema.valid?(valid_astronaut), "#{valid_astronaut} should be valid since :has_oxygen is not required")
35
+ assert(astronaut_schema.valid?(candidate: valid_astronaut), "#{valid_astronaut} should be valid since :has_oxygen is not required")
36
36
  end
37
37
  end
@@ -0,0 +1,23 @@
1
+ require "test_helper"
2
+
3
+ module EasyJSONMatcher
4
+
5
+ describe SchemaGenerator do
6
+
7
+ describe "SchemaGenerator#generate_schema" do
8
+
9
+ it "should return a validator" do
10
+ end
11
+ end
12
+
13
+ describe "Global Options" do
14
+
15
+ it "should accept a global options parameter" do
16
+ end
17
+
18
+ it "should filter out Schema options before passing them onto the NodeGenerator" do
19
+ end
20
+ end
21
+ end
22
+ end
23
+
@@ -1,63 +1,34 @@
1
1
  require 'test_helper'
2
2
 
3
- class StrictModeTest < ActiveSupport::TestCase
4
-
5
- test "As a user I want my schema to only validate objects iff the set of keys
6
- in the candidate minus the set of keys specified in the schema equals the
7
- empty set" do
8
-
9
- test_schema = EasyJSONMatcher::SchemaGenerator.new(global_opts: {strict: true}) {|s|
10
- s.has_string key: :first
11
- s.has_string key: :second
12
- s.has_string key: :third
13
- }.generate_schema
14
-
15
- too_many_planets = {
16
- first: 'Zeus',
17
- second: 'Poseidon',
18
- third: 'Hera',
19
- fourth: 'Demeter'
20
- }.to_json
21
-
22
- assert_not(test_schema.valid?(too_many_planets), "#{too_many_planets} should not be valid as it has the unspecified :fourth key")
23
- end
3
+ module EasyJSONMatcher
24
4
 
25
- setup do
26
- @test_schema = EasyJSONMatcher::SchemaGenerator.new(global_opts: {strict: true}) {|s|
27
- s.has_string key: :name
28
- s.contains_node key: :about do |n|
29
- n.has_string key: :religion
30
- n.has_string key: :domain
31
- end
32
- }.generate_schema
33
- end
5
+ describe "Strict Mode" do
34
6
 
35
- test "As a user, if I have specified strict at the top level, I expect it to apply to nested nodes" do
7
+ subject {
8
+ SchemaGenerator.new(global_opts: [ :strict ]) { |s|
9
+ s.has_attribute key: :a, opts: []
10
+ s.has_attribute key: :b, opts: []
11
+ }.generate_schema
12
+ }
36
13
 
37
- poseidon = {
38
- name: 'Poseidon',
39
- about: {
40
- religion: 'Olympian Gods',
41
- domain: 'The Sea',
42
- hobbies: ['Battleships Champion', 'Horse Tamer', 'Seismology']
43
- }
44
- }.to_json
45
14
 
46
- assert_not(@test_schema.valid?(poseidon), "#{poseidon} should not be valid as hobbies wasn't expected in the schema, and he didn't have any")
47
- end
15
+ it "should validate candidates with the correct keys" do
16
+ candidate = {
17
+ a: "a",
18
+ b: "b"
19
+ }.to_json
20
+
21
+ subject.validate(candidate: candidate).must_be :empty?
22
+ end
23
+
24
+ it "should not validate candidates with extra keys" do
25
+ candidate = {
26
+ a: "a",
27
+ b: "b",
28
+ c: "c"
29
+ }.to_json
48
30
 
49
- test "As a user, if the strict validation fails, I want to be told why" do
50
- zeus = {
51
- name: 'Zeus',
52
- about: {
53
- religion: 'Olympian Gods',
54
- domain: 'Firmament',
55
- spouse: 'Hera',
56
- address: 'Mount Olympus'
57
- }
58
- }.to_json
59
-
60
- assert_not(@test_schema.valid? zeus)
61
- assert_match(/\[:spouse, :address\] found in addition to expected keys/, @test_schema.get_errors[:about][:node_errors_][0])
31
+ subject.validate(candidate: candidate).wont_be :empty?
32
+ end
62
33
  end
63
34
  end
data/test/test_helper.rb CHANGED
@@ -1,9 +1,11 @@
1
- require 'pretty_backtrace/enable'
2
- require 'minitest'
3
- require 'minitest/autorun'
4
- require 'active_support'
5
- require 'mocha/mini_test'
6
- require 'easy_json_matcher'
1
+ require "pretty_backtrace/enable"
2
+ require "minitest"
3
+ require "minitest/autorun"
4
+ require "active_support"
5
+ require "mocha/mini_test"
6
+ require "easy_json_matcher"
7
+ require "pp"
8
+ require "byebug"
7
9
 
8
10
  # Filter out Minitest backtrace while allowing backtrace from other libraries
9
11
  # to be shown.
@@ -1,139 +1,47 @@
1
1
  require 'test_helper'
2
-
3
- class ValidatingArraysTest < ActiveSupport::TestCase
4
-
5
- test "As a user I want to validate that my arrays can contain objects of a specific schema" do
6
- EasyJSONMatcher::SchemaGenerator.new {|s|
7
-
8
- s.has_attribute key: :name, opts: {type: :string, required: true}
9
- s.has_attribute key: :spouse, opts: {type: :string}
10
- }.register as: :greek_hero
11
-
12
- test_schema = EasyJSONMatcher::SchemaGenerator.new {|s|
13
- s.contains_array(key: :data, opts: {required: true}) do |a|
14
- a.should_only_contain(type: :schema, opts: {name: :greek_hero})
15
- end
16
- }.generate_schema
17
-
18
- valid_json = {
19
- data: [{
2
+ require "easy_json_matcher/array_validator"
3
+
4
+ module EasyJSONMatcher
5
+
6
+ describe ArrayValidator do
7
+
8
+ before do
9
+ SchemaGenerator.new {|s|
10
+ s.has_attribute key: :name, opts: [:string, :required]
11
+ s.has_attribute key: :spouse, opts: [:string, :required]
12
+ }.register as: :greek_hero
13
+ end
14
+
15
+ subject{
16
+ test_schema = SchemaGenerator.new {|s|
17
+ s.contains_array(key: :data) do |a|
18
+ a.elements_should be: [:greek_hero]
19
+ end
20
+ }.generate_schema
21
+ }
22
+
23
+ it "should validate each value in the array" do
24
+ validator = SchemaGenerator.new { |s|
25
+ s.contains_array(key: :array) do |a|
26
+ a.elements_should be: [:number]
27
+ end
28
+ }.generate_schema
29
+ candidate = { array: [1,2,3,4,"oops"] }.to_json
30
+ validator.validate(candidate: candidate).wont_be :empty?
31
+ end
32
+
33
+ it "should validate schemas" do
34
+ invalid_json = {
35
+ data: [{
20
36
  name: 'More Greek Heroes',
21
37
  items: ['Hector', 'Ajax', 'Hippolyta', 'Penthesila']
22
38
  },
23
- {
24
- name: 'Roman Heroes',
25
- items: ['Romulus', 'Remus', 'Aeneus']
39
+ {
40
+ name: 'Roman Heroes',
41
+ items: ['Romulus', 'Remus', 'Aeneus']
26
42
  }]
27
- }.to_json
28
-
29
- assert(test_schema.valid?(valid_json), "#{valid_json} should have been validated as it follows the correct schema")
30
-
31
- invalid_json = {
32
- data: [1,2,3,4,5]
33
- }.to_json
34
-
35
- assert_not(test_schema.valid?(invalid_json), "#{invalid_json} should not have validated as it does not contain a :heroes schema")
36
- end
37
-
38
- test "As a user I want to validate that my arrays only contain strings" do
39
- test_schema = EasyJSONMatcher::SchemaGenerator.new {|s|
40
- s.has_string key: :name, opts: {required: true}
41
- s.contains_array key: :data, opts: {required: true} do |a|
42
- a.should_only_contain_strings
43
- end
44
- }.generate_schema
45
-
46
- valid_json = {
47
- name: 'German Heroes',
48
- data: ['Brunhilda', 'Siegfried', 'Faust']
49
- }.to_json
50
-
51
- assert(test_schema.valid?(valid_json), "#{valid_json} should have been valid as it only contained strings")
52
-
53
- invalid_json = {
54
- name: 'Fibonacci sequence',
55
- data: [0,1,1,2,3,5,8,13,24]
56
- }.to_json
57
-
58
- assert_not(test_schema.valid?(invalid_json), "#{invalid_json} should not have been valid as it did not contain strings")
59
-
60
- end
61
-
62
- test 'As a user I want to validate that my arrays only contain numbers' do
63
- test_schema = EasyJSONMatcher::SchemaGenerator.new {|s|
64
- s.has_string key: :name, opts: {required: true}
65
- s.contains_array key: :data, opts: {required: true} do |a|
66
- a.should_only_contain_numbers
67
- end
68
- }.generate_schema
69
-
70
- valid_json = {
71
- name: 'Square Numbers',
72
- data: [1,4,9,16,25,36,49,64]
73
- }.to_json
74
-
75
- assert(test_schema.valid?(valid_json), "#{valid_json} should have been valid as it contained only numbers")
76
-
77
- invalid_json = {
78
- name: 'Notable Scots',
79
- data: ['Alexander Fleming', 'Adam Smith', 'John Logie Baird', 'St. Andrew', 'St. Columba', 1, 2, 3, 4, 5]
80
- }.to_json
81
- assert_not(test_schema.valid?(invalid_json), "#{invalid_json} should not have been valid as it did not only contain numbers")
82
- end
83
-
84
- test 'As a user I want to validate that my arrays only contain booleans' do
85
-
86
- test_schema = EasyJSONMatcher::SchemaGenerator.new {|s|
87
- s.has_string key: :name, opts: {required: true}
88
- s.contains_array key: :data, opts: {required: true} do |a|
89
- a.should_only_contain_booleans
90
- end
91
- }.generate_schema
92
-
93
- valid_json = {
94
- name: 'Predicates',
95
- data: [true, true, false, true, false, false]
96
- }.to_json
97
-
98
- assert(test_schema.valid?(valid_json), "#{valid_json} should have been valid as it contains only booleans")
99
-
100
- invalid_json = {
101
- name: 'Notable English',
102
- data: ['St. George', 'Winston Churchill', 'Admiral Nelson', true, true, false, false]
103
- }.to_json
104
-
105
- assert_not(test_schema.valid?(invalid_json), "#{invalid_json} should have been invalid as it contains strings")
106
- end
107
-
108
- test 'As a user I want to validate that my arrays contain only objects' do
109
-
110
- test_schema = EasyJSONMatcher::SchemaGenerator.new { |s|
111
- s.has_string key: :name, opts: {required: true}
112
- s.contains_array key: :data, opts: {required: true} do |a|
113
- a.should_only_contain_objects
114
- end
115
- }.generate_schema
116
-
117
- valid_json = {
118
- name: 'Hand of Cards',
119
- data: [
120
- {suite: 'Spades', value: 'Ace'},
121
- {suite: 'Hearts', value: 'Queen'},
122
- {suite: 'Clubs', value: 'Jack'},
123
- {suite: 'Diamonds', value: '3'}]
124
- }.to_json
125
-
126
- assert(test_schema.valid?(valid_json), "#{valid_json} should have been valid as it only contains objects")
127
-
128
- invalid_json = {
129
- name: 'Norse Gods',
130
- data: [{'Thor': 'Thunder'}, 'Odin', 'Loki', 'Freya']
131
- }.to_json
132
-
133
- assert_not(test_schema.valid?(invalid_json), "#{invalid_json} should not have been valid as it does not only contain objects")
134
- end
135
-
136
- test 'as a user I want to validate that my arrays contain only dates' do
137
-
43
+ }.to_json
44
+ subject.validate(candidate: invalid_json).wont_be :empty?
45
+ end
138
46
  end
139
47
  end
@@ -0,0 +1,45 @@
1
+ require "test_helper"
2
+ require "easy_json_matcher/validation_chain_factory"
3
+ require "easy_json_matcher/unknown_validation_step_error"
4
+
5
+ module EasyJSONMatcher
6
+
7
+ describe ValidationChainFactory do
8
+
9
+ STD_TYPES = [:object, :string, :number, :date, :boolean, :value, :required, :not_required]
10
+
11
+ describe "#get_step_for" do
12
+
13
+ it "should return a validation step for all available value types" do
14
+ STD_TYPES.each do |type|
15
+ ValidationChainFactory.get_step_for(validating: type).wont_be :==, nil
16
+ end
17
+ end
18
+
19
+ it "should return a custom validator if :validating responds to call" do
20
+ can_be_called = ->(value, errors) { }
21
+ ValidationChainFactory.get_step_for(validating: can_be_called).
22
+ wont_be :==, nil
23
+ end
24
+
25
+ it "should otherwise raise an error" do
26
+ cannot_use = String.new
27
+ -> { ValidationChainFactory.get_step_for(validating: cannot_use) }.
28
+ must_raise UnknownValidationStepError
29
+ end
30
+ end
31
+
32
+ describe "#get_chain" do
33
+
34
+ it "should move :required validation to the start of the chain" do
35
+ chain = ValidationChainFactory.get_chain(steps: [:string, :required])
36
+ chain.check(value: nil)[0].must_match /no value found/
37
+ end
38
+
39
+ it "should put :not_required at the start if :required is not specified" do
40
+ chain = ValidationChainFactory.get_chain(steps: [:string])
41
+ chain.check(value: nil).must_be :empty?
42
+ end
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,13 @@
1
+ require "easy_json_matcher/validation_chain_factory"
2
+
3
+ module ValidationChainTestHelper
4
+
5
+ def get_instance(type:)
6
+ EasyJSONMatcher::ValidationChainFactory.get_step_for validating: type
7
+ end
8
+
9
+ def assert_chain_verifies(type:, test_value:, outcome:)
10
+ v_step = get_instance(type: type)
11
+ expect(v_step.check(value: test_value).empty?).must_be :==, outcome
12
+ end
13
+ end
@@ -0,0 +1,27 @@
1
+ require "test_helper"
2
+ require "easy_json_matcher/array_validator"
3
+
4
+ module EasyJSONMatcher
5
+
6
+ describe ArrayValidator do
7
+
8
+ before do
9
+ @subject = ArrayValidator.new verify_content_as: [:string]
10
+ end
11
+
12
+ describe "Validating Array Types" do
13
+
14
+ it "should return valid for empty arrays" do
15
+ @subject.check(value: []).must_be :empty?
16
+ end
17
+
18
+ it "should return errors for any other object" do
19
+ @subject.check(value: 1).wont_be :empty?
20
+ end
21
+
22
+ it "should check all the content is of a particular type" do
23
+ @subject.check(value: [1,2,3]).wont_be :empty?
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,19 @@
1
+ require "test_helper"
2
+ require "validation_chain_test_helper"
3
+
4
+ include ValidationChainTestHelper
5
+
6
+ describe "Boolean Validation Step" do
7
+
8
+ describe "should be able to return an instance that validates booleans" do
9
+
10
+ it "should not return any errors for a boolean" do
11
+ [true,false].each { |b| assert_chain_verifies(type: :boolean, test_value: b, outcome: true)}
12
+ end
13
+
14
+ it "should return an error for an invalid boolean" do
15
+ no_bool = 0
16
+ assert_chain_verifies(type: :boolean, test_value: no_bool, outcome: false)
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,20 @@
1
+ require "test_helper"
2
+ require "validation_chain_test_helper"
3
+
4
+ include ValidationChainTestHelper
5
+
6
+ describe "Date Validation Step" do
7
+
8
+ describe "should be able to return a instance that validates dates" do
9
+
10
+ it "should not return any errors for a valid ISO8601 date" do
11
+ date = "2020-03-01"
12
+ assert_chain_verifies(type: :date, test_value: date, outcome: true)
13
+ end
14
+
15
+ it "should return an error for an invalid date" do
16
+ no_date = 1
17
+ assert_chain_verifies(type: :date, test_value: no_date, outcome: false)
18
+ end
19
+ end
20
+ end