{"record":{"id":"1fd338dc4548f90e","repo":"Humanizr/Humanizer","slug":"specified-argument-was-out-of-the-range-of-valid-v-1fd338","errorCode":null,"errorMessage":"Specified argument was out of the range of valid values. (Parameter 'casing')","messagePattern":"Specified argument was out of the range of valid values\\. \\(Parameter 'casing'\\)","errorType":"exception","errorClass":"ArgumentOutOfRangeException","httpStatus":null,"severity":"error","filePath":"src/Humanizer/EnumHumanizeExtensions.cs","lineNumber":146,"sourceCode":"        Humanize(input, null, EnumHumanizeSource.Default);\n\n    static string Humanize<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicFields)] T>(\n        T input,\n        LetterCasing? casing,\n        EnumHumanizeSource source)\n        where T : struct, Enum\n    {\n        if (!Enum.IsDefined(source))\n        {\n            throw new ArgumentOutOfRangeException(nameof(source));\n        }\n\n        var (zero, values) = EnumCache<T>.GetInfo(source);\n        if (EnumCache<T>.TreatAsFlags(input, source))\n        {\n            if (casing is { } flagsCasing && !Enum.IsDefined(flagsCasing))\n            {\n                throw new ArgumentOutOfRangeException(nameof(casing));\n            }\n\n            // Avoid LINQ allocations by manually iterating and building the list\n            List<string>? flagValues = null;\n            foreach (var value in values)\n            {\n                if (value.CompareTo(zero) != 0 && input.HasFlag(value))\n                {\n                    flagValues ??= new List<string>();\n                    var flag = EnumCache<T>.GetHumanized(value, source);\n                    flagValues.Add(casing is { } flagCasing && !flag.IsMetadata ? flag.Text.ApplyCase(flagCasing) : flag.Text);\n                }\n            }\n\n            return flagValues?.Humanize() ?? string.Empty;\n        }\n\n        var humanizedEnum = EnumCache<T>.GetHumanized(input, source);","sourceCodeStart":128,"sourceCodeEnd":164,"githubUrl":"https://github.com/Humanizr/Humanizer/blob/ffc2b77c0f30d2fb176875841424379319d0ae9b/src/Humanizer/EnumHumanizeExtensions.cs#L128-L164","documentation":"Thrown in the flags-enum branch of Humanize<T> when a LetterCasing value is supplied that is not defined. Because flags enums humanize each flag separately and apply casing per flag, the method checks Enum.IsDefined(flagsCasing) inside the TreatAsFlags path and throws ArgumentOutOfRangeException named 'casing'. This fires only for [Flags] enums and only when a casing argument was provided.","triggerScenarios":"Calling (Permission.Read | Permission.Write).Humanize((LetterCasing)7) where Permission is a [Flags] enum; passing an invalid LetterCasing to a flags humanization that also set a non-null casing. Non-flags enums do not reach this specific throw.","commonSituations":"Sharing a single 'style' setting across both flags and non-flags humanization where the style int can drift out of the [0..3] range; deserializing the casing from config as an int; default(LetterCasing) is Title (0) and is valid, so an unset field will not trigger this.","solutions":["Guard the casing with Enum.IsDefined(casing) before calling Humanize on a flags enum.","Centralize casing selection into a validated option so only defined LetterCasing values propagate.","If you do not need a specific casing for flags, call the parameterless Humanize() to bypass the check entirely."],"exampleFix":"// before\nvar text = perms.Humanize((LetterCasing)styleCode);\n\n// after\nvar casing = Enum.IsDefined(typeof(LetterCasing), styleCode)\n    ? (LetterCasing)styleCode\n    : (LetterCasing?)null;\nvar text = casing is { } c ? perms.Humanize(c) : perms.Humanize();","handlingStrategy":"validation","validationCode":"if (casing.HasValue && !Enum.IsDefined(typeof(LetterCasing), casing.Value))\n    throw new ArgumentOutOfRangeException(nameof(casing));\nvar text = perms.Humanize(casing!.Value);","typeGuard":"static bool IsValidCasing(LetterCasing c) => Enum.IsDefined(typeof(LetterCasing), c);","tryCatchPattern":"try { return perms.Humanize(casing); }\ncatch (ArgumentOutOfRangeException ex) when (ex.ParamName == \"casing\")\n{ return perms.Humanize(); }","preventionTips":["Validate LetterCasing once at the boundary and reuse the checked value.","For flags enums where casing is optional, pass the parameterless overload when unsure.","Keep casing selection in a single validated option object."],"tags":["enum","flags","casing","argument-validation","argument-out-of-range"],"backgroundTag":null,"analyzedSha":"ffc2b77c0f30d2fb176875841424379319d0ae9b","analyzedAt":"2026-08-13T21:42:34.584Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}