How to validate all searchParams in a GET request?
Unanswered
MooKorea posted this in #help-forum
MooKoreaOP
I have a project that has a lot of route handlers, but I'm wondering if there's a way to efficiently check if all of the parameters are provided for a GET request.
This code works fine, but I'm repeating myself way too much. Also, I have several route handlers that require a lot of parameters like this, and I feel like there's a better way to do this.
This code works fine, but I'm repeating myself way too much. Also, I have several route handlers that require a lot of parameters like this, and I feel like there's a better way to do this.
export async function GET(request: NextRequest) {
const { searchParams } = new URL(request.url);
const stocksTicker = searchParams.get("stocksTicker");
if (stocksTicker === null) {
return new Response("Error: stocksTicker not provided", { status: 400 });
}
const multiplier = searchParams.get("multiplier");
if (multiplier === null) {
return new Response("Error: multiplier not provided", { status: 400 });
}
const timespan = searchParams.get("timespan");
if (timespan === null) {
return new Response("Error: timespan not provided", { status: 400 });
}
...