Skip to content

Commit

Permalink
Split Zod schema into two parts and incorporate additional fields
Browse files Browse the repository at this point in the history
  • Loading branch information
John-Paul-Larkin committed Mar 13, 2024
1 parent e280676 commit d4ec7fc
Showing 1 changed file with 90 additions and 0 deletions.
90 changes: 90 additions & 0 deletions app/(app)/alpha/settings/profile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,93 @@ export const updateProfilePhotoUrlSchema = z.object({
});

export type saveSettingsInput = z.TypeOf<typeof saveSettingsSchema>;

export const saveSettingsFormOneSchema = z.object({
firstName: z
.string()
.trim()
.min(2, "Min name length is 2 characters.")
.max(50, "Max name length is 50 characters."),
surname: z
.string()
.trim()
.min(2, "Min name length is 2 characters.")
.max(50, "Max name length is 50 characters."),
username: z
.string()
.trim()
.min(3, "Min username length is 3 characters.")
.max(40, "Max username length is 40 characters.")
.regex(
/^[a-zA-Z0-9-]+$/,
"Username can only contain alphanumerics and dashes.",
),
location: z.string().min(1, "Location is required"),
bio: z.string().max(200).optional(),
websiteUrl: z.string().url().optional().or(z.literal("")),
emailNotifications: z.boolean().optional(),
newsletter: z.boolean().optional(),
});

export const saveSettingsFormTwoSchema = z.object({
gender: z.string().min(1, "Gender is required"),
dateOfBirth: z.date(),
employmentStatus: z
.object({
professionalOrStudent: z.string().min(1, "Select an option"),
workplace: z.string().max(30, "Max length is 30 characters."),
jobTitle: z.string().max(30, "Max length is 30 characters."),
levelOfStudy: z.string(),
course: z.string().max(30, "Max name length is 30 characters."),
})
.superRefine((val, ctx) => {
if (
val.professionalOrStudent === "Current student" &&
val.levelOfStudy === ""
) {
ctx.addIssue({
path: ["levelOfStudy"],
code: "custom",
message: "required",
});
}
if (
val.professionalOrStudent === "Current student" &&
val.course === ""
) {
ctx.addIssue({
path: ["course"],
code: "custom",
message: "required",
});
}
if (
val.professionalOrStudent === "Working professional" &&
val.workplace === ""
) {
ctx.addIssue({
path: ["workplace"],
code: "custom",
message: "required",
});
}
if (
val.professionalOrStudent === "Working professional" &&
val.jobTitle === ""
) {
ctx.addIssue({
path: ["jobTitle"],
code: "custom",
message: "required",
});
}
}),
});

export type saveSettingsFormOneInput = z.TypeOf<
typeof saveSettingsFormOneSchema
>;

export type saveSettingsFormTwoInput = z.TypeOf<
typeof saveSettingsFormTwoSchema
>;

0 comments on commit d4ec7fc

Please sign in to comment.