Skip to content

Fix API: Validate and Clamp Grace Parameter in calculateStreak Function#8356

Open
tamilr0727-ux wants to merge 1 commit into
JhaSourav07:mainfrom
tamilr0727-ux:fix/clamp-grace-parameter
Open

Fix API: Validate and Clamp Grace Parameter in calculateStreak Function#8356
tamilr0727-ux wants to merge 1 commit into
JhaSourav07:mainfrom
tamilr0727-ux:fix/clamp-grace-parameter

Conversation

@tamilr0727-ux

Copy link
Copy Markdown
Contributor

Description

Fixes validation for the grace parameter in calculateStreak() by enforcing the documented range of 0–7 before the value is used in streak calculations.

Changes

  • Added input validation for the `grace` parameter.
    
  • Clamped values greater than 7 to 7.
    
  • Clamped values less than 0 to 0.
    
  • Added handling for invalid or non-numeric inputs (`NaN`, strings, etc.) by falling back to a safe default value.
    
  • Ensured the implementation matches the documented behavior and prevents inconsistent streak calculations.
    

Problem Addressed

Previously, calculateStreak() accepted arbitrary grace values without validation, resulting in behavior that differed from the documentation. Invalid values such as excessively large numbers, negative values, or non-numeric inputs could produce inconsistent results.

This update guarantees that the function always operates with a valid grace value within the supported range.
Validation

Implemented tests covering:

  • Valid `grace` values within the supported range.
    
  • Values greater than 7 being clamped to 7.
    
  • Negative values being clamped to 0.
    
  • Invalid inputs (`NaN`, non-numeric values) falling back to the default.
    
  • Existing valid behavior remaining unchanged.
    

Verification

✅ `vitest run` passes successfully.
✅ `calculateStreak()` now behaves consistently with the documented API contract.

Fixes #6739

Pillar

  • 🎨 Pillar 1 — New Theme Design
  • 📐 Pillar 2 — Geometric SVG Improvement
  • 🕐 Pillar 3 — Timezone Logic Optimization
  • 🛠️ Other (Bug fix, refactoring, docs)

Visual Preview

Can Check in the Files Changed Section.

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).
  • I have run npm run format and npm run lint locally and resolved all errors (CI will fail otherwise).
  • 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.
  • I have started the repo.
  • I have made sure that i have only one commit to merge in this PR.
  • 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.

@vercel

vercel Bot commented Jul 26, 2026

Copy link
Copy Markdown
Contributor

@tamilr0727-ux is attempting to deploy a commit to the jhasourav07's projects Team on Vercel.

A member of the Team first needs to authorize it.

@retenta-bot

retenta-bot Bot commented Jul 26, 2026

Copy link
Copy Markdown

This PR addresses the validation of the grace parameter in the calculateStreak() function, ensuring it adheres to the documented range and prevents inconsistent behavior. This change is crucial as it builds upon previous decisions made to fix dead code in the same function, which aimed to enhance the accuracy of streak calculations. Thank you for your contribution, and I look forward to your updates!

@retenta-bot retenta-bot Bot changed the title fix(api): clamp grace parameter to valid range [0, 7] in calculateStreak Fix API: Validate and Clamp Grace Parameter in calculateStreak Function Jul 26, 2026
@github-actions

Copy link
Copy Markdown
Contributor

📦 Next.js Bundle Size Report (Gzipped Sizes)

✨ No significant bundle size changes detected.

📊 Summary of Totals

Category PR Size Base Size Difference
Total JS 4052.50 KB 4052.50 KB 0 B
Total CSS 338.94 KB 338.94 KB 0 B

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

bug(api): grace parameter is not clamped to documented range [0–7] in calculateStreak

1 participant