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.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/rest-json-validator.rb +30 -29
  3. metadata +5 -6
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 161e085b2b258feae48399013b72a21daf2dac1b
4
- data.tar.gz: 43564c0f528e0575730e40c7187874298140cb1d
3
+ metadata.gz: 89d6c21b73964870660f4228ceb411960176b2ac
4
+ data.tar.gz: 30474fa342e95e240cd18f33005f174f2fc618fc
5
5
  SHA512:
6
- metadata.gz: 9f8dbe289fbe9f756988f2d248b3c7ab88c5d8da7c755ba9908ccc45d1e8d81690b0b1f97bd25b9b7e6b6868b70769ded0829d616d9e223c34fb24cd2aaa6a42
7
- data.tar.gz: 3359873f8a54e0d8260b5b30ebecfe8ea9db9495a6a619bffa75b0a6230e86a5c9d2197881b5bcf7e2de32d8515c635d5c475eb68e528e0c1f1f2ffd05660d6d
6
+ metadata.gz: ce86e2a7ee16a1985f0b2f810ac2e0381b5e4b72aec8afa778fa4c9d02466abcec5a70451c19b178ae6d342a2b26806d9e43da4f7edd9fda62abcb113f40ac3f
7
+ data.tar.gz: 21d7aa6c59625fe52ed3dbb2b91d95ad0c42fcbd5c30ecdfa69d8bf970fd8df9a59085a8991846ed1e60690251a2b06bedfb70a63b2ff96e39217323bfc05910
@@ -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 json structure)"
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(json, api, level)
68
- if is_sub_composite_checker?(api)
69
- send(api[:sub_composite_checker], json, find_id(json), 'vetikke')
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
- api.keys.each do |api_key|
72
- api_element = api[api_key]
73
- id = find_id(json)
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, json[api_key], id, api_key)
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], json[api_key], id, api_key)
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(json, api, level)
84
- key_diff(json, api, level)
85
- run_field_validators(json, api, level)
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(json, api, level, content_checker=nil)
89
- send(content_checker.intern, json, @media_id, level) unless content_checker.nil?
90
- json.each do |json_element|
91
- validate_json_api_compliance(json_element, api, level)
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(json, api, level=0)
106
- return unless [Hash, Array].include? api.class
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 json.class == Array
109
- depth_validate_array(json, api[0][:data], level, api[0][:content_checker])
110
- elsif json.class == Hash
111
- depth_validate_hash(json, api, level)
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(json, api, level)
116
+ def key_diff(actual_json, specification, level)
116
117
  optionals = []
117
- if api.respond_to?(:has_key?)
118
- optionals = api.has_key?(:optionals) ? api[:optionals].keys : []
118
+ if specification.respond_to?(:has_key?)
119
+ optionals = specification.has_key?(:optionals) ? specification[:optionals].keys : []
119
120
  end
120
- api_keys = 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 = 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 << 'api - json: '
133
+ diff << 'specification - actual_json: '
133
134
  diff << api_keys - json_keys
134
- diff << 'json - api: '
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.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-06 00:00:00.000000000 Z
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.0.3
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: