puppetlabs/puppet · error · ArgumentError
Json syntax checker: the syntax identifier must be a String,
Error message
Json syntax checker: the syntax identifier must be a String, e.g. json, data+json
What it means
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.
Source
Thrown at lib/puppet/syntax_checkers/json.rb:19
# frozen_string_literal: true
# A syntax checker for JSON.
# @api public
require_relative '../../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, _("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
end
View on GitHub (pinned to e227c27540)
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
Example fix
# before checker.check(text, :json, acceptor, pos) # => ArgumentError # after checker.check(text, 'json', acceptor, pos)
Defensive patterns
Strategy: validation
Validate before calling
syntax = syntax.to_s if syntax.is_a?(Symbol) raise ArgumentError, 'syntax identifier required' unless syntax.is_a?(String) && !syntax.empty?
Prevention
- 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
When it happens
Trigger: 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.
Common situations: 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.
Understand the failure class
Background: Invalid argument type errors: "must be of type string", "expected X, got Y", and ERR_INVALID_ARG_TYPE explained — this error's family across 15 libraries.
Related errors
- Json syntax checker: the text to check must be a String.
- Json syntax checker: invalid Acceptor, got: '%{klass}'.
- EPP syntax checker: invalid Acceptor, got: '%{klass}'.
- PP syntax checker: the text to check must be a String.
- PP syntax checker: the syntax identifier must be a String, e
AI-assisted analysis of puppetlabs/puppet@e227c27540 (2026-08-21).
Data as JSON: /api/errors/57155d0085ea140b.
Report an issue: GitHub.