Files
web-boilerplate/src/server/api/schemas/post.ts
JonLuca De Caro 5ff5ef65c3 boilerplate
2026-03-03 18:43:35 -08:00

41 lines
1.3 KiB
TypeScript

import { z } from "zod";
const nonEmptyString = (field: string) => z.string().trim().min(1, `${field} is required`);
const blankStringToUndefined = (value: unknown) => {
if (value === null || value === undefined) {
return undefined;
}
if (typeof value !== "string") {
return value;
}
const trimmedValue = value.trim();
return trimmedValue.length > 0 ? trimmedValue : undefined;
};
export const postIdSchema = nonEmptyString("Post ID");
export const listPostsInputSchema = z.object({
limit: z.coerce.number().int().min(1).max(50).optional(),
});
export const getPostByIdInputSchema = z.object({
postId: postIdSchema,
});
export const createPostInputSchema = z.object({
title: nonEmptyString("Title").max(160, "Title must be 160 characters or fewer"),
body: nonEmptyString("Body").max(10_000, "Body must be 10000 characters or fewer"),
});
export const createCommentInputSchema = z.object({
postId: postIdSchema,
body: z.preprocess(blankStringToUndefined, nonEmptyString("Comment").max(4_000, "Comment must be 4000 characters or fewer")),
});
export type ListPostsInput = z.infer<typeof listPostsInputSchema>;
export type CreatePostInput = z.infer<typeof createPostInputSchema>;
export type CreateCommentInput = z.infer<typeof createCommentInputSchema>;