From 34e38644fc8649342a9c2a7376c30161d9eff3c3 Mon Sep 17 00:00:00 2001 From: MuslemRahimi Date: Mon, 11 Nov 2024 16:16:03 +0100 Subject: [PATCH] bugfixing --- src/lib/schemas.js | 258 ++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 257 insertions(+), 1 deletion(-) diff --git a/src/lib/schemas.js b/src/lib/schemas.js index be08d4fd..c1441010 100644 --- a/src/lib/schemas.js +++ b/src/lib/schemas.js @@ -9,6 +9,15 @@ export const loginUserSchema = z.object({ export const registerUserSchema = z .object({ + username: z + .string({ required_error: "Username is required" }) + .regex(/^[a-zA-Z0-9!@#$%^&*()_+\-=\[\]{};':"\\|,.<>\/?]*$/, { + message: + "Username can only contain letters, numbers, and special characters.", + }) // Updated regex pattern + .min(3, { message: "Username must be at least 2 characters" }) + .max(64, { message: "Username must be less than 64 characters" }) + .trim(), email: z .string({ required_error: "Email is required" }) .email({ message: "Email must be a valid email" }), @@ -46,8 +55,143 @@ export const registerUserSchema = z } }); +export const createPostTextSchema = z.object({ + title: z + .string({ required_error: "Title is required" }) + .min(1, { message: "Title is required" }) + .max(300, { message: "Title must be 300 characters or less" }) + .trim(), + //url: z.string().optional().url({ message: 'URL must be a valid URL' }), + tagline: z.string(), + tagTopic: z.string(), + atLeastOneTag: z + .string({ required_error: "At least 1 tag is required" }) + .min(1, { message: "At least 1 tag is required" }), + description: z.string(), + postType: z.string({ required_error: "PostType is required." }), + user: z.string({ required_error: "User is required." }), +}); +const imageTypes = [ + "image/jpeg", + "image/jpg", + "image/png", + "image/webp", + "image/svg+xml", + "image/gif", +]; + +const videoTypes = ["video/mp4", "video/gif"]; + +export const createPostImageSchema = z.object({ + title: z + .string({ required_error: "Title is required" }) + .min(1, { message: "Title is required" }) + .max(300, { message: "Title must be 300 characters or less" }) + .trim(), + tagTopic: z.string(), + tagline: z.string(), + atLeastOneTag: z + .string({ required_error: "At least 1 tag is required" }) + .min(1, { message: "At least 1 tag is required" }), + thumbnail: z.instanceof(File).superRefine((val, ctx) => { + if (val) { + if (val.size > 5121440) { + ctx.addIssue({ + code: z.ZodIssueCode.custom, + message: "File must be less than 5MB", + }); + } + + if ( + val.type && + !imageTypes.includes(val.type) && + !videoTypes.includes(val.type) + ) { + ctx.addIssue({ + code: z.ZodIssueCode.custom, + message: + "Unsupported file type. Supported formats: jpeg, jpg, png, webp, svg", + }); + } + } + }), + postType: z.string({ required_error: "PostType is required." }), + user: z.string({ required_error: "User is required." }), +}); + +export const createPostLinkSchema = z.object({ + title: z + .string({ required_error: "Title is required" }) + .min(1, { message: "Title is required" }) + .max(300, { message: "Title must be 300 characters or less" }) + .trim(), + tagTopic: z.string(), + tagline: z.string(), + atLeastOneTag: z + .string({ required_error: "At least 1 tag is required" }) + .min(1, { message: "At least 1 tag is required" }), + link: z.string().url({ message: "URL must be a valid URL" }), + description: z.string(), + /* + thumbnail: z + .instanceof(Blob) + .superRefine((val, ctx) => { + if (val) { + if (val.size > 5242880) { + ctx.addIssue({ + code: z.ZodIssueCode.custom, + message: 'Image must be less than 5MB' + }); + } + + if (!imageTypes.includes(val.type)) { + ctx.addIssue({ + code: z.ZodIssueCode.custom, + message: 'Unsupported file type. Supported formats: jpeg, jpg, png, webp, svg, gif' + }); + } + } + }), + */ + postType: z.string({ required_error: "PostType is required." }), + user: z.string({ required_error: "User is required." }), +}); + +//export const updatePostSchema = createPostSchema.omit({ user: true }); + +export const updatePersonalDataSchema = z.object({ + /* + email: z + .string({ required_error: 'Email is required' }) + .email({ message: 'Email must be a valid email' }), + */ + username: z + .string({ required_error: "Username is required" }) + .min(2, { message: "Username must be at least 2 characters" }) + .max(24, { message: "Username must be 24 characters or less" }) + .regex(/^[a-zA-Z0-9!@#$%^&*()_+\-=\[\]{};':"\\|,.<>\/?]*$/, { + message: + "Username can only contain letters, numbers, and special characters.", + }), // Updated regex pattern +}); + +export const updateEmailSchema = z.object({ + email: z + .string({ required_error: "Email is required" }) + .email({ message: "Email must be a valid email" }), +}); + +export const updateUsernameSchema = z.object({ + username: z + .string({ required_error: "Username is required" }) + .min(2, { message: "Username must be at least 2 characters" }) + .max(24, { message: "Username must be 24 characters or less" }) + .regex(/^[a-zA-Z0-9]*$/, { + message: "Username can only contain letters or numbers.", + }), +}); export const updatePasswordSchema = z .object({ @@ -86,8 +230,120 @@ export const updatePasswordSchema = z } }); +export const updateProfileSchema = z.object({ + name: z + .string({ required_error: "Name is required" }) + .min(1, { message: "Name is required" }) + .max(64, { message: "Name must be 64 characters or less" }) + .trim(), + avatar: z + .instanceof(Blob) + .optional() + .superRefine((val, ctx) => { + if (val) { + if (val.size > 5242880) { + ctx.addIssue({ + code: z.ZodIssueCode.custom, + message: "Avatar must be less than 5MB", + }); + } + if (!imageTypes.includes(val.type)) { + ctx.addIssue({ + code: z.ZodIssueCode.custom, + message: + "Unsupported file type. Supported formats: jpeg, jpg, png, webp, svg, gif", + }); + } + } + }), +}); +export const updateAvatarSchema = z.object({ + avatar: z.instanceof(File).superRefine((val, ctx) => { + if (val) { + if (val.size > 5242880) { + ctx.addIssue({ + code: z.ZodIssueCode.custom, + message: "Avatar must be less than 5MB", + }); + } + + if ( + val.type && + !imageTypes.includes(val.type) && + !videoTypes.includes(val.type) + ) { + ctx.addIssue({ + code: z.ZodIssueCode.custom, + message: + "Unsupported file type. Supported formats: jpeg, jpg, png, webp, svg, gif", + }); + } + } + }), +}); + +export const createCommentTextSchema = z.object({ + comment: z + .string({ required_error: "Comment cannot be empty" }) + .min(1, { message: "Comment cannot be empty" }) + //.max(40000, { message: 'Comment is too long. Keep it simple and concise bruv!' }) + .trim(), + user: z.string({ required_error: "User is required." }), + post: z.string({ required_error: "Post is required." }), + reply: z.string(), +}); + +export const updateCommentTextSchema = z.object({ + comment: z + .string({ required_error: "Comment cannot be empty" }) + .min(1, { message: "Comment cannot be empty" }) + //.max(40000, { message: 'Comment is too long. Keep it simple and concise bruv!' }) + .trim(), +}); + +export const createCommentImageSchema = z.object({ + comment: z + .string({ required_error: "Comment cannot be empty" }) + .min(1, { message: "Comment cannot be empty" }) + //.max(40000, { message: 'Comment is too long. Keep it simple and concise bruv!' }) + .trim(), + image: z + .instanceof(Blob, { message: "Image is required" }) + .superRefine((val, ctx) => { + if (val) { + if (val.size > 5242880) { + ctx.addIssue({ + code: z.ZodIssueCode.custom, + message: "Image must be less than 5MB", + }); + } + + if (!imageTypes.includes(val.type)) { + ctx.addIssue({ + code: z.ZodIssueCode.custom, + message: + "Unsupported file type. Supported formats: jpeg, jpg, png, webp, svg, gif", + }); + } + } + }), + user: z.string({ required_error: "User is required." }), + post: z.string({ required_error: "Post is required." }), + reply: z.string(), +}); + +export const createNotebookSchema = z.object({ + title: z + .string({ required_error: "Title cannot be empty" }) + .min(1, { message: "Title cannot be empty" }) + .max(100, { + message: "Title is too long. Keep it simple and concise bruv!", + }) + .trim(), + user: z.string({ required_error: "User is required." }), +}); export const createStrategySchema = z.object({ title: z @@ -135,4 +391,4 @@ export const editWatchListSchema = z.object({ }) .trim(), watchListId: z.string({ required_error: "Id is required." }), -}); +}); \ No newline at end of file