Skip to content

Commit bb827a3

Browse files
authored
Feat/grace range validation (JhaSourav07#3041)
## Description Fixes JhaSourav07#1462 Updated grace validation tests and aligned `validations.ts` to correctly validate `grace` values between `0–7`. The original issue focused on tests, but validation logic also required updates so the schema behavior matched expected test outcomes. ### Changes made * Added/updated tests for `grace` validation * Ensured invalid `grace` values below `0` fail validation * Updated validation logic in `validations.ts` to align with expected schema behavior * Verified schema behavior for valid and invalid inputs ## Pillar * [ ] 🎨 Pillar 1 — New Theme Design * [ ] 📐 Pillar 2 — Geometric SVG Improvement * [ ] 🕐 Pillar 3 — Timezone Logic Optimization * [x] 🛠️ Other (Bug fix, refactoring, docs) ## Visual Preview N/A (validation and test changes only) ## Checklist before requesting a review: * [x] I have read the `CONTRIBUTING.md` file. * [x] I have tested these changes locally (`localhost:3000/api/streak?user=YOUR_USERNAME`). * [x] I have run `npm run format` and `npm run lint` locally and resolved all errors (CI will fail otherwise). * [x] My commits follow the Conventional Commits format (e.g., `feat(themes): ...`, `fix(calculate): ...`). * [ ] I have updated `README.md` if I added a new theme or URL parameter. * [x] I have started the repo. * [x] I have made sure that I have only one commit to merge in this PR. * [x] The SVG output matches the CommitPulse "premium quality" aesthetic standard (no raw elements, smooth animations, correct fonts). * [ ] (Recommended) I joined the CommitPulse Discord community for contributor discussions, mentorship, and faster PR support.
2 parents e9235b4 + 0025c3b commit bb827a3

3 files changed

Lines changed: 35 additions & 2 deletions

File tree

app/api/streak/route.test.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,24 @@ describe('GET /api/streak', () => {
277277
expect(fetchGitHubContributions).not.toHaveBeenCalled();
278278
});
279279

280+
it('returns 400 when grace is below the minimum value', async () => {
281+
const response = await GET(
282+
makeRequest({
283+
user: 'octocat',
284+
grace: '-1',
285+
})
286+
);
287+
288+
expect(response.status).toBe(400);
289+
290+
const body = await response.json();
291+
292+
expect(body.error).toBe('Invalid parameters');
293+
expect(body.details.fieldErrors.grace[0]).toBe('grace must be an integer between 0 and 7');
294+
295+
expect(fetchGitHubContributions).not.toHaveBeenCalled();
296+
});
297+
280298
it('returns 400 for unsupported ?layout query parameter values (strict schema validation)', async () => {
281299
const response = await GET(
282300
new Request('http://localhost:3000/api/streak?user=octocat&layout=unsupported_layout')

lib/validations.test.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,20 @@ describe('streakParamsSchema', () => {
229229
}
230230
});
231231

232+
it('should fail when grace is below the minimum value of 0', () => {
233+
const result = streakParamsSchema.safeParse({
234+
user: 'octocat',
235+
grace: '-1',
236+
});
237+
238+
expect(result.success).toBe(false);
239+
if (!result.success) {
240+
expect(result.error.flatten().fieldErrors.grace?.[0]).toBe(
241+
'grace must be an integer between 0 and 7'
242+
);
243+
}
244+
});
245+
232246
it('accepts a valid width value', () => {
233247
const result = streakParamsSchema.safeParse({
234248
user: 'octocat',

lib/validations.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -280,13 +280,14 @@ const baseStreakParamsSchema = z.object({
280280
.optional()
281281
.refine(
282282
(val) => {
283-
if (val === undefined) return true;
283+
if (!val) return true;
284284
const parsed = Number(val);
285285
return !isNaN(parsed) && Number.isInteger(parsed) && parsed >= 0 && parsed <= 7;
286286
},
287287
{ message: 'grace must be an integer between 0 and 7' }
288288
)
289-
.transform((val) => (val === undefined ? 1 : Number(val))),
289+
.transform(toGraceValue)
290+
.default(1),
290291
mode: z.enum(['commits', 'loc']).catch('commits').default('commits'),
291292
repo: z.string().optional(),
292293
org: z

0 commit comments

Comments
 (0)