Humanizr/Humanizer · error · InvalidOperationException

Inflection rules must be mappings.

Error message

Inflection rules must be mappings.

What it means

Each item in the 'rules:' sequence must be a YAML mapping describing one rule (id, direction, priority, scope, match, output, ...). ParseRules throws when a list item is a scalar, null, or nested sequence.

Source

Thrown at src/Humanizer.SourceGenerators/Generators/ProfileCatalogs/InflectionCatalogParsing.cs:423

            string casing)
        {
            if (!mapping.TryGetValue("rules", out var value))
            {
                return [];
            }

            if (value is not SimpleYamlSequence sequence)
            {
                throw new InvalidOperationException("Inflection rules must be a block sequence.");
            }

            var rules = ImmutableArray.CreateBuilder<InflectionRuleInput>();
            var ids = new HashSet<string>(StringComparer.Ordinal);
            foreach (var item in sequence.Items)
            {
                if (item is not SimpleYamlMapping rule)
                {
                    throw new InvalidOperationException("Inflection rules must be mappings.");
                }

                ValidateProperties(
                    rule,
                    "Inflection rule",
                    "id", "direction", "priority", "scope", "match", "output",
                    "hostileExclusions", "hostile-exclusions", "reverse", "sources");
                var id = GetRequiredScalar(rule, "id", "Inflection rule must define id");
                if (!ids.Add(id))
                {
                    throw new InvalidOperationException($"Duplicate inflection rule id '{id}'.");
                }

                ValidateSources(rule, sourceIds, $"Inflection rule '{id}'");
                var direction = GetRequiredScalar(rule, "direction", $"Inflection rule '{id}' must define direction");
                if (direction is not ("forward" or "reverse"))
                {
                    throw new InvalidOperationException($"Inflection rule '{id}' has unsupported direction '{direction}'.");

View on GitHub (pinned to ffc2b77c0f)

Solutions

  1. Ensure each item under 'rules:' is a mapping with the required rule keys

Example fix

# before
    rules:
      - 'zz.forward.consonant-y'
# after
    rules:
      - id: 'zz.forward.consonant-y'
        direction: 'forward'
        priority: 100
        scope: { ... }
        match: { ... }
        output: { ... }
        sources: [ 'fixture' ]
Defensive patterns

Strategy: validation

Validate before calling

foreach (var item in ruleSequence.Items)
    if (item is not SimpleYamlMapping)
        Report($"{locale}: each rules entry must be a mapping.");

Type guard

static bool AllRulesAreMappings(SimpleYamlSequence s) =>
    s.Items.All(i => i is SimpleYamlMapping);

Prevention

When it happens

Trigger: A 'rules:' list item is a scalar or sequence instead of a mapping.

Common situations: A YAML indentation slip flattening rule keys into the list; pasting a bare id string as a list item.

Related errors


AI-assisted analysis of Humanizr/Humanizer@ffc2b77c0f (2026-08-13). Data as JSON: /api/errors/2ce9196ce4ef489c. Report an issue: GitHub.