Humanizr/Humanizer · error · NoMatchFoundException

Couldn't find any enum member that matches the string '{inpu

Error message

Couldn't find any enum member that matches the string '{input}'

What it means

Thrown by DehumanizeTo<T> (the throwing overload) when no enum member matches the input string. Matching is case-insensitive against the enum member name, its humanized form, and configured metadata aliases (DisplayAttribute Name/Description/ShortName, DescriptionAttribute). The custom NoMatchFoundException message echoes the offending input. The nullable overload with OnNoMatch.ReturnsNull returns null instead.

Source

Thrown at src/Humanizer/EnumDehumanizeExtensions.cs:152

            throw exception.InnerException!;
        }
    }

    static T? DehumanizeToPrivate<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicFields)] T>(string input, OnNoMatch onNoMatch)
        where T : struct, Enum
    {
        var dehumanized = EnumCache<T>.GetDehumanized();
        if (dehumanized.TryGetValue(input, out var value))
        {
            return value;
        }

        if (onNoMatch != OnNoMatch.ThrowsException)
        {
            return null;
        }

        throw new NoMatchFoundException($"Couldn't find any enum member that matches the string '{input}'");
    }
}

View on GitHub (pinned to ffc2b77c0f)

Solutions

  1. Use the overload "input".DehumanizeTo<MyEnum>(OnNoMatch.ReturnsNull) and handle the null result explicitly.
  2. Pre-validate input against the set of humanized enum values (typeof(MyEnum).GetValues()...Humanize()) before calling.
  3. Catch NoMatchFoundException specifically (not the general Exception) and present a 'no matching value' error to the user.

Example fix

// before
var value = userInput.DehumanizeTo<MyEnum>(); // throws NoMatchFoundException

// after
var value = userInput.DehumanizeTo<MyEnum>(OnNoMatch.ReturnsNull);
if (value is null)
    return BadRequest($"Unknown value '{userInput}'.");
Defensive patterns

Strategy: fallback

Validate before calling

// Prefer the non-throwing overload:
var value = input.DehumanizeTo<MyEnum>(OnNoMatch.ReturnsNull);
if (value is null) return NotFound($"No enum member matches '{input}'.");

Type guard

static bool CanDehumanizeTo<TEnum>(string input) where TEnum : struct, Enum =>
    input.DehumanizeTo<TEnum>(OnNoMatch.ReturnsNull) is not null;

Try / catch

try { return input.DehumanizeTo<MyEnum>(); }
catch (NoMatchFoundException) { return NotFound(); }

Prevention

When it happens

Trigger: Calling "Bad Value".DehumanizeTo<MyEnum>() where no member humanizes to 'Bad Value'; dehumanizing a localized display string whose culture differs from what was used to humanize; whitespace or case variants that the matcher does not normalize (matching does NOT trim whitespace); passing user input directly without validation.

Common situations: Round-tripping enums through an external system that altered the string (extra spaces, different locale, a different metadata attribute used at write time); accepting free-text enum selection from a user without validating against known values; version skew where an enum member was renamed/removed but stored strings persist.

Related errors


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