Skip to content

fix(api): refactor color parameter validations to use transforms and fallbacks#6127

Closed
hardik-bhalekar wants to merge 1 commit into
JhaSourav07:mainfrom
hardik-bhalekar:fix/api-parameter-validation
Closed

fix(api): refactor color parameter validations to use transforms and fallbacks#6127
hardik-bhalekar wants to merge 1 commit into
JhaSourav07:mainfrom
hardik-bhalekar:fix/api-parameter-validation

Conversation

@hardik-bhalekar

Copy link
Copy Markdown

Summary

Refactors Zod validation schemas in lib/validations.ts to use inline .transform() with regex guards instead of .refine() for color hex parameters (bgStart, bgEnd, border) and the theme field. Malformed values are now silently dropped to safe defaults rather than throwing ZodError, preventing 400-level API failures for end users embedding streak cards.

Problem

The /api/streak route uses Zod schemas to validate query parameters. When users supply malformed hex colors (e.g., bgStart=xyz) or invalid theme names, the .refine() validators throw a ZodError, surfacing a 400 error instead of gracefully falling back. This is a poor UX for a card-embedding endpoint where parameters are often hand-edited in URLs.

Changes

lib/validations.ts

Field Before After
theme .refine() threw if theme name was invalid .transform() falls back to 'dark' for unknown theme names
bgStart .refine() threw if not valid hex .transform() returns undefined for non-hex input (uses theme default)
bgEnd .refine() threw if not valid hex .transform() returns undefined for non-hex input (uses theme default)
border .refine() + .transform() pipeline threw on bad hex Single .transform() returns undefined for non-hex input

app/api/streak/route.ts

  • Added nullish coalescing (??) fallbacks for bgStart, bgEnd, and border so that undefined values (from dropped malformed input) resolve to the selected theme's defaults instead of propagating as undefined.

Design Decision

Using .transform() over .refine() is intentional: it converts the validation step into a pure mapping function that always succeeds, making the API maximally lenient for URL-embedded use cases while still sanitizing inputs.

Testing

  • npx tsc --noEmit passes with zero errors
  • Valid hex colors are still parsed and sanitized correctly
  • Malformed hex values silently fall back to theme defaults
  • Invalid theme names resolve to 'dark'

@vercel

vercel Bot commented Jun 20, 2026

Copy link
Copy Markdown
Contributor

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

A member of the Team first needs to authorize it.

@github-actions

Copy link
Copy Markdown
Contributor

👋 Hey @hardik-bhalekar! Thanks for your contribution! 🎉

Unfortunately, this PR has been automatically closed because it is not linked to any open issue.

To resolve this, please do the following:

  1. Link a valid open issue by editing your PR description to include a closing keyword (e.g., Fixes #<issue-number>).
  2. Reopen this PR once the link is added.

💡 You can link multiple issues if needed (e.g. Fixes #12, Closes #34).
If you're working on something that doesn't have an issue yet, please open one first and then link it here.

We look forward to reviewing your PR once an issue is linked! 🚀

@github-actions github-actions Bot closed this Jun 20, 2026
@github-actions github-actions Bot added the type:bug Something isn't working as expected label Jun 21, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

type:bug Something isn't working as expected

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant