2022-03-25 05:54:57 +00:00
|
|
|
import { withValidation } from "next-validations";
|
2022-03-24 23:04:07 +00:00
|
|
|
import { z } from "zod";
|
|
|
|
|
|
|
|
// Extracted out as utility function so can be reused
|
|
|
|
// at different endpoints that require this validation.
|
2022-03-26 23:58:22 +00:00
|
|
|
const schemaQueryIdParseInt = z
|
2022-03-24 23:04:07 +00:00
|
|
|
.object({
|
|
|
|
// since nextjs parses query params as strings,
|
|
|
|
// we need to cast them to numbers using z.transform() and parseInt()
|
|
|
|
id: z
|
|
|
|
.string()
|
|
|
|
.regex(/^\d+$/)
|
|
|
|
.transform((id) => parseInt(id)),
|
|
|
|
})
|
|
|
|
.strict();
|
2022-03-25 05:54:57 +00:00
|
|
|
|
2022-03-25 19:17:37 +00:00
|
|
|
const withValidQueryIdTransformParseInt = withValidation({
|
2022-03-26 23:58:22 +00:00
|
|
|
schema: schemaQueryIdParseInt,
|
2022-03-25 05:54:57 +00:00
|
|
|
type: "Zod",
|
|
|
|
mode: "query",
|
|
|
|
});
|
|
|
|
|
2022-03-26 23:58:22 +00:00
|
|
|
export { schemaQueryIdParseInt, withValidQueryIdTransformParseInt };
|