Function validateQuery

  • Validate the query parameters in a request with a Zod schema. Throws an exception if the query parameters do not match the schema. You can use ExceptionWrapper to automatically catch the exception and send it to the client.

    Type Parameters

    • T extends ZodType<any, ZodTypeDef, any>

    Parameters

    • request: Pick<NextRequest, "url">

      The next/server#NextRequest object to validate the query parameters for

    • schema: T

      The Zod schema to validate the query parameters against

    Returns z.infer<T>

    The validated query parameters

    import { validateQuery } from 'next-api-utils';
    import { z } from 'zod';

    export const GET = (request) => {
    const query = validateQuery(request, z.object({ foo: z.string() }));
    };

    InvalidQueryParametersException Thrown if the query parameters failed validation