rest-json-validator 0.0.0 → 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/rest-json-validator.rb +30 -29
- metadata +5 -6
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 89d6c21b73964870660f4228ceb411960176b2ac
|
4
|
+
data.tar.gz: 30474fa342e95e240cd18f33005f174f2fc618fc
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ce86e2a7ee16a1985f0b2f810ac2e0381b5e4b72aec8afa778fa4c9d02466abcec5a70451c19b178ae6d342a2b26806d9e43da4f7edd9fda62abcb113f40ac3f
|
7
|
+
data.tar.gz: 21d7aa6c59625fe52ed3dbb2b91d95ad0c42fcbd5c30ecdfa69d8bf970fd8df9a59085a8991846ed1e60690251a2b06bedfb70a63b2ff96e39217323bfc05910
|
data/lib/rest-json-validator.rb
CHANGED
@@ -53,7 +53,7 @@ module RestJsonValidator
|
|
53
53
|
def find_id(json)
|
54
54
|
id = nil
|
55
55
|
if json.nil?
|
56
|
-
puts "Trying to find id in a nil object (expected a
|
56
|
+
puts "Warning: Trying to find id in a nil object (expected a actual_json structure)"
|
57
57
|
elsif json.has_key? 'id'
|
58
58
|
id = json['id']
|
59
59
|
elsif json.has_key? 'programId'
|
@@ -64,31 +64,32 @@ module RestJsonValidator
|
|
64
64
|
id
|
65
65
|
end
|
66
66
|
|
67
|
-
def run_field_validators(
|
68
|
-
if is_sub_composite_checker?(
|
69
|
-
send(
|
67
|
+
def run_field_validators(actual_json, specification, level)
|
68
|
+
if is_sub_composite_checker?(specification)
|
69
|
+
send(specification[:sub_composite_checker], actual_json, find_id(actual_json), 'vetikke')
|
70
70
|
else
|
71
|
-
|
72
|
-
api_element =
|
73
|
-
id = find_id(
|
71
|
+
specification.keys.each do |api_key|
|
72
|
+
api_element = specification[api_key]
|
73
|
+
id = find_id(actual_json)
|
74
74
|
if is_field_validator?(api_element, api_key)
|
75
|
-
send(api_element,
|
75
|
+
send(api_element, actual_json[api_key], id, api_key)
|
76
76
|
elsif is_composite_checker?(api_element)
|
77
|
-
send(api_element[:composite_checker],
|
77
|
+
send(api_element[:composite_checker], actual_json[api_key], id, api_key)
|
78
78
|
end
|
79
79
|
end
|
80
80
|
end
|
81
81
|
end
|
82
82
|
|
83
|
-
def validate_json(
|
84
|
-
|
85
|
-
|
83
|
+
def validate_json(actual_json, specification, level)
|
84
|
+
# puts "son: #{actual_json} api: #{api} level: #{level}"
|
85
|
+
key_diff(actual_json, specification, level)
|
86
|
+
run_field_validators(actual_json, specification, level)
|
86
87
|
end
|
87
88
|
|
88
|
-
def depth_validate_array(
|
89
|
-
send(content_checker.intern,
|
90
|
-
|
91
|
-
validate_json_api_compliance(json_element,
|
89
|
+
def depth_validate_array(actual_json, specification, level, content_checker=nil)
|
90
|
+
send(content_checker.intern, actual_json, @media_id, level) unless content_checker.nil?
|
91
|
+
actual_json.each do |json_element|
|
92
|
+
validate_json_api_compliance(json_element, specification, level)
|
92
93
|
end
|
93
94
|
end
|
94
95
|
|
@@ -102,26 +103,26 @@ module RestJsonValidator
|
|
102
103
|
end
|
103
104
|
end
|
104
105
|
|
105
|
-
def validate_json_api_compliance(
|
106
|
-
return unless [Hash, Array].include?
|
106
|
+
def validate_json_api_compliance(actual_json, specification, level=0)
|
107
|
+
return unless [Hash, Array].include? specification.class
|
107
108
|
level += 1
|
108
|
-
if
|
109
|
-
depth_validate_array(
|
110
|
-
elsif
|
111
|
-
depth_validate_hash(
|
109
|
+
if actual_json.class == Array
|
110
|
+
depth_validate_array(actual_json, specification[0][:data], level, specification[0][:content_checker])
|
111
|
+
elsif actual_json.class == Hash
|
112
|
+
depth_validate_hash(actual_json, specification, level)
|
112
113
|
end
|
113
114
|
end
|
114
115
|
|
115
|
-
def key_diff(
|
116
|
+
def key_diff(actual_json, specification, level)
|
116
117
|
optionals = []
|
117
|
-
if
|
118
|
-
optionals =
|
118
|
+
if specification.respond_to?(:has_key?)
|
119
|
+
optionals = specification.has_key?(:optionals) ? specification[:optionals].keys : []
|
119
120
|
end
|
120
|
-
api_keys =
|
121
|
+
api_keys = specification.keys
|
121
122
|
api_keys.delete(:composite_checker)
|
122
123
|
api_keys.delete(:sub_composite_checker)
|
123
124
|
api_keys.delete(:optionals)
|
124
|
-
json_keys =
|
125
|
+
json_keys = actual_json.keys
|
125
126
|
diff = []
|
126
127
|
if api_keys.sort != json_keys.sort
|
127
128
|
extra_api = api_keys - json_keys
|
@@ -129,9 +130,9 @@ module RestJsonValidator
|
|
129
130
|
if extra_json.length == 0 and (extra_api - optionals).length == 0
|
130
131
|
return
|
131
132
|
else
|
132
|
-
diff << '
|
133
|
+
diff << 'specification - actual_json: '
|
133
134
|
diff << api_keys - json_keys
|
134
|
-
diff << '
|
135
|
+
diff << 'actual_json - specification: '
|
135
136
|
diff << json_keys - api_keys
|
136
137
|
message = ("Nivå #{level}-feil\n(#{@stack.join('.')})\nfor #{@url}\ndiff: #{diff}\n")
|
137
138
|
notify message: message
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rest-json-validator
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- John Åsmund Westberg, Aril Spetalen, Magnus de Laval, Dagfinn Olsen
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-06-
|
11
|
+
date: 2017-06-08 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: ''
|
14
14
|
email: dagfinno@gmail.com
|
@@ -27,19 +27,18 @@ require_paths:
|
|
27
27
|
- lib
|
28
28
|
required_ruby_version: !ruby/object:Gem::Requirement
|
29
29
|
requirements:
|
30
|
-
- -
|
30
|
+
- - ">="
|
31
31
|
- !ruby/object:Gem::Version
|
32
32
|
version: '0'
|
33
33
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
34
34
|
requirements:
|
35
|
-
- -
|
35
|
+
- - ">="
|
36
36
|
- !ruby/object:Gem::Version
|
37
37
|
version: '0'
|
38
38
|
requirements: []
|
39
39
|
rubyforge_project:
|
40
|
-
rubygems_version: 2.
|
40
|
+
rubygems_version: 2.6.12
|
41
41
|
signing_key:
|
42
42
|
specification_version: 4
|
43
43
|
summary: ''
|
44
44
|
test_files: []
|
45
|
-
has_rdoc:
|