Sha256: f65af8af160e3c2248bcb2e201c1d985d3968254a440bb56a36dd6375ad8e2c5

Contents?: true

Size: 1.33 KB

Versions: 31

Compression:

Stored size: 1.33 KB

Contents

module Lono::Yamler
  class Validator
    def initialize(path)
      @path = path
    end

    def validate!
      validate_yaml(@path)
    end

    def validate_yaml(path)
      text = IO.read(path)
      begin
        YAML.load(text)
      rescue Psych::SyntaxError => e
        handle_yaml_syntax_error(e, path)
      end
    end

    def handle_yaml_syntax_error(e, path)
      io = StringIO.new
      io.puts "Invalid yaml. Output written for debugging: #{path}".color(:red)
      io.puts "ERROR: #{e.message}".color(:red)

      # Grab line info.  Example error:
      #   ERROR: (<unknown>): could not find expected ':' while scanning a simple key at line 2 column 1
      md = e.message.match(/at line (\d+) column (\d+)/)
      line = md[1].to_i

      lines = IO.read(path).split("\n")
      context = 5 # lines of context
      top, bottom = [line-context-1, 0].max, line+context-1
      spacing = lines.size.to_s.size
      lines[top..bottom].each_with_index do |line_content, index|
        line_number = top+index+1
        if line_number == line
          io.printf("%#{spacing}d %s\n".color(:red), line_number, line_content)
        else
          io.printf("%#{spacing}d %s\n", line_number, line_content)
        end
      end

      if ENV['LONO_TEST']
        io.string
      else
        puts io.string
        exit 1
      end
    end
  end
end

Version data entries

31 entries across 31 versions & 1 rubygems

Version Path
lono-8.0.0.pre.rc2 lib/lono/yamler/validator.rb
lono-8.0.0.pre.rc1 lib/lono/yamler/validator.rb
lono-7.5.2 lib/lono/yamler/validator.rb
lono-7.5.1 lib/lono/yamler/validator.rb
lono-7.5.0 lib/lono/yamler/validator.rb
lono-7.4.11 lib/lono/yamler/validator.rb
lono-7.4.10 lib/lono/yamler/validator.rb
lono-7.4.9 lib/lono/yamler/validator.rb
lono-7.4.8 lib/lono/yamler/validator.rb
lono-7.4.7 lib/lono/yamler/validator.rb
lono-7.4.6 lib/lono/yamler/validator.rb
lono-7.4.5 lib/lono/yamler/validator.rb
lono-7.4.4 lib/lono/yamler/validator.rb
lono-7.4.3 lib/lono/yamler/validator.rb
lono-7.4.2 lib/lono/yamler/validator.rb
lono-7.4.1 lib/lono/yamler/validator.rb
lono-7.4.0 lib/lono/yamler/validator.rb
lono-7.3.2 lib/lono/yamler/validator.rb
lono-7.3.1 lib/lono/yamler/validator.rb
lono-7.3.0 lib/lono/yamler/validator.rb