Skip to content

Commit 7bb40d1

Browse files
authored
fix(validation): enforce year >= 2008 with consistent error message in zod schema (JhaSourav07#278)
## Description Fixes JhaSourav07#146 - `lib/validations.ts` — updated the `year` field in `streakParamsSchema` to reject any year before 2008 using a single refine check, returning a consistent "GitHub was founded in 2008. Please provide a year of 2008 or later." error message for all invalid year inputs including `non-numeric`, `out-of-range`, and `pre-2008 values`. - `app/api/streak/route.test.ts` — updated 4 test assertions to expect the new consistent error message instead of the previous 'Invalid "year" parameter' string. ## Pillar - 🛠️ Other (Bug fix, refactoring, docs) ## Visual Preview https://collection.cloudinary.com/dqvm8dce2/8a46b6e30ac67c2ca9344f3b72beb8fc ## Checklist before requesting a review: - I have read the `CONTRIBUTING.md` file. - I have tested these changes locally (`localhost:3000/api/streak?user=YOUR_USERNAME&year=....`). - I have run `npm run format` and `npm run lint` locally and resolved all errors. - My commits follow the Conventional Commits format. - I have starred the repo. - I have made sure that i have only one commit to merge in this PR.
2 parents e5e7769 + 478eee2 commit 7bb40d1

2 files changed

Lines changed: 11 additions & 14 deletions

File tree

app/api/streak/route.test.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -260,23 +260,23 @@ describe('GET /api/streak', () => {
260260
const body = await response.json();
261261

262262
expect(response.status).toBe(400);
263-
expect(body.details.fieldErrors.year[0]).toContain('Invalid "year" parameter');
263+
expect(body.details.fieldErrors.year[0]).toContain('GitHub was founded in 2008');
264264
});
265265

266266
it('returns 400 for malformed numeric year', async () => {
267267
const response = await GET(makeRequest({ user: 'octocat', year: '100000' }));
268268
const body = await response.json();
269269

270270
expect(response.status).toBe(400);
271-
expect(body.details.fieldErrors.year[0]).toContain('Invalid "year" parameter');
271+
expect(body.details.fieldErrors.year[0]).toContain('GitHub was founded in 2008');
272272
});
273273

274274
it('returns 400 for years before GitHub existed', async () => {
275275
const response = await GET(makeRequest({ user: 'octocat', year: '1999' }));
276276
const body = await response.json();
277277

278278
expect(response.status).toBe(400);
279-
expect(body.details.fieldErrors.year[0]).toContain('Invalid "year" parameter');
279+
expect(body.details.fieldErrors.year[0]).toContain('GitHub was founded in 2008');
280280
});
281281

282282
it('returns 400 for future years', async () => {
@@ -286,7 +286,7 @@ describe('GET /api/streak', () => {
286286
const body = await response.json();
287287

288288
expect(response.status).toBe(400);
289-
expect(body.details.fieldErrors.year[0]).toContain('Invalid "year" parameter');
289+
expect(body.details.fieldErrors.year[0]).toContain('GitHub was founded in 2008');
290290
});
291291
});
292292

lib/validations.ts

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -40,21 +40,18 @@ export const streakParamsSchema = z.object({
4040
.transform((val) => sanitizeFont(val) || undefined),
4141
year: z
4242
.string()
43-
.regex(/^\d{4}$/, {
44-
message: 'Invalid "year" parameter. Use YYYY format, e.g. 2024.',
45-
})
43+
.optional()
4644
.refine(
47-
(year) => {
48-
const yearNum = parseInt(year, 10);
45+
(val) => {
46+
if (!val) return true;
47+
const yearNum = parseInt(val, 10);
4948
const currentYear = new Date().getFullYear();
50-
51-
return yearNum >= 2005 && yearNum <= currentYear;
49+
return /^\d{4}$/.test(val) && yearNum >= 2008 && yearNum <= currentYear;
5250
},
5351
{
54-
message: 'Invalid "year" parameter. Use YYYY format, e.g. 2024.',
52+
message: 'GitHub was founded in 2008. Please provide a year of 2008 or later.',
5553
}
56-
)
57-
.optional(),
54+
),
5855
refresh: z
5956
.string()
6057
.optional()

0 commit comments

Comments
 (0)