cal.pub0.org/lib/validations/shared/queryIdTransformParseInt.ts

27 lines
836 B
TypeScript
Raw Normal View History

2022-03-25 05:54:57 +00:00
import { withValidation } from "next-validations";
import { z } from "zod";
// Extracted out as utility function so can be reused
// at different endpoints that require this validation.
const schemaQueryIdParseInt = z
.object({
2022-03-29 01:23:22 +00:00
// since we added apiKey as query param this is required by next-validations helper
// for query params to work properly and not fail.
apiKey: z.string().cuid(),
// 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
const withValidQueryIdTransformParseInt = withValidation({
schema: schemaQueryIdParseInt,
2022-03-25 05:54:57 +00:00
type: "Zod",
mode: "query",
});
export { schemaQueryIdParseInt, withValidQueryIdTransformParseInt };