boilerplate
This commit is contained in:
40
src/server/api/schemas/post.ts
Normal file
40
src/server/api/schemas/post.ts
Normal file
@@ -0,0 +1,40 @@
|
||||
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>;
|
||||
Reference in New Issue
Block a user