Humanizr/Humanizer · error · InvalidOperationException
Template '{path}' must define a scalar 'value'.
Error message
Template '{path}' must define a scalar 'value'. What it means
Thrown by ParseNamedTemplate when a 'template' entry is a mapping but either lacks a 'value' key or its value is not a scalar. Templates require a scalar 'value' string (optionally with a 'name'). Surfaced as compiler diagnostic HSG003 (severity Error).
Source
Thrown at src/Humanizer.SourceGenerators/Common/LocalePhraseNormalization.cs:432
null or "" or "before-form" or "before" => CountPlacement.BeforeForm,
"after-form" or "after" => CountPlacement.AfterForm,
"none" => CountPlacement.None,
var value => throw new InvalidOperationException($"Phrase '{path}.countPlacement' uses unsupported count placement '{value}'.")
};
static NamedTemplatePhrase ParseNamedTemplate(SimpleYamlValue value, string path, params string[] allowedPlaceholders)
{
if (value is SimpleYamlScalar scalar)
{
return CreateNamedTemplatePhrase(null, scalar.Value, path, allowedPlaceholders);
}
var mapping = ExpectMapping(value, path);
RejectUnknownKeys(mapping, path, ["name", "value"]);
var name = GetOptionalLiteral(mapping, "name", $"{path}.name");
if (!mapping.TryGetValue("value", out var templateValue) || templateValue is not SimpleYamlScalar templateScalar)
{
throw new InvalidOperationException($"Template '{path}' must define a scalar 'value'.");
}
return CreateNamedTemplatePhrase(name, templateScalar.Value, path, allowedPlaceholders);
}
static NamedTemplatePhrase CreateNamedTemplatePhrase(string? templateName, string template, string path, params string[] allowedPlaceholders)
{
var placeholders = ImmutableArray.CreateBuilder<string>();
foreach (var placeholderName in GetPlaceholderNames(template))
{
if (int.TryParse(placeholderName, out _))
{
throw new InvalidOperationException($"Template '{path}' cannot use numeric placeholder '{{{placeholderName}}}'.");
}
if (placeholderName.Length == 0 || !char.IsLetter(placeholderName[0]) || placeholderName.Any(static c => !char.IsLetterOrDigit(c) && c != '_'))
{View on GitHub (pinned to ffc2b77c0f)
Solutions
- Add a scalar 'value' string to the template mapping (e.g. value: '{count} {unit}').
- If no name is needed, write the template as a plain scalar (template: '{count} {unit}').
- Rebuild to confirm the diagnostic clears.
Example fix
# before
template:
name: age
# after
template:
name: age
value: '{count} {unit} ago' Defensive patterns
Strategy: validation
Validate before calling
# Any template that is a mapping must contain a scalar 'value'.
yq -o=json '.. | select(has("template")) | .template | select(tag == "!!map" and (has("value") | not))' src/Humanizer/Locales/<code>.yml Prevention
- Default to scalar templates (template: '{count} {unit}') unless you need a name.
- When using a mapping, add 'value' first.
- Build after authoring templates.
When it happens
Trigger: Authoring template: { name: ... } with no value; making the value a mapping/sequence; misspelling 'value' as 'Value' or 'text'.
Common situations: Authoring a named template and forgetting the value field; YAML indentation making value a child of the wrong key; copy-paste from a doc snippet that omitted value.
Related errors
- Template '{path}' cannot use numeric placeholder '{{placehol
- Template '{path}' uses unsupported placeholder '{{placeholde
- Template '{path}' can only use placeholders: {allowedPlaceho
- Phrase '{path}' must define a scalar string or an explicit '
- Phrase '{path}' can only use placeholders in explicit 'templ
AI-assisted analysis of Humanizr/Humanizer@ffc2b77c0f (2026-08-13).
Data as JSON: /api/errors/f2b9b32afd3f4b99.
Report an issue: GitHub.