Skip to content

Commit 8708e62

Browse files
committed
fix(og): resolve OpenGraph query parameter desynchronization
1 parent 98ae406 commit 8708e62

2 files changed

Lines changed: 71 additions & 33 deletions

File tree

lib/validations.test.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,15 @@ describe('githubParamsSchema', () => {
5151
}
5252
});
5353
});
54+
describe('streakParamsSchema user validation', () => {
55+
it('should pass when user is valid', () => {
56+
const result = streakParamsSchema.safeParse({
57+
user: 'octocat',
58+
});
59+
60+
expect(result.success).toBe(true);
61+
});
62+
});
5463

5564
describe('streakParamsSchema', () => {
5665
it('should fail when user is missing', () => {
@@ -402,4 +411,24 @@ describe('ogParamsSchema', () => {
402411
expect(result.data.user).toBe('unknown');
403412
}
404413
});
414+
415+
it('should parse "user" parameter successfully', () => {
416+
const result = ogParamsSchema.parse({ user: 'octocat' });
417+
expect(result.user).toBe('octocat');
418+
});
419+
420+
it('should parse "username" parameter successfully and fallback to user key', () => {
421+
const result = ogParamsSchema.parse({ username: 'octocat' });
422+
expect(result.user).toBe('octocat');
423+
});
424+
425+
it('should prioritize "user" over "username" when both are provided', () => {
426+
const result = ogParamsSchema.parse({ user: 'octocat', username: 'ignored' });
427+
expect(result.user).toBe('octocat');
428+
});
429+
430+
it('should fall back to "unknown" when neither are provided', () => {
431+
const result = ogParamsSchema.parse({});
432+
expect(result.user).toBe('unknown');
433+
});
405434
});

lib/validations.ts

Lines changed: 42 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -152,39 +152,48 @@ export const githubParamsSchema = z.object({
152152
.transform((val) => val === 'true'),
153153
});
154154

155-
export const ogParamsSchema = z.object({
156-
user: z
157-
.string()
158-
.trim()
159-
.optional()
160-
.transform((val) => (val === '' ? undefined : val))
161-
.default('unknown'),
162-
theme: z
163-
.string()
164-
.trim()
165-
.optional()
166-
.transform((val) => (val === '' ? undefined : val))
167-
.transform((val) => (val && Object.hasOwn(themes, val) ? val : 'dark'))
168-
.default('dark'),
169-
bg: z
170-
.string()
171-
.trim()
172-
.optional()
173-
.transform((val) => (val === '' ? undefined : val))
174-
.transform((val) => (val && isValidHex(val) ? sanitizeHexColor(val, '000000') : undefined)),
175-
text: z
176-
.string()
177-
.trim()
178-
.optional()
179-
.transform((val) => (val === '' ? undefined : val))
180-
.transform((val) => (val && isValidHex(val) ? sanitizeHexColor(val, '000000') : undefined)),
181-
accent: z
182-
.string()
183-
.trim()
184-
.optional()
185-
.transform((val) => (val === '' ? undefined : val))
186-
.transform((val) => (val && isValidHex(val) ? sanitizeHexColor(val, '000000') : undefined)),
187-
});
155+
export const ogParamsSchema = z
156+
.object({
157+
user: z
158+
.string()
159+
.trim()
160+
.optional()
161+
.transform((val) => (val === '' ? undefined : val)),
162+
username: z
163+
.string()
164+
.trim()
165+
.optional()
166+
.transform((val) => (val === '' ? undefined : val)),
167+
theme: z
168+
.string()
169+
.trim()
170+
.optional()
171+
.transform((val) => (val === '' ? undefined : val))
172+
.transform((val) => (val && Object.hasOwn(themes, val) ? val : 'dark'))
173+
.default('dark'),
174+
bg: z
175+
.string()
176+
.trim()
177+
.optional()
178+
.transform((val) => (val === '' ? undefined : val))
179+
.transform((val) => (val && isValidHex(val) ? sanitizeHexColor(val, '000000') : undefined)),
180+
text: z
181+
.string()
182+
.trim()
183+
.optional()
184+
.transform((val) => (val === '' ? undefined : val))
185+
.transform((val) => (val && isValidHex(val) ? sanitizeHexColor(val, '000000') : undefined)),
186+
accent: z
187+
.string()
188+
.trim()
189+
.optional()
190+
.transform((val) => (val === '' ? undefined : val))
191+
.transform((val) => (val && isValidHex(val) ? sanitizeHexColor(val, '000000') : undefined)),
192+
})
193+
.transform((data) => ({
194+
...data,
195+
user: data.user || data.username || 'unknown',
196+
}));
188197

189198
export const statsParamsSchema = z.object({
190199
user: z.string({ error: 'Missing user parameter' }).min(1, { message: 'Missing user parameter' }),

0 commit comments

Comments
 (0)