Humanizr/Humanizer · error · InvalidOperationException

'{path}.cases' is only valid when classification is 'distinc

Error message

'{path}.cases' is only valid when classification is 'distinct'.

What it means

Thrown by ParseLegacyForTests when the durationCases block includes a 'cases' key but the classification is not 'distinct'. Cases (genitive, dative, etc.) only make sense when the locale actually has distinct case forms. For 'same-as-nominative', 'not-applicable', and 'unsupported', no cases block should be present.

Source

Thrown at src/Humanizer.SourceGenerators/Common/DurationCaseModels.cs:339

            RejectUnknownKeys(mapping, path, ["classification", "cases"]);

            var classification = mapping.GetScalar("classification") switch
            {
                "distinct" => DurationCaseClassification.Distinct,
                "same-as-nominative" => DurationCaseClassification.Invariant,
                "not-applicable" => DurationCaseClassification.NotApplicable,
                "unsupported" => DurationCaseClassification.Unsupported,
                { } unsupported => throw new InvalidOperationException(
                    $"'{path}.classification' has unsupported value '{unsupported}'. " +
                    "Supported values: distinct, same-as-nominative, not-applicable, unsupported."),
                null => throw new InvalidOperationException($"'{path}' must define 'classification'.")
            };

            if (classification != DurationCaseClassification.Distinct)
            {
                if (mapping.TryGetValue("cases", out _))
                {
                    throw new InvalidOperationException(
                        $"'{path}.cases' is only valid when classification is 'distinct'.");
                }

                return new DurationCaseCatalog(
                    localeCode,
                    classification,
                    "nominative",
                    classification == DurationCaseClassification.Invariant ? ["nominative"] : [],
                    EmptyRealizations(),
                    EmptySources(),
                    classification == DurationCaseClassification.Invariant
                        ? ImmutableDictionary<string, DurationCaseOverlay>.Empty
                            .WithComparers(StringComparer.Ordinal)
                            .Add("nominative", CreateBaseDurationOverlay())
                        : ImmutableDictionary<string, DurationCaseOverlay>.Empty.WithComparers(StringComparer.Ordinal));
            }

            if (!mapping.TryGetValue("cases", out var casesValue))

View on GitHub (pinned to ffc2b77c0f)

Solutions

  1. Remove the 'cases' key and its children if the classification is not 'distinct'.
  2. If cases are genuinely needed, change classification back to 'distinct'.
  3. Review the locale's grammar to confirm whether case distinctions actually apply before choosing classification.

Example fix

# before (cases without distinct classification)
durationCases:
  classification: same-as-nominative
  cases:
    genitive: { ... }
# after (remove cases for non-distinct)
durationCases:
  classification: same-as-nominative
Defensive patterns

Strategy: validation

Validate before calling

// Cases only valid when classification is distinct
static bool CasesConsistentWithClassification(SimpleYamlMapping durationCases)
{
    var classification = durationCases.GetScalar("classification");
    var hasCases = durationCases.TryGetValue("cases", out _);
    return classification != "distinct" ? !hasCases : true;
}

Prevention

When it happens

Trigger: After resolving classification, ParseLegacyForTests checks if classification != Distinct and mapping has 'cases'. If so, it throws. For example 'classification: same-as-nominative\ncases:\n genitive: ...' triggers it.

Common situations: A contributor changes classification from 'distinct' to 'same-as-nominative' but leaves the cases block in place. Or a copy-paste from a distinct locale includes cases that are no longer relevant after reclassification.

Related errors


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