rest-json-validator 0.0.0

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 +7 -0
  2. data/lib/rest-json-validator.rb +142 -0
  3. metadata +45 -0
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 161e085b2b258feae48399013b72a21daf2dac1b
4
+ data.tar.gz: 43564c0f528e0575730e40c7187874298140cb1d
5
+ SHA512:
6
+ metadata.gz: 9f8dbe289fbe9f756988f2d248b3c7ab88c5d8da7c755ba9908ccc45d1e8d81690b0b1f97bd25b9b7e6b6868b70769ded0829d616d9e223c34fb24cd2aaa6a42
7
+ data.tar.gz: 3359873f8a54e0d8260b5b30ebecfe8ea9db9495a6a619bffa75b0a6230e86a5c9d2197881b5bcf7e2de32d8515c635d5c475eb68e528e0c1f1f2ffd05660d6d
@@ -0,0 +1,142 @@
1
+ module RestJsonValidator
2
+ class JsonValidator
3
+
4
+ def initialize
5
+ @stack = []
6
+ @listeners = []
7
+ end
8
+
9
+ # a listener must implement a method called notify, input string
10
+ def add_listener(listener)
11
+ @listeners << listener
12
+ end
13
+
14
+ def notify(message)
15
+ @listeners.each do |l|
16
+ l.notify message
17
+ end
18
+ end
19
+
20
+ def is_field_validator?(api_element, api_key)
21
+ if [:composite_checker].include?(api_key)
22
+ retval = false
23
+ elsif api_element == :composite
24
+ retval = false
25
+ elsif api_element.class == Symbol
26
+ retval = true
27
+ else
28
+ retval = false
29
+ end
30
+ retval
31
+ end
32
+
33
+ def is_composite_checker?(api_element)
34
+ (api_element.respond_to? :has_key? and api_element.has_key?(:composite_checker))
35
+ end
36
+
37
+ def is_sub_composite_checker?(api_element)
38
+ (api_element.respond_to? :has_key? and api_element.has_key?(:sub_composite_checker))
39
+ end
40
+
41
+ def is_composite?(api_element)
42
+ api_element == :composite
43
+ end
44
+
45
+ def is_content_checker?(api_element)
46
+ api_element.is_a?(Array) and api_element[0].respond_to?(:has_key?) and api_element[0].has_key? :content_checker
47
+ end
48
+
49
+ def has_content_checker?(api_element)
50
+ api_element.is_a?(Array) and api_element[0].respond_to?(:has_key?) and api_element[0].has_key? :content_checker
51
+ end
52
+
53
+ def find_id(json)
54
+ id = nil
55
+ if json.nil?
56
+ puts "Trying to find id in a nil object (expected a json structure)"
57
+ elsif json.has_key? 'id'
58
+ id = json['id']
59
+ elsif json.has_key? 'programId'
60
+ id = json['programId']
61
+ elsif json.has_key? 'dataId'
62
+ id = json['dataId']
63
+ end
64
+ id
65
+ end
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')
70
+ else
71
+ api.keys.each do |api_key|
72
+ api_element = api[api_key]
73
+ id = find_id(json)
74
+ if is_field_validator?(api_element, api_key)
75
+ send(api_element, json[api_key], id, api_key)
76
+ elsif is_composite_checker?(api_element)
77
+ send(api_element[:composite_checker], json[api_key], id, api_key)
78
+ end
79
+ end
80
+ end
81
+ end
82
+
83
+ def validate_json(json, api, level)
84
+ key_diff(json, api, level)
85
+ run_field_validators(json, api, level)
86
+ end
87
+
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)
92
+ end
93
+ end
94
+
95
+ def depth_validate_hash(json, api, level)
96
+ @last_id = json['id']
97
+ validate_json(json, api, level)
98
+ json.keys.each do |api_key|
99
+ @stack.push(api_key)
100
+ validate_json_api_compliance(json[api_key], api[api_key], level)
101
+ @stack.pop
102
+ end
103
+ end
104
+
105
+ def validate_json_api_compliance(json, api, level=0)
106
+ return unless [Hash, Array].include? api.class
107
+ 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)
112
+ end
113
+ end
114
+
115
+ def key_diff(json, api, level)
116
+ optionals = []
117
+ if api.respond_to?(:has_key?)
118
+ optionals = api.has_key?(:optionals) ? api[:optionals].keys : []
119
+ end
120
+ api_keys = api.keys
121
+ api_keys.delete(:composite_checker)
122
+ api_keys.delete(:sub_composite_checker)
123
+ api_keys.delete(:optionals)
124
+ json_keys = json.keys
125
+ diff = []
126
+ if api_keys.sort != json_keys.sort
127
+ extra_api = api_keys - json_keys
128
+ extra_json = json_keys - api_keys
129
+ if extra_json.length == 0 and (extra_api - optionals).length == 0
130
+ return
131
+ else
132
+ diff << 'api - json: '
133
+ diff << api_keys - json_keys
134
+ diff << 'json - api: '
135
+ diff << json_keys - api_keys
136
+ message = ("Nivå #{level}-feil\n(#{@stack.join('.')})\nfor #{@url}\ndiff: #{diff}\n")
137
+ notify message: message
138
+ end
139
+ end
140
+ end
141
+ end
142
+ end
metadata ADDED
@@ -0,0 +1,45 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rest-json-validator
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.0
5
+ platform: ruby
6
+ authors:
7
+ - John Åsmund Westberg, Aril Spetalen, Magnus de Laval, Dagfinn Olsen
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-06-06 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: ''
14
+ email: dagfinno@gmail.com
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - lib/rest-json-validator.rb
20
+ homepage: http://rubygems.org/gems/hls-parser
21
+ licenses:
22
+ - MIT
23
+ metadata: {}
24
+ post_install_message:
25
+ rdoc_options: []
26
+ require_paths:
27
+ - lib
28
+ required_ruby_version: !ruby/object:Gem::Requirement
29
+ requirements:
30
+ - - '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ required_rubygems_version: !ruby/object:Gem::Requirement
34
+ requirements:
35
+ - - '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ requirements: []
39
+ rubyforge_project:
40
+ rubygems_version: 2.0.3
41
+ signing_key:
42
+ specification_version: 4
43
+ summary: ''
44
+ test_files: []
45
+ has_rdoc: