feat: add required parser option for loaders - #1504
Conversation
Parsers can opt into `withOptions({ required: true })` so `createLoader`
throws when a key resolves to `null` (missing from the URL, or failed to
parse) and no `defaultValue` is set to fall back to.
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
|
@Cauatn is attempting to deploy a commit to the 47ng Team on Vercel. A member of the Team first needs to authorize it. |
|
Important Review skippedAuto reviews are disabled on this repository. Please check the settings in the CodeRabbit UI or the ⚙️ Run configurationConfiguration used: Organization UI Review profile: CHILL Plan: Pro Plus Run ID: You can disable this status message by setting the Use the checkbox below for a quick retry:
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
|
Hum this feels redundant with |
A concrete case that motivated this: a page like const { orderId } = await loadOrderParams(searchParams)
const order = await db.order.findUnique({ where: { id: orderId } })
// orderId is null here, depending on the ORM/DB this either throws a
// generic, unrelated error, or in the worst case returns unexpected dataThe resulting error (if any) points at order.total being undefined three lines away, not at the actual root cause. required turns that into a clear, early failure right at the loader: orderId: parseAsInteger.withOptions({ required: true })
// → "[nuqs] Missing required search param `orderId`"So the two options are complementary rather than overlapping: |
Keep the required-parser-option check alongside the strict-parsing fix for empty queries brought in from next (47ng#1510).
I'd wager your problem is a URL design one: that A good example of this is the YouTube watch page: they encode the video ID in the Having required and optional search params (e.g. for YouTube, |
Summary
withOptions({ required: true })socreateLoader/loadSearchParamsthrows when a key resolves tonull(missing from the URL, or failed to parse) and there is nodefaultValueto fall back to. AdefaultValuealways satisfies the requirement, even when the param itself is absent from the URL.strictmode.<SinceVersion v="2.10.0">until released.Closes #1404
Test plan
vitest run --project unit src/loader.test.tspasses (26/26)loader.ts