{"record":{"id":"180ce3d5fcbb6bff","repo":"puppetlabs/puppet","slug":"json-syntax-checker-the-text-to-check-must-be-a-s","errorCode":null,"errorMessage":"Json syntax checker: the text to check must be a String.","messagePattern":"Json syntax checker: the text to check must be a String\\.","errorType":"exception","errorClass":"ArgumentError","httpStatus":null,"severity":"error","filePath":"lib/puppet/syntax_checkers/json.rb","lineNumber":18,"sourceCode":"# frozen_string_literal: true\n\n# A syntax checker for JSON.\n# @api public\nrequire_relative '../../puppet/syntax_checkers'\nclass Puppet::SyntaxCheckers::Json < Puppet::Plugins::SyntaxCheckers::SyntaxChecker\n  # Checks the text for JSON syntax issues and reports them to the given acceptor.\n  #\n  # Error messages from the checker are capped at 100 chars from the source text.\n  #\n  # @param text [String] The text to check\n  # @param syntax [String] The syntax identifier in mime style (e.g. 'json', 'json-patch+json', 'xml', 'myapp+xml'\n  # @param acceptor [#accept] A Diagnostic acceptor\n  # @param source_pos [Puppet::Pops::Adapters::SourcePosAdapter] A source pos adapter with location information\n  # @api public\n  #\n  def check(text, syntax, acceptor, source_pos)\n    raise ArgumentError, _(\"Json syntax checker: the text to check must be a String.\") unless text.is_a?(String)\n    raise ArgumentError, _(\"Json syntax checker: the syntax identifier must be a String, e.g. json, data+json\") unless syntax.is_a?(String)\n    raise ArgumentError, _(\"Json syntax checker: invalid Acceptor, got: '%{klass}'.\") % { klass: acceptor.class.name } unless acceptor.is_a?(Puppet::Pops::Validation::Acceptor)\n\n    begin\n      Puppet::Util::Json.load(text)\n    rescue => e\n      # Cap the message to 100 chars and replace newlines\n      msg = _(\"JSON syntax checker: Cannot parse invalid JSON string. \\\"%{message}\\\"\") % { message: e.message().slice(0, 100).gsub(/\\r?\\n/, \"\\\\n\") }\n\n      # TODO: improve the pops API to allow simpler diagnostic creation while still maintaining capabilities\n      # and the issue code. (In this case especially, where there is only a single error message being issued).\n      #\n      issue = Puppet::Pops::Issues.issue(:ILLEGAL_JSON) { msg }\n      acceptor.accept(Puppet::Pops::Validation::Diagnostic.new(:error, issue, source_pos.file, source_pos, {}))\n    end\n  end\nend\n","sourceCodeStart":1,"sourceCodeEnd":36,"githubUrl":"https://github.com/puppetlabs/puppet/blob/e227c27540975c25aa22d533a52424a9d2fc886a/lib/puppet/syntax_checkers/json.rb#L1-L36","documentation":"The JSON syntax-checker plugin (lib/puppet/syntax_checkers/json.rb) validates raw JSON text and reports issues to an acceptor. Its contract requires the text argument to be a String; anything else (a parsed Hash, Symbol, Pathname, IO) raises ArgumentError immediately. The checker is registered under the 'json' key in Puppet::Plugins and is invoked when Puppet validates heredoc/template content tagged with json syntax.","triggerScenarios":"Calling Puppet::SyntaxCheckers::Json.new.check(hash, 'json', acceptor, source_pos) where hash is an already-parsed JSON object, a Symbol, or nil. Typical in tooling that does JSON.parse(text) and then forwards the result instead of the original string.","commonSituations":"Custom validation pipelines that parse-then-check, passing File objects or Pathname instead of File.read output, or spec helpers that pass fixtures loaded via YAML/JSON helpers rather than raw strings.","solutions":["Pass the raw source string: File.read(path) or the template text exactly as produced","If the value came from JSON.parse, go back to the original string or call .to_json only for output, never for checking","Add a guard: raise ArgumentError, 'expected String' unless text.is_a?(String) before invoking the checker"],"exampleFix":"# before\nparsed = JSON.parse(raw)\nchecker.check(parsed, 'json', acceptor, pos) # => ArgumentError\n\n# after\nchecker.check(raw, 'json', acceptor, pos)","handlingStrategy":"validation","validationCode":"raise ArgumentError, \"text must be a String, got #{text.class}\" unless text.is_a?(String)","typeGuard":"def string?(v)\n  v.is_a?(String)\nend","tryCatchPattern":"begin\n  checker.check(text, 'json', acceptor, pos)\nrescue ArgumentError => e\n  raise ArgumentError, \"JSON checker input invalid: #{e.message}\"\nend","preventionTips":["Check raw source, not parsed structures: forward the original string","Use File.read, never File objects or Pathname","Fail fast on nil/missing content before validation"],"tags":["puppet","json","syntax-checker","argumenterror"],"backgroundTag":"invalid-argument-type","analyzedSha":"e227c27540975c25aa22d533a52424a9d2fc886a","analyzedAt":"2026-08-21T20:49:46.650Z","schemaVersion":2},"datasetVersion":"2026-08-21T23:17:16.201Z"}