Skip to content

Commit b7a215b

Browse files
committed
rebase fixed
1 parent 8d3fc1b commit b7a215b

2 files changed

Lines changed: 26 additions & 7 deletions

File tree

app/api/streak/route.test.ts

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,18 @@ describe('GET /api/streak', () => {
103103
});
104104

105105
describe('parameter validation', () => {
106+
it('returns 400 when grace=-1 is provided', async () => {
107+
const response = await GET(
108+
makeRequest({
109+
user: 'octocat',
110+
grace: '-1',
111+
})
112+
);
113+
expect(response.status).toBe(400);
114+
const body = await response.json();
115+
expect(body.details.fieldErrors.grace[0]).toBe('grace must be an integer between 0 and 7');
116+
});
117+
106118
it('returns 400 Bad Request when ?layout= is set to an unsupported format', async () => {
107119
const response = await GET(
108120
makeRequest({
@@ -305,18 +317,15 @@ describe('GET /api/streak', () => {
305317
expect(fetchGitHubContributions).toHaveBeenCalled();
306318
});
307319

308-
it('returns valid SVG when grace exceeds max value', async () => {
320+
it('returns 400 when grace exceeds max value', async () => {
309321
const response = await GET(
310322
makeRequest({
311323
user: 'octocat',
312324
grace: '999',
313325
})
314326
);
315327

316-
expect(response.status).toBe(200);
317-
318-
const body = await response.text();
319-
expect(body).toContain('<svg');
328+
expect(response.status).toBe(400);
320329
});
321330

322331
it('embeds the username (uppercased) in the SVG title', async () => {

lib/validations.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -223,8 +223,18 @@ const baseStreakParamsSchema = z.object({
223223
delta_format: z.enum(['percent', 'absolute', 'both']).catch('percent').default('percent'),
224224
width: dimensionParam('width', 100, 1200),
225225
height: dimensionParam('height', 80, 800),
226-
grace: z.string().optional().transform(toGraceValue).default(1),
227-
opacity: z.string().optional().transform(toOpacityValue).default(1.0),
226+
grace: z
227+
.string()
228+
.optional()
229+
.refine(
230+
(val) => {
231+
if (val === undefined) return true;
232+
const parsed = Number(val);
233+
return !isNaN(parsed) && Number.isInteger(parsed) && parsed >= 0 && parsed <= 7;
234+
},
235+
{ message: 'grace must be an integer between 0 and 7' }
236+
)
237+
.transform((val) => (val === undefined ? 1 : Number(val))),
228238
mode: z.enum(['commits', 'loc']).catch('commits').default('commits'),
229239
repo: z.string().optional(),
230240
org: z

0 commit comments

Comments
 (0)