Humanizr/Humanizer · error · ArgumentOutOfRangeException

Specified argument was out of the range of valid values. (Pa

Error message

Specified argument was out of the range of valid values. (Parameter 'source')

What it means

Thrown inside the private Humanize<T> worker when the EnumHumanizeSource argument is not a defined member of the enum (Default, EnumName, DisplayName, DisplayDescription, DisplayShortName). The guard runs Enum.IsDefined(source) at the top of the method, before any caching/formatting, so an undefined source fails fast. The parameter is named 'source' in the exception.

Source

Thrown at src/Humanizer/EnumHumanizeExtensions.cs:138

    ///     [Description("Currently active")]
    ///     Active 
    /// }
    /// Status.Active.Humanize() => "Currently active"
    /// </code>
    /// </example>
    public static string Humanize<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicFields)] T>(this T input)
        where T : struct, Enum =>
        Humanize(input, null, EnumHumanizeSource.Default);

    static string Humanize<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicFields)] T>(
        T input,
        LetterCasing? casing,
        EnumHumanizeSource source)
        where T : struct, Enum
    {
        if (!Enum.IsDefined(source))
        {
            throw new ArgumentOutOfRangeException(nameof(source));
        }

        var (zero, values) = EnumCache<T>.GetInfo(source);
        if (EnumCache<T>.TreatAsFlags(input, source))
        {
            if (casing is { } flagsCasing && !Enum.IsDefined(flagsCasing))
            {
                throw new ArgumentOutOfRangeException(nameof(casing));
            }

            // Avoid LINQ allocations by manually iterating and building the list
            List<string>? flagValues = null;
            foreach (var value in values)
            {
                if (value.CompareTo(zero) != 0 && input.HasFlag(value))
                {
                    flagValues ??= new List<string>();
                    var flag = EnumCache<T>.GetHumanized(value, source);

View on GitHub (pinned to ffc2b77c0f)

Solutions

  1. Validate with Enum.IsDefined(typeof(EnumHumanizeSource), source) before calling and coerce/reject invalid values.
  2. Bind configuration via strongly-typed enum parsing (e.g. Enum.TryParse) rather than raw int casts.
  3. If you only need default metadata precedence, omit the source argument entirely and call the simpler Humanize overloads.

Example fix

// before
var text = status.Humanize(LetterCasing.Title, (EnumHumanizeSource)rawConfig);

// after
if (!Enum.IsDefined(typeof(EnumHumanizeSource), rawConfig))
    rawConfig = (int)EnumHumanizeSource.Default;
var text = status.Humanize(LetterCasing.Title, (EnumHumanizeSource)rawConfig);
Defensive patterns

Strategy: validation

Validate before calling

if (!Enum.IsDefined(typeof(EnumHumanizeSource), source))
    source = EnumHumanizeSource.Default;
var text = input.Humanize(LetterCasing.Title, source);

Type guard

static bool IsValidSource(EnumHumanizeSource s) => Enum.IsDefined(typeof(EnumHumanizeSource), s);

Try / catch

try { return input.Humanize(casing, source); }
catch (ArgumentOutOfRangeException ex) when (ex.ParamName == "source")
{ return input.Humanize(casing, EnumHumanizeSource.Default); }

Prevention

When it happens

Trigger: Calling enumValue.Humanize(LetterCasing.Title, (EnumHumanizeSource)99); passing an EnumHumanizeSource deserialized from an out-of-range integer; a default(EnumHumanizeSource) value is valid (=Default=0) and will NOT throw.

Common situations: Configuration files or databases storing the source as a raw int with an out-of-range value after an enum was reshaped; reflection-based callers that build the argument dynamically; interop with another enum that happens to be cast to EnumHumanizeSource.

Related errors


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