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
@@ -1,262 +0,0 @@
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
- class EasyJSONMatcherTest < ActiveSupport::TestCase
7
-
8
- test "As a user I want to create new Schemas to match JSON objects" do
9
- # This test represents the minimum level of implementation required to create a
10
- # working node.
11
- test_schema = EasyJSONMatcher::SchemaGenerator.new { |schema|
12
- schema.contains_node(key: :data) do |node|
13
- node.has_attribute(key: :title, opts: {type: :string})
14
- end
15
- }.register(as: :test)
16
-
17
- valid_json = {
18
- data: {
19
- 'title'=> "here's a title"
20
- }
21
- }.to_json
22
- assert(test_schema.valid? valid_json)
23
- end
24
-
25
- # The first thing the gem ought to do is to check that the JSON candidate is actually JSON
26
- test "As a user, if the validation candidate cannot be parsed as JSON, the schema should not be valid" do
27
- test_schema = EasyJSONMatcher::SchemaGenerator.new {|s|
28
- s.has_number(key: :population_of_china_1970)
29
- }.generate_schema
30
-
31
- invalid_json = "'population_of_china_1970' 810000000"
32
-
33
- assert_not(test_schema.valid? invalid_json)
34
- end
35
-
36
-
37
- test "As a user I want to be able to validate strings" do
38
- test_schema = EasyJSONMatcher::SchemaGenerator.new {|schema|
39
- schema.has_string(key: :string, opts: { required: :true})
40
- }.generate_schema
41
-
42
- valid_json = {
43
- string: "Mrs Mogs Hamilton"
44
- }.to_json
45
-
46
- assert(test_schema.valid?(valid_json), 'String was not validated')
47
-
48
- # There isn't really a clear case for the string validator picking up if a
49
- # value is not intended to be a string since all json values are effectively
50
- # strings, and how is the library to know if the client meant 16 to be passed
51
- # as a number or as a string?
52
- # invalid_json = {
53
- # string: 16
54
- # }.to_json
55
- #
56
- # assert_not(test_schema.valid?(invalid_json), 'Number was validated as a string')
57
- end
58
-
59
-
60
- test "As a user I want to be able to validate numbers" do
61
-
62
- test_schema = EasyJSONMatcher::SchemaGenerator.new {|schema|
63
- schema.has_number(key: :number, opts: {required: :true})
64
- }.generate_schema
65
-
66
- valid_json = {
67
- number: 5.55,
68
- }.to_json
69
-
70
- assert(test_schema.valid?(valid_json), "Number was not validated")
71
-
72
- invalid_json = {
73
- number: "hi"
74
- }.to_json
75
- assert_not(test_schema.valid?(invalid_json), "\"hi\" should not have been valid")
76
-
77
- invalid_nil = {
78
- number: nil
79
- }.to_json
80
- assert_not(test_schema.valid?(invalid_nil), "#{invalid_nil} should not have validated, or thrown an error")
81
- end
82
-
83
- test "As a user I want to be able to validate booleans" do
84
-
85
- test_schema = EasyJSONMatcher::SchemaGenerator.new {|schema|
86
- schema.has_boolean(key: :true)
87
- schema.has_boolean(key: :false)
88
- }.generate_schema
89
-
90
- valid_json = {
91
- true: true,
92
- false: false
93
- }.to_json
94
-
95
- assert(test_schema.valid?(valid_json), "Boolean was not validated")
96
-
97
- invalid_json = {
98
- true: 1,
99
- false: "wibble"
100
- }.to_json
101
-
102
- # byebug
103
- assert_not(test_schema.valid?(invalid_json), "\"1\" and \"wibble\" are not valid boolean values")
104
- end
105
-
106
- test "As a user I want to be able to validate Array values" do
107
- test_schema = EasyJSONMatcher::SchemaGenerator.new {|schema|
108
- schema.has_attribute(key: :array, opts: {type: :array})
109
- }.generate_schema
110
-
111
- valid_json = {
112
- array: []
113
- }.to_json
114
-
115
- assert(test_schema.valid?(valid_json), "Array was not validated")
116
-
117
- invalid_json = {
118
- array: 1
119
- }.to_json
120
-
121
- assert(!test_schema.valid?(invalid_json), "\"1\" is not a valid array value")
122
- end
123
-
124
- test "As a user I want to be able to validate date values" do
125
- test_schema = EasyJSONMatcher::SchemaGenerator.new { |schema|
126
- schema.has_date(key: :date)
127
- }.generate_schema
128
-
129
- valid_json = {
130
- date: "2015-01-15"
131
- }.to_json
132
-
133
- assert(test_schema.valid?(valid_json), "Date was not validated")
134
-
135
- not_a_date = 'Good Night Mr. Tom'
136
- invalid_json = {
137
- date: not_a_date
138
- }.to_json
139
-
140
- assert_not(test_schema.valid?(invalid_json), "\"#{not_a_date}\" should not have been validated as a date")
141
- end
142
-
143
- test "As a user I want to be able to use different types of date format" do
144
- flunk "Implement me"
145
- end
146
-
147
- test "As a user I want to validate object values" do
148
- test_schema = EasyJSONMatcher::SchemaGenerator.new { |schema|
149
- schema.has_object(key: :object)
150
- }.generate_schema
151
-
152
- is_an_object = {}
153
-
154
- valid_json = {
155
- object: is_an_object
156
- }.to_json
157
-
158
- assert(test_schema.valid?(valid_json),"#{is_an_object} was not validated as an object" )
159
-
160
- not_an_object = "Popular Music"
161
-
162
- invalid_json = {
163
- object: not_an_object
164
- }.to_json
165
-
166
- assert_not(test_schema.valid?(invalid_json), "#{not_an_object} should not have validated as an object")
167
- end
168
-
169
- # Refers to validation of a JSON value attribute. This one is slightly tricky
170
- # though since attempting to access a Ruby Hash with a missing key will return
171
- # nil. The ValueValidator (or indeed any Validator) will accept nil as a value
172
- # when the value is not marked as required.
173
- # ValueValidator we will be stuck because although the other Validator
174
- # classes nil indicates a missing value, in the case of the ValueValidator null is
175
- # a valid value and we just want to check that there is a key available.
176
- test "As a user I want to validate json value attributes" do
177
- test_schema = EasyJSONMatcher::SchemaGenerator.new {|schema|
178
- schema.has_value(key: :array)
179
- schema.has_value(key: :boolean)
180
- schema.has_value(key: :date)
181
- schema.has_value(key: :number)
182
- schema.has_value(key: :object)
183
- schema.has_value(key: :string)
184
- schema.has_value(key: :null)
185
- }.generate_schema
186
-
187
-
188
- valid_json = {
189
- array: [],
190
- boolean: true,
191
- date: Date.today,
192
- number: 1.11,
193
- object: {},
194
- string: 'The Tenderness of Wolves',
195
- null: nil
196
- }.to_json
197
-
198
- assert(test_schema.valid?(valid_json), 'Value did not validate')
199
-
200
- # There is no 'negative' test for this validator at this stage, since
201
- # the lack of a value does not mean the key is required. See the tests
202
- # on required validation later on.
203
- end
204
-
205
- test "As a user I want to validate nested json objects" do
206
- test_schema = EasyJSONMatcher::SchemaGenerator.new {|schema|
207
- schema.has_attribute(key: :level_1_attribute, opts: {type: :number})
208
- schema.contains_node(key: :level_2) do |n|
209
- n.has_attribute(key: :level_2_attribute, opts: {type: :number})
210
- n.contains_node(key: :level_3) do |n3|
211
- n3.has_attribute(key: :level_3_attribute, opts: {type: :number})
212
- end
213
- end
214
- }.generate_schema
215
-
216
- valid_json = {
217
- level_1_attribute: 1,
218
- level_2:{
219
- level_2_attribute: 2,
220
- level_3:{
221
- level_3_attribute: 3
222
- }
223
- }
224
- }.to_json
225
-
226
- assert(test_schema.valid?(valid_json), "Nested JSON was not correctly validated")
227
- end
228
-
229
- test "As a user, if I specify a node and the content is not a node, it should be invalid without raising an error" do
230
- test_schema = EasyJSONMatcher::SchemaGenerator.new {|schema|
231
- schema.has_attribute(key: :fish_name, opts: {type: :string, required: :true})
232
- schema.contains_node(key: :scientific_name) do |n|
233
- n.has_attribute(key: :genus, opts: {type: :string, required: :true})
234
- n.has_attribute(key: :species, opts: {type: :string, required: :true})
235
- end
236
- }.generate_schema
237
-
238
- valid_json = {
239
- fish_name: 'Clownfish',
240
- scientific_name: {
241
- genus: 'Amphiprion',
242
- species: 'ocellaris'
243
- }
244
- }.to_json
245
-
246
- assert(test_schema.valid?(valid_json), "#{valid_json} should have been valid")
247
-
248
- invalid_with_array = {
249
- fish_name: 'Green Mandarin',
250
- scientific_name: ['Synchiropus', 'splendidus']
251
- }.to_json
252
-
253
- assert_not(test_schema.valid?(invalid_with_array), "#{invalid_with_array} should not have been valid as it has an array instead of a node")
254
-
255
- invalid_with_primitive = {
256
- fish_name: 'Hawaiian Tang',
257
- scientific_name: 'Zebrasoma flavescens'
258
- }.to_json
259
-
260
- assert_not(test_schema.valid?(invalid_with_primitive), "#{invalid_with_primitive} shoudl not have been valid as it has a primite instead of a node")
261
- end
262
- end
@@ -1,148 +0,0 @@
1
- require 'test_helper'
2
-
3
- class ErrorMessagesTest < ActiveSupport::TestCase
4
-
5
- test "As a user I want to know why my json was not valid" do
6
-
7
- test_schema = EasyJSONMatcher::SchemaGenerator.new { |schema|
8
- schema.has_string key: :oops
9
- schema.has_value key: :ok
10
- schema.contains_node(key: :nested_oops) do |node|
11
- node.has_value key: :ok
12
- node.has_string key: :bigger_oops
13
- end
14
- }.generate_schema
15
-
16
- has_errors = {
17
- oops: 1,
18
- ok: 'ok',
19
- nested_oops: {
20
- ok: 'ok',
21
- bigger_oops: 2
22
- }
23
- }.to_json
24
-
25
- # The resulting error object should show an error for oops, and an error in the
26
- # :nested_oops object for :bigger_oops. It should not show any errors for either
27
- # of the :oks.
28
-
29
- # Generate error messages. Better test that the thing is definitely invalid too...
30
- assert_not(test_schema.valid? has_errors)
31
-
32
- assert_match(/.*is not a String/, test_schema.get_errors[:oops][0])
33
- assert_match( /.*is not a String/, test_schema.get_errors[:nested_oops][:bigger_oops][0])
34
- end
35
-
36
- test "As a user, given that I have specified that an array should be mapped to a key and that the actual value
37
- is not an array, I want to know that the value is not an array" do
38
-
39
- test_schema = EasyJSONMatcher::SchemaGenerator.new do |s|
40
- s.contains_array(key: :arr)
41
- end.generate_schema
42
-
43
- wrong_type = {
44
- arr: "This not an array"
45
- }.to_json
46
-
47
- #As above just check that the validator is actually behaving itself
48
- assert_not(test_schema.valid? wrong_type)
49
-
50
- assert_match(/.*is not an Array/, test_schema.get_errors[:arr][0])
51
- end
52
-
53
- test "As a user, given that I have specified that an array should contain a specific
54
- type of value and the array contains other types of value, I want to know which
55
- value was the wrong type and why" do
56
-
57
- test_schema = EasyJSONMatcher::SchemaGenerator.new {|s|
58
- s.contains_array key: :array do |a|
59
- a.should_only_contain type: :string
60
- end
61
- }.generate_schema
62
-
63
- episodes_where_sheldon_says_bazinga_in_series_2 = {
64
- array: [1,2,3]
65
- }.to_json
66
-
67
- assert_not(test_schema.valid? episodes_where_sheldon_says_bazinga_in_series_2)
68
- assert_match(/.* is not a String/, test_schema.get_errors[:array][0][0])
69
- end
70
-
71
- test "As a user, given that I have specified that a boolean should map to a given
72
- key and that the actual value is not a boolean, I want to now that the
73
- value is not a boolean" do
74
-
75
- test_schema = EasyJSONMatcher::SchemaGenerator.new { |s|
76
- s.has_boolean(key: :bool)
77
- }.generate_schema
78
-
79
- no_bool = {
80
- bool: "false"
81
- }.to_json
82
-
83
- assert_not(test_schema.valid? no_bool)
84
-
85
- assert_match(/.* is not a Boolean/, test_schema.get_errors[:bool][0])
86
- end
87
-
88
- test "As a user, given that I have specified that a date should map to a given key
89
- and that the actual value is not a date, I want to be informed that the value
90
- is not a date" do
91
-
92
- test_schema = EasyJSONMatcher::SchemaGenerator.new {|s|
93
- s.has_date(key: :date)
94
- }.generate_schema
95
-
96
- no_date = {
97
- date: "hello world"
98
- }.to_json
99
-
100
- assert_not(test_schema.valid? no_date)
101
-
102
- assert_match(/.* is not a Date/, test_schema.get_errors[:date][0])
103
- end
104
-
105
- test "As a user, given that I have specified that a value should be a number,
106
- I want to be informed that the value was not a number" do
107
-
108
- test_schema = EasyJSONMatcher::SchemaGenerator.new {|s|
109
- s.has_number(key: :number)
110
- }.generate_schema
111
-
112
- no_number = {
113
- number: 'six'
114
- }.to_json
115
-
116
-
117
- assert_not(test_schema.valid? no_number)
118
- assert_match(/.* is not a Number/, test_schema.get_errors[:number][0])
119
- end
120
-
121
- test "As a user, given that I have specified that a value should be an object,
122
- I want an error message to inform me if a value is not an object" do
123
-
124
- test_schema = EasyJSONMatcher::SchemaGenerator.new {|s|
125
- s.has_object(key: :object)
126
- }.generate_schema
127
-
128
- no_object = {
129
- object: false
130
- }.to_json
131
-
132
- assert_not(test_schema.valid? no_object)
133
- assert_match(/.* is not an Object/, test_schema.get_errors[:object][0])
134
- end
135
-
136
- test "As a user, given that I have supplied an invalid JSON object, I want an
137
- error message to inform me that the candidate could not be parsed" do
138
-
139
- test_schema = EasyJSONMatcher::SchemaGenerator.new {|s|
140
- s.has_object(key: :object)
141
- }.generate_schema
142
-
143
- not_json = 'this is not a JSON String'
144
-
145
- test_schema.valid? not_json
146
- assert_match(/.* is not a valid JSON String/, test_schema.get_errors[:root][0])
147
- end
148
- end
data/test/reset_test.rb DELETED
@@ -1,32 +0,0 @@
1
- require 'test_helper'
2
- class ResetTest < ActiveSupport::TestCase
3
-
4
- test 'As a user, I want to be able to reuse a validator' do
5
- # In other words, after a valid? has been called, the error messages need to be cleared
6
- test_schema = EasyJSONMatcher::SchemaGenerator.new(global_opts: { strict: true }) { |sc|
7
- sc.has_boolean key: :bool, opts: { required: true }
8
- sc.contains_node key: :node do |n|
9
- n.has_value key: :val
10
- end
11
- }.generate_schema
12
-
13
- invalid_json = {
14
-
15
- }.to_json
16
-
17
- assert_not(test_schema.valid?(invalid_json))
18
- assert_not(test_schema.get_errors[:bool][0].nil?)
19
-
20
- test_schema.reset!
21
-
22
- valid_json = {
23
- bool: true
24
- }.to_json
25
-
26
- assert(test_schema.valid?(valid_json), test_schema.get_errors)
27
-
28
- errors = test_schema.get_errors
29
- assert errors[:node][:val].empty?
30
- assert errors[:bool].empty?
31
- end
32
- end
data/test/reset_test.rb~ DELETED
@@ -1,32 +0,0 @@
1
- require 'test_helper'
2
- class ResetTest < ActiveSupport::TestCase
3
-
4
- test 'As a user, I want to be able to reuse a validator' do
5
- # In other words, after a valid? has been called, the error messages need to be cleared
6
- test_schema = EasyJSONMatcher::SchemaGenerator.new(global_opts: { strict: true }) { |sc|
7
- sc.has_boolean key: :bool, opts: { required: true }
8
- sc.contains_node key: :node do |n|
9
- n.has_value key: :val
10
- end
11
- }.generate_schema
12
-
13
- invalid_json = {
14
-
15
- }.to_json
16
-
17
- assert_not(test_schema.valid?(invalid_json))
18
- assert_not(test_schema.get_errors[:bool][0].nil?)
19
-
20
- test_schema.reset!
21
-
22
- valid_json = {
23
- bool: true
24
- }.to_json
25
-
26
- assert(test_schema.valid?(valid_json))
27
-
28
- errors = test_schema.get_errors
29
- assert errors[:node][:val].empty?
30
- assert errors[:bool].empty?
31
- end
32
- end