Humanizr/Humanizer · error · InvalidOperationException

Missing required property '{sourcePath}'.

Error message

Missing required property '{sourcePath}'.

What it means

EngineContractUtilities.GetRequiredElement (EngineContractUtilities.cs:9-12) walks a dot-separated sourcePath through a JSON document and throws if any segment is missing. It is used by engine-contract members that point at a specific location inside resolved locale data; a mismatch between the contract's declared sourcePath and the locale JSON aborts code generation.

Source

Thrown at src/Humanizer.SourceGenerators/Common/EngineContractUtilities.cs:12

using System.Text.Json;

namespace Humanizer.SourceGenerators;

public sealed partial class HumanizerSourceGenerator
{
    static class EngineContractUtilities
    {
        public static JsonElement GetRequiredElement(JsonElement root, string? sourcePath) =>
            TryGetElement(root, sourcePath, out var element)
                ? element
                : throw new InvalidOperationException($"Missing required property '{sourcePath}'.");

        public static bool TryGetElement(JsonElement root, string? sourcePath, out JsonElement element)
        {
            element = root;
            if (string.IsNullOrWhiteSpace(sourcePath))
            {
                return true;
            }

            foreach (var segment in sourcePath!.Split('.'))
            {
                if (!element.TryGetProperty(segment, out element))
                {
                    return false;
                }
            }

            return true;

View on GitHub (pinned to ffc2b77c0f)

Solutions

  1. Add the missing data at the declared path in the locale file.
  2. Correct the contract's sourcePath to match the actual data layout.
  3. If the path is legitimately optional for some locales, make the contract member optional instead of required.

Example fix

// contract member points at 'numbers.unitsMap'
// before - locale JSON lacks numbers.unitsMap
{ "numbers": { } }
// after
{ "numbers": { "unitsMap": ["zero","one","two"] } }
Defensive patterns

Strategy: validation

Validate before calling

// Verify a contract sourcePath resolves before generation (C#)
using System.Text.Json;
static bool PathExists(JsonElement root, string path) {
    var el = root;
    foreach (var seg in path.Split('.'))
        if (!el.TryGetProperty(seg, out el)) return false;
    return true;
}
if (!PathExists(localeJson, member.SourcePath))
    throw new InvalidOperationException($"Contract sourcePath '{member.SourcePath}' not found in locale JSON");

Prevention

When it happens

Trigger: An engine-contract member declares a sourcePath (e.g. 'numbers.unitsMap') that does not exist in the locale's resolved JSON, because a segment along the path is absent.

Common situations: Locale data was restructured without updating the engine contract; a contract points at a path present only in some locales; a locale is missing a section the contract assumes.

Related errors


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