class Puppet::SyntaxCheckers::Json

Public Instance Methods

check(text, syntax, acceptor, source_pos) click to toggle source

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

   # File lib/puppet/syntax_checkers/json.rb
16 def check(text, syntax, acceptor, source_pos)
17   raise ArgumentError.new(_("Json syntax checker: the text to check must be a String.")) unless text.is_a?(String)
18   raise ArgumentError.new(_("Json syntax checker: the syntax identifier must be a String, e.g. json, data+json")) unless syntax.is_a?(String)
19   raise ArgumentError.new(_("Json syntax checker: invalid Acceptor, got: '%{klass}'.") % { klass: acceptor.class.name }) unless acceptor.is_a?(Puppet::Pops::Validation::Acceptor)
20   #raise ArgumentError.new("Json syntax checker: location_info must be a Hash") unless location_info.is_a?(Hash)
21 
22   begin
23     Puppet::Util::Json.load(text)
24   rescue => e
25     # Cap the message to 100 chars and replace newlines
26     msg = _("JSON syntax checker: Cannot parse invalid JSON string. \"%{message}\"") % { message: e.message().slice(0,100).gsub(/\r?\n/, "\\n") }
27 
28     # TODO: improve the pops API to allow simpler diagnostic creation while still maintaining capabilities
29     # and the issue code. (In this case especially, where there is only a single error message being issued).
30     #
31     issue = Puppet::Pops::Issues::issue(:ILLEGAL_JSON) { msg }
32     acceptor.accept(Puppet::Pops::Validation::Diagnostic.new(:error, issue, source_pos.file, source_pos, {}))
33   end
34 end