Sha256: 8d7f01d62cb0ae079ca6d1aad228e6a86fc682518010329cc8083e26358c8c8f

Contents?: true

Size: 1.92 KB

Versions: 76

Compression:

Stored size: 1.92 KB

Contents

# A syntax checker for JSON.
# @api public
require 'puppet/syntax_checkers'
class Puppet::SyntaxCheckers::Json < Puppet::Plugins::SyntaxCheckers::SyntaxChecker

  # Checks the text for JSON syntax issues and reports them to the given acceptor.
  #
  # Error messages from the checker are capped at 100 chars from the source text.
  #
  # @param text [String] The text to check
  # @param syntax [String] The syntax identifier in mime style (e.g. 'json', 'json-patch+json', 'xml', 'myapp+xml'
  # @param acceptor [#accept] A Diagnostic acceptor
  # @param source_pos [Puppet::Pops::Adapters::SourcePosAdapter] A source pos adapter with location information
  # @api public
  #
  def check(text, syntax, acceptor, source_pos)
    raise ArgumentError.new(_("Json syntax checker: the text to check must be a String.")) unless text.is_a?(String)
    raise ArgumentError.new(_("Json syntax checker: the syntax identifier must be a String, e.g. json, data+json")) unless syntax.is_a?(String)
    raise ArgumentError.new(_("Json syntax checker: invalid Acceptor, got: '%{klass}'.") % { klass: acceptor.class.name }) unless acceptor.is_a?(Puppet::Pops::Validation::Acceptor)
    #raise ArgumentError.new("Json syntax checker: location_info must be a Hash") unless location_info.is_a?(Hash)

    begin
      JSON.parse(text)
    rescue => e
      # Cap the message to 100 chars and replace newlines
      msg = _("JSON syntax checker: Cannot parse invalid JSON string. \"%{message}\"") % { message: e.message().slice(0,100).gsub(/\r?\n/, "\\n") }

      # TODO: improve the pops API to allow simpler diagnostic creation while still maintaining capabilities
      # and the issue code. (In this case especially, where there is only a single error message being issued).
      #
      issue = Puppet::Pops::Issues::issue(:ILLEGAL_JSON) { msg }
      acceptor.accept(Puppet::Pops::Validation::Diagnostic.new(:error, issue, source_pos.file, source_pos, {}))
    end
  end
end

Version data entries

76 entries across 76 versions & 2 rubygems

Version Path
bolt-0.16.2 vendored/puppet/lib/puppet/syntax_checkers/json.rb
puppet-5.4.0 lib/puppet/syntax_checkers/json.rb
puppet-5.4.0-x86-mingw32 lib/puppet/syntax_checkers/json.rb
puppet-5.4.0-x64-mingw32 lib/puppet/syntax_checkers/json.rb
puppet-5.4.0-universal-darwin lib/puppet/syntax_checkers/json.rb
puppet-5.3.5 lib/puppet/syntax_checkers/json.rb
puppet-5.3.5-x86-mingw32 lib/puppet/syntax_checkers/json.rb
puppet-5.3.5-x64-mingw32 lib/puppet/syntax_checkers/json.rb
puppet-5.3.5-universal-darwin lib/puppet/syntax_checkers/json.rb
bolt-0.16.1 vendored/puppet/lib/puppet/syntax_checkers/json.rb
puppet-5.3.4 lib/puppet/syntax_checkers/json.rb
puppet-5.3.4-x86-mingw32 lib/puppet/syntax_checkers/json.rb
puppet-5.3.4-x64-mingw32 lib/puppet/syntax_checkers/json.rb
puppet-5.3.4-universal-darwin lib/puppet/syntax_checkers/json.rb
bolt-0.16.0 vendored/puppet/lib/puppet/syntax_checkers/json.rb
bolt-0.15.0 vendored/puppet/lib/puppet/syntax_checkers/json.rb
bolt-0.14.0 vendored/puppet/lib/puppet/syntax_checkers/json.rb
bolt-0.13.0 vendored/puppet/lib/puppet/syntax_checkers/json.rb
bolt-0.12.0 vendored/puppet/lib/puppet/syntax_checkers/json.rb
bolt-0.11.0 vendored/puppet/lib/puppet/syntax_checkers/json.rb