Skip to content

Commit bd23853

Browse files
authored
fix(validation): validate lang parameter against supported languages keys (JhaSourav07#1889)
## Description Fixes JhaSourav07#1796 ### Changes Made - Added `supportedLanguages` derived from translation keys in `badgeLabels.ts` - Updated `streakParamsSchema` to validate `lang` using `z.enum()` - Added `.catch('en')` fallback behavior for unsupported language inputs - Preserved default English fallback behavior ### Verification - [x] npm run format - [x] npm run lint - [x] npm run test - [x] npm run typecheck - [x] Verified invalid lang values fall back to `en` ### Result - Valid language inputs continue to work correctly - Unsupported language values now safely fall back to English (`en`) - Prevents downstream localization lookup failures during SVG generation
2 parents 46283be + 1d04c74 commit bd23853

3 files changed

Lines changed: 17 additions & 5 deletions

File tree

lib/i18n/badgeLabels.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,11 @@ export const labels: Record<string, BadgeLabels> = {
7272
},
7373
};
7474

75+
export const supportedLanguages = Object.keys(labels) as [
76+
keyof typeof labels,
77+
...(keyof typeof labels)[],
78+
];
79+
7580
export function getLabels(lang: string = 'en'): BadgeLabels {
7681
return labels[lang.toLowerCase()] || labels['en'];
7782
}

lib/validations.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// lib/validations.ts
2-
2+
import { supportedLanguages } from './i18n/badgeLabels';
33
import { z } from 'zod';
44
import {
55
isValidHex,
@@ -188,7 +188,7 @@ export const streakParamsSchema = z.object({
188188
hide_title: z.string().optional().transform(toBooleanFlag),
189189
hide_background: z.string().optional().transform(toBooleanFlag),
190190
hide_stats: z.string().optional().transform(toBooleanFlag),
191-
lang: z.string().optional().default('en'),
191+
lang: z.enum(supportedLanguages).catch('en').default('en'),
192192
// Unknown view values fall back to the default dashboard view.
193193
view: z.enum(['default', 'monthly']).catch('default').default('default'),
194194
// Invalid delta formats fall back to percentage mode.

package-lock.json

Lines changed: 10 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)