class JsonChecker::JSONValidator
Public Class Methods
validate_with_config(config)
click to toggle source
# File lib/json_checker/json_validator.rb, line 9 def self.validate_with_config(config) files = config['files'] if files.nil? || files.empty? puts "[ERROR] Invalid json" return end files.each do |file| fileToCheck = JSONToCheck.new(file) fileContent = fileToCheck.get_content() unless fileToCheck.keys.nil? title = "Validating #{fileToCheck.name} values" jsonValidator = JSONValidator.new() jsonValidator.validate_JSON_with_keys(title, fileToCheck.keys, fileContent) end unless fileToCheck.compareTo.nil? JSONComparator.compare(fileToCheck, fileToCheck.compareTo) end end HTMLOutput.generate_output(config['output-path']) end
Public Instance Methods
is_numeric?(obj)
click to toggle source
# File lib/json_checker/json_validator.rb, line 52 def is_numeric?(obj) obj.to_s.match(/\A[+-]?\d+?(\.\d+)?\Z/) == nil ? false : true end
validate_JSON_with_keys(title, jsonKeys, json)
click to toggle source
# File lib/json_checker/json_validator.rb, line 36 def validate_JSON_with_keys(title, jsonKeys, json) items = Array.new() jsonKeys.keys.each do |key| value = value_for_key(key, json) expected = jsonKeys[key] items << verify_value(expected, value, key) end if items.size > 0 HTMLOutput.add_validation_item(title, items) end end
value_for_key(key, json)
click to toggle source
# File lib/json_checker/json_validator.rb, line 48 def value_for_key(key, json) return value_for_key_with_split_character(key, json, '.') end
value_for_key_with_split_character(key, json, splitCharacter)
click to toggle source
# File lib/json_checker/json_validator.rb, line 56 def value_for_key_with_split_character(key, json, splitCharacter) value = json key.split(splitCharacter).each do |item| if !value.nil? && !item.empty? if is_numeric?(item) value = value.kind_of?(Array) ? value[item.to_i] : nil else value = value.kind_of?(Array) ? nil : value[item] end end end return value end
verify_value(expected, value, key)
click to toggle source
# File lib/json_checker/json_validator.rb, line 70 def verify_value(expected, value, key) formatter = "<tr><td>%{first}</td><td>%{second}</td><td>%{third}</td><td>%{fourth}</td></tr>" result = "Error" if value.nil? result = "Not found" elsif value == expected result = "Success" end return formatter % {first: result, second: key, third:expected, fourth:value} end