Class: RestJsonValidator::JsonValidator

Inherits:
Object
  • Object
show all
Defined in:
lib/rest-json-validator.rb

Instance Method Summary collapse

Constructor Details

#initializeJsonValidator

Returns a new instance of JsonValidator.



4
5
6
7
# File 'lib/rest-json-validator.rb', line 4

def initialize
  @stack        = []
  @listeners = []
end

Instance Method Details

#add_listener(listener) ⇒ Object

a listener must implement a method called notify, input string



10
11
12
# File 'lib/rest-json-validator.rb', line 10

def add_listener(listener)
  @listeners << listener
end

#depth_validate_array(json, api, level, content_checker = nil) ⇒ Object



88
89
90
91
92
93
# File 'lib/rest-json-validator.rb', line 88

def depth_validate_array(json, api, level, content_checker=nil)
  send(content_checker.intern, json, @media_id, level) unless content_checker.nil?
  json.each do |json_element|
    validate_json_api_compliance(json_element, api, level)
  end
end

#depth_validate_hash(json, api, level) ⇒ Object



95
96
97
98
99
100
101
102
103
# File 'lib/rest-json-validator.rb', line 95

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



53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/rest-json-validator.rb', line 53

def find_id(json)
  id = nil
  if json.nil?
    puts "Trying to find id in a nil object (expected a 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

Returns:

  • (Boolean)


49
50
51
# File 'lib/rest-json-validator.rb', line 49

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

Returns:

  • (Boolean)


41
42
43
# File 'lib/rest-json-validator.rb', line 41

def is_composite?(api_element)
  api_element == :composite
end

#is_composite_checker?(api_element) ⇒ Boolean

Returns:

  • (Boolean)


33
34
35
# File 'lib/rest-json-validator.rb', line 33

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

Returns:

  • (Boolean)


45
46
47
# File 'lib/rest-json-validator.rb', line 45

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

Returns:

  • (Boolean)


20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/rest-json-validator.rb', line 20

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

Returns:

  • (Boolean)


37
38
39
# File 'lib/rest-json-validator.rb', line 37

def is_sub_composite_checker?(api_element)
  (api_element.respond_to? :has_key? and api_element.has_key?(:sub_composite_checker))
end

#key_diff(json, api, level) ⇒ Object



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/rest-json-validator.rb', line 115

def key_diff(json, api, level)
  optionals = []
  if api.respond_to?(:has_key?)
    optionals = api.has_key?(:optionals) ? api[:optionals].keys : []
  end
  api_keys = api.keys
  api_keys.delete(:composite_checker)
  api_keys.delete(:sub_composite_checker)
  api_keys.delete(:optionals)
  json_keys = 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 << 'api - json: '
      diff << api_keys - json_keys
      diff << 'json - api: '
      diff << json_keys - api_keys
      message = ("Nivå #{level}-feil\n(#{@stack.join('.')})\nfor #{@url}\ndiff: #{diff}\n")
      notify message: message
    end
  end
end

#notify(message) ⇒ Object



14
15
16
17
18
# File 'lib/rest-json-validator.rb', line 14

def notify(message)
  @listeners.each do |l|
    l.notify message
  end
end

#run_field_validators(json, api, level) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/rest-json-validator.rb', line 67

def run_field_validators(json, api, level)
   if is_sub_composite_checker?(api)
     send(api[:sub_composite_checker], json, find_id(json), 'vetikke')
   else
  api.keys.each do |api_key|
    api_element = api[api_key]
    id          = find_id(json)
    if is_field_validator?(api_element, api_key)
      send(api_element, json[api_key], id, api_key)
    elsif is_composite_checker?(api_element)
      send(api_element[:composite_checker], json[api_key], id, api_key)
    end
  end
   end
end

#validate_json(json, api, level) ⇒ Object



83
84
85
86
# File 'lib/rest-json-validator.rb', line 83

def validate_json(json, api, level)
  key_diff(json, api, level)
  run_field_validators(json, api, level)
end

#validate_json_api_compliance(json, api, level = 0) ⇒ Object



105
106
107
108
109
110
111
112
113
# File 'lib/rest-json-validator.rb', line 105

def validate_json_api_compliance(json, api, level=0)
  return unless [Hash, Array].include? api.class
  level += 1
  if json.class == Array
    depth_validate_array(json, api[0][:data], level, api[0][:content_checker])
  elsif json.class == Hash
    depth_validate_hash(json, api, level)
  end
end