|
| 1 | +import z from "zod"; |
| 2 | + |
| 3 | +export const CreateNoteBodySchema = z.object({ |
| 4 | + title: z |
| 5 | + .string() |
| 6 | + .trim() |
| 7 | + .min(1, { message: "Title is required and must be at least 1 character." }), |
| 8 | + content: z.string().trim().min(1, { |
| 9 | + message: "Content is required and must be at least 1 character.", |
| 10 | + }), |
| 11 | +}); |
| 12 | + |
| 13 | +export const NoteSchema = z.object({ |
| 14 | + id: z.string().cuid({ message: "id must be a valid CUID." }), |
| 15 | + title: z.string().min(1, { message: "Title must be at least 1 character." }), |
| 16 | + content: z |
| 17 | + .string() |
| 18 | + .min(1, { message: "Content must be at least 1 character." }), |
| 19 | + topicId: z.string().cuid({ message: "topicId must be a valid CUID." }), |
| 20 | + userId: z.string().cuid({ message: "userId must be a valid CUID." }), |
| 21 | + courseId: z.string().cuid({ message: "courseId must be a valid CUID." }), |
| 22 | + createdAt: z.date({ message: "createdAt must be a valid date." }), |
| 23 | + updatedAt: z.date({ message: "updatedAt must be a valid date." }), |
| 24 | +}); |
| 25 | + |
| 26 | +export const UpdateNoteBodySchema = z.object({ |
| 27 | + title: z |
| 28 | + .string() |
| 29 | + .trim() |
| 30 | + .min(1, { message: "Title must be at least 1 character." }) |
| 31 | + .optional(), |
| 32 | + content: z |
| 33 | + .string() |
| 34 | + .trim() |
| 35 | + .min(1, { message: "Content must be at least 1 character." }) |
| 36 | + .optional(), |
| 37 | +}); |
| 38 | + |
| 39 | +// valitate query string |
| 40 | +const validSortFields = new Set([ |
| 41 | + "title", |
| 42 | + "-title", |
| 43 | + "createdAt", |
| 44 | + "-createdAt", |
| 45 | + "updatedAt", |
| 46 | + "-updatedAt", |
| 47 | +]); |
| 48 | + |
| 49 | +export const GetAllNotesQuerySchema = z.object({ |
| 50 | + courseId: z |
| 51 | + .string() |
| 52 | + .cuid({ message: "courseId must be a valid CUID." }) |
| 53 | + .optional(), |
| 54 | + topicId: z |
| 55 | + .string() |
| 56 | + .cuid({ message: "topicId must be a valid CUID." }) |
| 57 | + .optional(), |
| 58 | + search: z.string().optional(), |
| 59 | + sort: z |
| 60 | + .string() |
| 61 | + .refine( |
| 62 | + (value) => { |
| 63 | + // Ensure every comma-separated value is in whitelist |
| 64 | + return value.split(",").every((field) => validSortFields.has(field)); |
| 65 | + }, |
| 66 | + { message: `Invalid sort field.` }, |
| 67 | + ) |
| 68 | + .optional(), |
| 69 | + page: z.coerce |
| 70 | + .number({ message: "page must be an number." }) |
| 71 | + .int({ message: "page must be an integer." }) |
| 72 | + .min(1, { message: "page must be at least 1." }) |
| 73 | + .default(1), |
| 74 | + limit: z.coerce |
| 75 | + .number({ message: "limit must be an number." }) |
| 76 | + .int({ message: "limit must be an integer." }) |
| 77 | + .min(1, { message: "limit must be at least 1." }) |
| 78 | + .default(10), |
| 79 | +}); |
| 80 | + |
| 81 | +// valitate params |
| 82 | +export const topicIdSchema = z.object({ |
| 83 | + topicId: z.string().trim().cuid({ message: "id must be a valid CUID." }), |
| 84 | +}); |
| 85 | +export const noteIdSchema = z.object({ |
| 86 | + noteId: z.string().trim().cuid({ message: "id must be a valid CUID." }), |
| 87 | +}); |
| 88 | + |
| 89 | +export type NoteServiceType = z.infer<typeof NoteSchema>; |
| 90 | +export type UpdateNoteServiceType = z.infer<typeof UpdateNoteBodySchema>; |
| 91 | +export type CreateNoteBodyType = z.infer<typeof CreateNoteBodySchema>; |
| 92 | +export type GetAllNotesQueryType = z.infer<typeof GetAllNotesQuerySchema>; |
0 commit comments