class JsonLint::Linter
Attributes
errors[R]
Public Class Methods
new()
click to toggle source
# File lib/jsonlint/linter.rb, line 10 def initialize @errors = {} end
Public Instance Methods
check(path)
click to toggle source
# File lib/jsonlint/linter.rb, line 18 def check(path) fail FileNotFoundError, "#{path}: no such file" unless File.exist?(path) valid = false File.open(path, 'r') do |f| error_array = [] valid = check_data(f.read, error_array) errors[path] = error_array unless error_array.empty? end valid end
check_all(*files_to_check)
click to toggle source
# File lib/jsonlint/linter.rb, line 14 def check_all(*files_to_check) files_to_check.flatten.each { |f| check(f) } end
check_stream(io_stream)
click to toggle source
# File lib/jsonlint/linter.rb, line 31 def check_stream(io_stream) json_data = io_stream.read error_array = [] valid = check_data(json_data, error_array) errors[''] = error_array unless error_array.empty? valid end
display_errors()
click to toggle source
# File lib/jsonlint/linter.rb, line 50 def display_errors errors.each do |path, errors| puts path errors.each do |err| puts " #{err}" end end end
errors?()
click to toggle source
# File lib/jsonlint/linter.rb, line 41 def errors? !errors.empty? end
errors_count()
click to toggle source
Return the number of lint errors found
# File lib/jsonlint/linter.rb, line 46 def errors_count errors.length end
Private Instance Methods
check_data(json_data, errors_array)
click to toggle source
# File lib/jsonlint/linter.rb, line 61 def check_data(json_data, errors_array) valid = check_not_empty?(json_data, errors_array) valid &&= check_syntax_valid?(json_data, errors_array) valid &&= check_overlapping_keys?(json_data, errors_array) valid end
check_not_empty?(json_data, errors_array)
click to toggle source
# File lib/jsonlint/linter.rb, line 69 def check_not_empty?(json_data, errors_array) if json_data.empty? errors_array << 'The JSON should not be an empty string' false elsif json_data.strip.empty? errors_array << 'The JSON should not just be spaces' false else true end end
check_overlapping_keys?(json_data, errors_array)
click to toggle source
# File lib/jsonlint/linter.rb, line 176 def check_overlapping_keys?(json_data, errors_array) overlap_detector = KeyOverlapDetector.new Oj.saj_parse(overlap_detector, StringIO.new(json_data)) overlap_detector.overlapping_keys.each do |key| errors_array << "The same key is defined more than once: #{key.join('.')}" end !!overlap_detector.overlapping_keys.empty? end
check_syntax_valid?(json_data, errors_array)
click to toggle source
# File lib/jsonlint/linter.rb, line 81 def check_syntax_valid?(json_data, errors_array) Oj.load(json_data, nilnil: false) true rescue Oj::ParseError => e errors_array << e.message false end