Class: Puppet::SyntaxCheckers::Json

Inherits:
Plugins::SyntaxCheckers::SyntaxChecker show all
Defined in:
lib/puppet/syntax_checkers/json.rb

Instance Method Summary collapse

Instance Method Details

#check(text, syntax, acceptor, source_pos) ⇒ Object

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.

Parameters:

  • text (String)

    The text to check

  • syntax (String)

    The syntax identifier in mime style (e.g. ‘json’, ‘json-patch+json’, ‘xml’, ‘myapp+xml’

  • acceptor (#accept)

    A Diagnostic acceptor

  • source_pos (Puppet::Pops::Adapters::SourcePosAdapter)

    A source pos adapter with location information

Raises:

  • (ArgumentError)


17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/puppet/syntax_checkers/json.rb', line 17

def check(text, syntax, acceptor, source_pos)
  raise ArgumentError, _("Json syntax checker: the text to check must be a String.") unless text.is_a?(String)
  raise ArgumentError, _("Json syntax checker: the syntax identifier must be a String, e.g. json, data+json") unless syntax.is_a?(String)
  raise ArgumentError, _("Json syntax checker: invalid Acceptor, got: '%{klass}'.") % { klass: acceptor.class.name } unless acceptor.is_a?(Puppet::Pops::Validation::Acceptor)

  begin
    Puppet::Util::Json.load(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