{"record":{"id":"57155d0085ea140b","repo":"puppetlabs/puppet","slug":"json-syntax-checker-the-syntax-identifier-must-be","errorCode":null,"errorMessage":"Json syntax checker: the syntax identifier must be a String, e.g. json, data+json","messagePattern":"Json syntax checker: the syntax identifier must be a String, e\\.g\\. json, data\\+json","errorType":"exception","errorClass":"ArgumentError","httpStatus":null,"severity":"error","filePath":"lib/puppet/syntax_checkers/json.rb","lineNumber":19,"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 validates that the syntax identifier argument is a String before dispatching. Unlike the pp/epp checkers which compare against a fixed value, the JSON checker only tests syntax.is_a?(String), so any non-String (Symbol like :json, nil, or a mime-type object) raises ArgumentError. The identifier is a mime-style string such as 'json' or 'data+json', and the framework selects the checker from its registry using that key.","triggerScenarios":"Calling Puppet::SyntaxCheckers::Json.new.check(text, :json, acceptor, source_pos) with a Ruby Symbol, or passing nil/nil-able variables as the syntax argument. Also triggered by code that derives the syntax tag dynamically and lets a non-string slip through.","commonSituations":"Metaprogramming that stores syntax identifiers as symbols, DSL-facing APIs that accept keywords and forward them verbatim, or test code using symbol keys by habit.","solutions":["Pass a plain String identifier, e.g. 'json' or 'json-patch+json'","Convert at the boundary: syntax = syntax.to_s if syntax.is_a?(Symbol)","Fail fast with your own ArgumentError including the received class when the tag is not a String"],"exampleFix":"# before\nchecker.check(text, :json, acceptor, pos) # => ArgumentError\n\n# after\nchecker.check(text, 'json', acceptor, pos)","handlingStrategy":"validation","validationCode":"syntax = syntax.to_s if syntax.is_a?(Symbol)\nraise ArgumentError, 'syntax identifier required' unless syntax.is_a?(String) && !syntax.empty?","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Keep syntax identifiers as plain lowercase strings end to end","Normalize at API boundaries: .to_s.downcase.strip","Assert the identifier exists in the checker registry before dispatching"],"tags":["puppet","json","syntax-checker","argumenterror","symbol-vs-string"],"backgroundTag":"invalid-argument-type","analyzedSha":"e227c27540975c25aa22d533a52424a9d2fc886a","analyzedAt":"2026-08-21T20:49:46.650Z","schemaVersion":2},"datasetVersion":"2026-08-21T23:17:16.201Z"}