easy_json_matcher 0.2.1 → 0.2.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/easy_json_matcher/node.rb +1 -0
- data/lib/easy_json_matcher/node.rb~ +121 -0
- data/lib/easy_json_matcher/schema_generator.rb +1 -1
- data/lib/easy_json_matcher/schema_generator.rb~ +104 -0
- data/lib/easy_json_matcher/version.rb +1 -1
- data/test/custom_validations_test.rb +3 -4
- data/test/custom_validations_test.rb~ +25 -0
- data/test/easy_json_matcher_test.rb~ +262 -0
- data/test/reset_test.rb +6 -0
- data/test/reset_test.rb~ +32 -0
- metadata +9 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 29eb515cf0aae680f37f2c881b061c8d56fa5cc0
|
4
|
+
data.tar.gz: 5f617e24d4270c30f26e6bed85692402adad047e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 317e11578a3bd7420e88fa30ab294941a6d644a2303ecd2412cb2650973ed466ea565357455052bd0323acf4a1b30b4b9b63d89ce6b5266cb2205b09aadd6551
|
7
|
+
data.tar.gz: 5e12a27e3f4f436afec4951e26e8136df61662859a3b240c617b7c70d2d7aa891072724a976b1c6383334245446c654762b50da35652b88053feafb9c6b41ba9
|
@@ -0,0 +1,121 @@
|
|
1
|
+
require 'easy_json_matcher/validator_factory'
|
2
|
+
require 'json'
|
3
|
+
require 'easy_json_matcher/content_wrapper'
|
4
|
+
module EasyJSONMatcher
|
5
|
+
class Node < Validator
|
6
|
+
include ContentWrapper
|
7
|
+
|
8
|
+
attr_reader :validators, :validity, :strict
|
9
|
+
|
10
|
+
def initialize(opts: {})
|
11
|
+
super(options: opts)
|
12
|
+
@validators = []
|
13
|
+
@validity = true
|
14
|
+
end
|
15
|
+
|
16
|
+
def add_validator(validator)
|
17
|
+
validators << validator
|
18
|
+
end
|
19
|
+
|
20
|
+
def _post_initialise(options)
|
21
|
+
@strict = options[:strict]
|
22
|
+
end
|
23
|
+
|
24
|
+
def _validate
|
25
|
+
_validate_strict_keyset
|
26
|
+
validators.each do |val|
|
27
|
+
@validity = false unless _use_validator val
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
def reset!
|
32
|
+
errors.clear
|
33
|
+
validators.each(&:reset!)
|
34
|
+
end
|
35
|
+
|
36
|
+
def _validate_strict_keyset
|
37
|
+
_validate_keyset if strict
|
38
|
+
end
|
39
|
+
|
40
|
+
def _validate_keyset
|
41
|
+
unexpected_keys = keys - _expected_keys
|
42
|
+
errors << "#{unexpected_keys} found in addition to expected keys" unless unexpected_keys.empty?
|
43
|
+
end
|
44
|
+
|
45
|
+
def _expected_keys
|
46
|
+
validators.each_with_object([]) do |validator, keyset|
|
47
|
+
keyset << validator.key
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
def _use_validator(validator)
|
52
|
+
validator.valid? self
|
53
|
+
end
|
54
|
+
|
55
|
+
def _get_content_for(key)
|
56
|
+
content[key]
|
57
|
+
end
|
58
|
+
|
59
|
+
def _get_validator_for(key)
|
60
|
+
validators[key]
|
61
|
+
end
|
62
|
+
|
63
|
+
def _set_content(candidate)
|
64
|
+
@content = _is_root? ? _prep_root_content(candidate) : candidate[key]
|
65
|
+
end
|
66
|
+
|
67
|
+
def _is_root?
|
68
|
+
key.nil?
|
69
|
+
end
|
70
|
+
|
71
|
+
def get_errors
|
72
|
+
child_errors = _collect_child_errors
|
73
|
+
_wrap_errors(child_errors)
|
74
|
+
end
|
75
|
+
|
76
|
+
def _collect_child_errors
|
77
|
+
validators.each_with_object({}) do |val, container|
|
78
|
+
container.merge!(val.get_errors)
|
79
|
+
end
|
80
|
+
end
|
81
|
+
|
82
|
+
def _wrap_errors(child_errors)
|
83
|
+
_add_local_errors_to child_errors
|
84
|
+
unless _is_root?
|
85
|
+
_wrap_child_errors(child_errors)
|
86
|
+
else
|
87
|
+
_root_errors(child_errors)
|
88
|
+
end
|
89
|
+
end
|
90
|
+
|
91
|
+
def _add_local_errors_to(child_errors)
|
92
|
+
child_errors.merge!({node_errors_: errors}) unless errors.empty?
|
93
|
+
end
|
94
|
+
|
95
|
+
def _wrap_child_errors(child_errors)
|
96
|
+
errors_wrapper = {}
|
97
|
+
errors_wrapper[key] = child_errors
|
98
|
+
errors_wrapper
|
99
|
+
end
|
100
|
+
|
101
|
+
def _root_errors(child_errors)
|
102
|
+
errors.length > 0 ? {root: errors} : child_errors
|
103
|
+
end
|
104
|
+
|
105
|
+
def _prep_root_content(candidate)
|
106
|
+
candidate.is_a?(String) ? _parse_and_verify_json(candidate) : candidate
|
107
|
+
end
|
108
|
+
|
109
|
+
def _parse_and_verify_json(json)
|
110
|
+
begin
|
111
|
+
JSON.parse(json)
|
112
|
+
rescue JSON::ParserError
|
113
|
+
errors << '#{json} is not a valid JSON String'
|
114
|
+
end
|
115
|
+
end
|
116
|
+
|
117
|
+
def _no_errors?
|
118
|
+
validity && errors.empty?
|
119
|
+
end
|
120
|
+
end
|
121
|
+
end
|
@@ -0,0 +1,104 @@
|
|
1
|
+
require 'easy_json_matcher/validator_factory'
|
2
|
+
require 'easy_json_matcher/node'
|
3
|
+
require 'easy_json_matcher/schema_library'
|
4
|
+
require 'easy_json_matcher/exceptions'
|
5
|
+
module EasyJSONMatcher
|
6
|
+
class SchemaGenerator
|
7
|
+
|
8
|
+
attr_reader :node, :name, :options, :glob_opts
|
9
|
+
|
10
|
+
def initialize(opts: {}, global_opts: {})
|
11
|
+
@name = opts.delete(:key)
|
12
|
+
@options = opts
|
13
|
+
@glob_opts = global_opts
|
14
|
+
yield self if block_given?
|
15
|
+
end
|
16
|
+
|
17
|
+
def has_attribute(key:, opts: {})
|
18
|
+
node.add_validator(_create_validator(key, opts))
|
19
|
+
end
|
20
|
+
|
21
|
+
################ Methods for adding specific attribute types ##############
|
22
|
+
|
23
|
+
def contains_node(key:, opts: {})
|
24
|
+
generator = _node_generator _validator_opts(key, opts)
|
25
|
+
yield generator if block_given?
|
26
|
+
node.add_validator generator.generate_schema
|
27
|
+
end
|
28
|
+
|
29
|
+
def has_boolean(key:, opts: {})
|
30
|
+
has_attribute(key: key, opts: opts.merge({type: :boolean}))
|
31
|
+
end
|
32
|
+
|
33
|
+
def has_number(key: , opts: {})
|
34
|
+
has_attribute(key: key, opts: opts.merge({type: :number}))
|
35
|
+
end
|
36
|
+
|
37
|
+
def has_date(key:, opts: {})
|
38
|
+
has_attribute(key: key, opts: opts.merge({type: :date}))
|
39
|
+
end
|
40
|
+
|
41
|
+
def has_object(key:, opts: {})
|
42
|
+
has_attribute(key: key, opts: opts.merge({type: :object}))
|
43
|
+
end
|
44
|
+
|
45
|
+
def has_value(key:, opts: {})
|
46
|
+
has_attribute(key: key, opts: opts.merge({type: :value}))
|
47
|
+
end
|
48
|
+
|
49
|
+
def has_string(key:, opts: {})
|
50
|
+
has_attribute(key: key, opts: opts.merge({type: :string}))
|
51
|
+
end
|
52
|
+
|
53
|
+
def contains_array(key:, opts: {})
|
54
|
+
opts = opts.merge!({type: :array})
|
55
|
+
array_validator = _create_validator(key, opts)
|
56
|
+
yield array_validator if block_given?
|
57
|
+
node.add_validator array_validator
|
58
|
+
end
|
59
|
+
|
60
|
+
def has_schema(key:, opts: {})
|
61
|
+
has_attribute(key: key, opts: opts.merge({type: :schema}))
|
62
|
+
end
|
63
|
+
|
64
|
+
################ Methods for generating the schema #########################
|
65
|
+
|
66
|
+
def generate_schema
|
67
|
+
node
|
68
|
+
end
|
69
|
+
|
70
|
+
def register(as:)
|
71
|
+
SchemaLibrary.add_schema(name: as, schema: generate_schema)
|
72
|
+
generate_schema
|
73
|
+
end
|
74
|
+
|
75
|
+
################## Private methods #########################################
|
76
|
+
|
77
|
+
def _prep_schema_opts(schema_name, opts)
|
78
|
+
opts[:type] = :schema
|
79
|
+
opts[:name] = schema_name
|
80
|
+
opts
|
81
|
+
end
|
82
|
+
|
83
|
+
def _set_validator_key(validator, key)
|
84
|
+
validator.key = key
|
85
|
+
end
|
86
|
+
|
87
|
+
def _validator_opts(key, opts)
|
88
|
+
opts[:key] = key
|
89
|
+
glob_opts.merge(opts)
|
90
|
+
end
|
91
|
+
|
92
|
+
def _create_validator(key, opts)
|
93
|
+
ValidatorFactory.get_instance type: opts[:type], opts: _validator_opts(key, opts)
|
94
|
+
end
|
95
|
+
|
96
|
+
def _node_generator(opts = {})
|
97
|
+
self.class.new opts: opts, global_opts: glob_opts
|
98
|
+
end
|
99
|
+
|
100
|
+
def node
|
101
|
+
@node ||= Node.new(opts: _validator_opts(name, options))
|
102
|
+
end
|
103
|
+
end
|
104
|
+
end
|
@@ -4,23 +4,22 @@ class CustomValidationsTest < ActiveSupport::TestCase
|
|
4
4
|
|
5
5
|
test 'As a user, I want to be able to specify custom validations' do
|
6
6
|
test_schema = EasyJSONMatcher::SchemaGenerator.new do |s|
|
7
|
-
|
7
|
+
string_validator = ->(candidate) { "String did not say 'hi'" unless candidate == 'hi' }
|
8
|
+
s.has_string key: :a_string, opts: { custom_validator: string_validator }
|
8
9
|
end.generate_schema
|
9
10
|
|
10
11
|
should_not_validate = {
|
11
12
|
a_string: 'go away'
|
12
13
|
}.to_json
|
13
14
|
|
14
|
-
assert_not(test_schema.valid?
|
15
|
+
assert_not(test_schema.valid?(should_not_validate))
|
15
16
|
|
16
17
|
should_validate = {
|
17
18
|
a_string: 'hi'
|
18
19
|
}.to_json
|
19
20
|
|
20
|
-
|
21
21
|
test_schema.reset!
|
22
22
|
|
23
23
|
assert(test_schema.valid?(should_validate), test_schema.get_errors)
|
24
|
-
|
25
24
|
end
|
26
25
|
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class CustomValidationsTest < ActiveSupport::TestCase
|
4
|
+
|
5
|
+
test 'As a user, I want to be able to specify custom validations' do
|
6
|
+
test_schema = EasyJSONMatcher::SchemaGenerator.new do |s|
|
7
|
+
string_validator = ->(candidate) { "String did not say 'hi'" unless candidate == 'hi' }
|
8
|
+
s.has_string key: :a_string, opts: { custom_validator: string_validator }
|
9
|
+
end.generate_schema
|
10
|
+
|
11
|
+
should_not_validate = {
|
12
|
+
a_string: 'go away'
|
13
|
+
}.to_json
|
14
|
+
|
15
|
+
assert_not(test_schema.valid?(should_not_validate))
|
16
|
+
|
17
|
+
should_validate = {
|
18
|
+
a_string: 'hi'
|
19
|
+
}.to_json
|
20
|
+
|
21
|
+
test_schema.reset!
|
22
|
+
|
23
|
+
assert(test_schema.valid?(should_validate), test_schema.get_errors)
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,262 @@
|
|
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
|
data/test/reset_test.rb
CHANGED
@@ -19,6 +19,12 @@ class ResetTest < ActiveSupport::TestCase
|
|
19
19
|
|
20
20
|
test_schema.reset!
|
21
21
|
|
22
|
+
valid_json = {
|
23
|
+
bool: true
|
24
|
+
}.to_json
|
25
|
+
|
26
|
+
assert(test_schema.valid?(valid_json), test_schema.get_errors)
|
27
|
+
|
22
28
|
errors = test_schema.get_errors
|
23
29
|
assert errors[:node][:val].empty?
|
24
30
|
assert errors[:bool].empty?
|
data/test/reset_test.rb~
ADDED
@@ -0,0 +1,32 @@
|
|
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
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: easy_json_matcher
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- WJD Hamilton
|
@@ -27,9 +27,11 @@ files:
|
|
27
27
|
- lib/easy_json_matcher/date_validator.rb
|
28
28
|
- lib/easy_json_matcher/exceptions.rb
|
29
29
|
- lib/easy_json_matcher/node.rb
|
30
|
+
- lib/easy_json_matcher/node.rb~
|
30
31
|
- lib/easy_json_matcher/number_validator.rb
|
31
32
|
- lib/easy_json_matcher/object_validator.rb
|
32
33
|
- lib/easy_json_matcher/schema_generator.rb
|
34
|
+
- lib/easy_json_matcher/schema_generator.rb~
|
33
35
|
- lib/easy_json_matcher/schema_library.rb
|
34
36
|
- lib/easy_json_matcher/string_validator.rb
|
35
37
|
- lib/easy_json_matcher/validation_error.rb
|
@@ -41,13 +43,16 @@ files:
|
|
41
43
|
- lib/easy_json_matcher/version.rb~
|
42
44
|
- lib/tasks/jsonapi_matcher_tasks.rake
|
43
45
|
- test/custom_validations_test.rb
|
46
|
+
- test/custom_validations_test.rb~
|
44
47
|
- test/easy_json_matcher_test.rb
|
48
|
+
- test/easy_json_matcher_test.rb~
|
45
49
|
- test/error_messages_test.rb
|
46
50
|
- test/global_validation_options_test.rb
|
47
51
|
- test/json_api_home.txt
|
48
52
|
- test/managing_schemas_test.rb
|
49
53
|
- test/required_validation_test.rb
|
50
54
|
- test/reset_test.rb
|
55
|
+
- test/reset_test.rb~
|
51
56
|
- test/strict_mode_test.rb
|
52
57
|
- test/test_helper.rb
|
53
58
|
- test/validating_arrays_test.rb
|
@@ -77,13 +82,16 @@ specification_version: 4
|
|
77
82
|
summary: Easily test your JSON output with templates and Schemas
|
78
83
|
test_files:
|
79
84
|
- test/custom_validations_test.rb
|
85
|
+
- test/custom_validations_test.rb~
|
80
86
|
- test/easy_json_matcher_test.rb
|
87
|
+
- test/easy_json_matcher_test.rb~
|
81
88
|
- test/error_messages_test.rb
|
82
89
|
- test/global_validation_options_test.rb
|
83
90
|
- test/json_api_home.txt
|
84
91
|
- test/managing_schemas_test.rb
|
85
92
|
- test/required_validation_test.rb
|
86
93
|
- test/reset_test.rb
|
94
|
+
- test/reset_test.rb~
|
87
95
|
- test/strict_mode_test.rb
|
88
96
|
- test/test_helper.rb
|
89
97
|
- test/validating_arrays_test.rb
|