Class: RestJsonValidator::JsonValidator
- Inherits:
-
Object
- Object
- RestJsonValidator::JsonValidator
- Defined in:
- lib/rest-json-validator.rb
Instance Method Summary collapse
-
#add_listener(listener) ⇒ Object
a listener must implement a method called notify, input string.
- #depth_validate_array(actual_json, specification, level, content_checker = nil) ⇒ Object
- #depth_validate_hash(json, api, level) ⇒ Object
- #find_id(json) ⇒ Object
- #has_content_checker?(api_element) ⇒ Boolean
-
#initialize(receiver: nil) ⇒ JsonValidator
constructor
A new instance of JsonValidator.
- #is_composite?(api_element) ⇒ Boolean
- #is_composite_checker?(api_element) ⇒ Boolean
- #is_content_checker?(api_element) ⇒ Boolean
- #is_field_validator?(api_element, api_key) ⇒ Boolean
- #is_sub_composite_checker?(api_element) ⇒ Boolean
- #key_diff(actual_json, specification, level) ⇒ Object
- #notify(message) ⇒ Object
- #run_field_validators(actual_json, specification, level) ⇒ Object
- #validate_json(actual_json, specification, level) ⇒ Object
- #validate_json_api_compliance(actual_json, specification, level = 0) ⇒ Object
Constructor Details
#initialize(receiver: nil) ⇒ JsonValidator
Returns a new instance of JsonValidator.
4 5 6 7 8 |
# File 'lib/rest-json-validator.rb', line 4 def initialize(receiver: nil) @stack = [] @listeners = [] @receiver = receiver || self end |
Instance Method Details
#add_listener(listener) ⇒ Object
a listener must implement a method called notify, input string
11 12 13 |
# File 'lib/rest-json-validator.rb', line 11 def add_listener(listener) @listeners << listener end |
#depth_validate_array(actual_json, specification, level, content_checker = nil) ⇒ Object
90 91 92 93 94 95 |
# File 'lib/rest-json-validator.rb', line 90 def depth_validate_array(actual_json, specification, level, content_checker=nil) @receiver.send(content_checker.intern, actual_json, @media_id, level) unless content_checker.nil? actual_json.each do |json_element| validate_json_api_compliance(json_element, specification, level) end end |
#depth_validate_hash(json, api, level) ⇒ Object
97 98 99 100 101 102 103 104 105 |
# File 'lib/rest-json-validator.rb', line 97 def depth_validate_hash(json, api, level) @last_id = json['id'] validate_json(json, api, level) json.keys.each do |api_key| @stack.push(api_key) validate_json_api_compliance(json[api_key], api[api_key], level) @stack.pop end end |
#find_id(json) ⇒ Object
54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/rest-json-validator.rb', line 54 def find_id(json) id = nil if json.nil? puts "Warning: Trying to find id in a nil object (expected a actual_json structure)" elsif json.has_key? 'id' id = json['id'] elsif json.has_key? 'programId' id = json['programId'] elsif json.has_key? 'dataId' id = json['dataId'] end id end |
#has_content_checker?(api_element) ⇒ Boolean
50 51 52 |
# File 'lib/rest-json-validator.rb', line 50 def has_content_checker?(api_element) api_element.is_a?(Array) and api_element[0].respond_to?(:has_key?) and api_element[0].has_key? :content_checker end |
#is_composite?(api_element) ⇒ Boolean
42 43 44 |
# File 'lib/rest-json-validator.rb', line 42 def is_composite?(api_element) api_element == :composite end |
#is_composite_checker?(api_element) ⇒ Boolean
34 35 36 |
# File 'lib/rest-json-validator.rb', line 34 def is_composite_checker?(api_element) (api_element.respond_to? :has_key? and api_element.has_key?(:composite_checker)) end |
#is_content_checker?(api_element) ⇒ Boolean
46 47 48 |
# File 'lib/rest-json-validator.rb', line 46 def is_content_checker?(api_element) api_element.is_a?(Array) and api_element[0].respond_to?(:has_key?) and api_element[0].has_key? :content_checker end |
#is_field_validator?(api_element, api_key) ⇒ Boolean
21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/rest-json-validator.rb', line 21 def is_field_validator?(api_element, api_key) if [:composite_checker].include?(api_key) retval = false elsif api_element == :composite retval = false elsif api_element.class == Symbol retval = true else retval = false end retval end |
#is_sub_composite_checker?(api_element) ⇒ Boolean
38 39 40 |
# File 'lib/rest-json-validator.rb', line 38 def is_sub_composite_checker?(api_element) (api_element.respond_to? :has_key? and api_element.has_key?(:sub_composite_checker)) end |
#key_diff(actual_json, specification, level) ⇒ Object
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 |
# File 'lib/rest-json-validator.rb', line 117 def key_diff(actual_json, specification, level) optionals = [] if specification.respond_to?(:has_key?) optionals = specification.has_key?(:optionals) ? specification[:optionals].keys : [] end api_keys = specification.keys api_keys.delete(:composite_checker) api_keys.delete(:sub_composite_checker) api_keys.delete(:optionals) json_keys = actual_json.keys diff = [] if api_keys.sort != json_keys.sort extra_api = api_keys - json_keys extra_json = json_keys - api_keys if extra_json.length == 0 and (extra_api - optionals).length == 0 return else diff << 'specification - actual_json: ' diff << api_keys - json_keys diff << 'actual_json - specification: ' diff << json_keys - api_keys = ("Nivå #{level}-feil\n(#{@stack.join('.')})\nfor #{@url}\ndiff: #{diff}\n") notify message: end end end |
#notify(message) ⇒ Object
15 16 17 18 19 |
# File 'lib/rest-json-validator.rb', line 15 def notify() @listeners.each do |l| l.notify end end |
#run_field_validators(actual_json, specification, level) ⇒ Object
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/rest-json-validator.rb', line 68 def run_field_validators(actual_json, specification, level) if is_sub_composite_checker?(specification) @receiver.send(specification[:sub_composite_checker], actual_json, find_id(actual_json), 'vetikke') else specification.keys.each do |api_key| api_element = specification[api_key] id = find_id(actual_json) if is_field_validator?(api_element, api_key) @receiver.send(api_element, actual_json[api_key], id, api_key) elsif is_composite_checker?(api_element) @receiver.send(api_element[:composite_checker], actual_json[api_key], id, api_key) end end end end |
#validate_json(actual_json, specification, level) ⇒ Object
84 85 86 87 88 |
# File 'lib/rest-json-validator.rb', line 84 def validate_json(actual_json, specification, level) # puts "son: #{actual_json} api: #{api} level: #{level}" key_diff(actual_json, specification, level) run_field_validators(actual_json, specification, level) end |
#validate_json_api_compliance(actual_json, specification, level = 0) ⇒ Object
107 108 109 110 111 112 113 114 115 |
# File 'lib/rest-json-validator.rb', line 107 def validate_json_api_compliance(actual_json, specification, level=0) return unless [Hash, Array].include? specification.class level += 1 if actual_json.class == Array depth_validate_array(actual_json, specification[0][:data], level, specification[0][:content_checker]) elsif actual_json.class == Hash depth_validate_hash(actual_json, specification, level) end end |