{"record":{"id":"53f5fa69fa77a2dc","repo":"Humanizr/Humanizer","slug":"indexed-string-arrays-require-non-negative-integer","errorCode":null,"errorMessage":"Indexed string arrays require non-negative integer keys. Invalid key '{property.Name}'.","messagePattern":"Indexed string arrays require non-negative integer keys\\. Invalid key '(.+?)'\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Humanizer.SourceGenerators/Common/GenerationHelpers.cs","lineNumber":312,"sourceCode":"        }\n\n        builder.Append(\" }\");\n        return builder.ToString();\n    }\n\n    static string CreateNumericSlotStringArrayExpression(JsonElement objectElement)\n    {\n        if (!objectElement.EnumerateObject().Any())\n        {\n            return \"Array.Empty<string>()\";\n        }\n\n        var indexedValues = new SortedDictionary<int, string>();\n        foreach (var property in objectElement.EnumerateObject())\n        {\n            if (!int.TryParse(property.Name, NumberStyles.Integer, CultureInfo.InvariantCulture, out var index) || index < 0)\n            {\n                throw new InvalidOperationException($\"Indexed string arrays require non-negative integer keys. Invalid key '{property.Name}'.\");\n            }\n\n            if (property.Value.ValueKind != JsonValueKind.String)\n            {\n                throw new InvalidOperationException($\"Indexed string arrays require string values. Property '{property.Name}' was {property.Value.ValueKind}.\");\n            }\n\n            indexedValues[index] = property.Value.GetString()!;\n        }\n\n        var builder = new StringBuilder(\"new string[] { \");\n        var first = true;\n        var lastIndex = indexedValues.Keys.Max();\n\n        for (var index = 0; index <= lastIndex; index++)\n        {\n            if (!first)\n            {","sourceCodeStart":294,"sourceCodeEnd":330,"githubUrl":"https://github.com/Humanizr/Humanizer/blob/ffc2b77c0f30d2fb176875841424379319d0ae9b/src/Humanizer.SourceGenerators/Common/GenerationHelpers.cs#L294-L330","documentation":"CreateNumericSlotStringArrayExpression (GenerationHelpers.cs:310-313) handles the indexed-mapping form of a string array and requires every key to be a non-negative integer. A word key, a negative index, or a non-numeric key fails int.TryParse or the index<0 check.","triggerScenarios":"An indexed mapping uses word keys ({two: 'twenty'}) or negative keys ({-1: 'x'}) instead of non-negative integer keys.","commonSituations":"Authoring tensMap/unitsMap with readable word keys instead of numeric indices; copy-paste from a different mapping style.","solutions":["Use integer keys only: {2: 'twenty', 3: 'thirty'}.","Ensure all keys are >= 0 (leading empty slots are represented by index gaps, not negatives).","If a word-keyed mapping is intended, switch the field to the plain-array form instead."],"exampleFix":"# before\ntensMap:\n  two: 'twenty'\n  three: 'thirty'\n# after\ntensMap:\n  2: 'twenty'\n  3: 'thirty'","handlingStrategy":"validation","validationCode":"// Indexed string-array keys must be non-negative integers (C#)\nforeach (var prop in obj.EnumerateObject())\n    if (!int.TryParse(prop.Name, NumberStyles.Integer, CultureInfo.InvariantCulture, out var i) || i < 0)\n        throw new InvalidOperationException($\"Invalid index key '{prop.Name}'\");","typeGuard":"static bool IsValidIndexKey(string name) =>\n    int.TryParse(name, NumberStyles.Integer, CultureInfo.InvariantCulture, out var i) && i >= 0;","tryCatchPattern":null,"preventionTips":["Use numeric keys (0,1,2,...) for indexed mappings; never words or negatives.","If word keys are wanted, switch the field to the plain-array form.","Lint indexed mappings for non-numeric keys pre-build."],"tags":["source-generator","build-time","engine-contract","yaml","json","locale-data"],"backgroundTag":null,"analyzedSha":"ffc2b77c0f30d2fb176875841424379319d0ae9b","analyzedAt":"2026-08-13T21:42:34.584Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}