Skip to content

Commit

Permalink
trim validation added
Browse files Browse the repository at this point in the history
  • Loading branch information
sinanptm committed Aug 16, 2024
1 parent 46b54d0 commit e738b3d
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 29 deletions.
18 changes: 14 additions & 4 deletions client/components/forms/RegistrationForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,30 @@ import { zodResolver } from "@hookform/resolvers/zod";
import { useForm } from "react-hook-form";
import { z } from "zod";
import { Form, FormControl } from "@/components/ui/form";
import CustomFormField from "@/components/utils/CustomFormField"
import CustomFormField from "@/components/utils/CustomFormField";
import SubmitButton from "@/components/utils/SubmitButton";
import { registerFormValidation } from "@/lib/userValidation";
import { RadioGroup, RadioGroupItem } from "@/components/ui/radio-group";
import { Label } from "@radix-ui/react-label";
import { BloodTypes, DiseaseOptions, GenderOptions, PatientFormDefaultValues } from "@/constants";
import { BloodTypes, DiseaseOptions, GenderOptions } from "@/constants";
import { SelectItem } from "../ui/select";

const RegistrationForm = () => {
const [isLoading, setIsLoading] = useState<boolean>(false);
// const [login] = useLoginMutation();
const form = useForm<z.infer<typeof registerFormValidation>>({
resolver: zodResolver(registerFormValidation),
defaultValues: {...PatientFormDefaultValues},
defaultValues: {
birthDate: new Date(Date.now()),
gender: "Male",
address: "",
occupation: "",
concent: false,
disclosureConsent: false,
privacyConsent: false,
bloodType:"O+",
disease:"none"
},
});

const onSubmit = async (values: z.infer<typeof registerFormValidation>) => {
Expand Down Expand Up @@ -47,7 +57,7 @@ const RegistrationForm = () => {
<section className="mb-12 space-y-4">
<h3 className="header">Personal information 🧑‍⚕️</h3>
</section>

{/* BirthDate & Gender */}
<div className="flex flex-col gap-6 xl:flex-row">
<CustomFormField
Expand Down
12 changes: 0 additions & 12 deletions client/constants/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,15 +74,3 @@ export const DiseaseOptions: string[] = [
"Meningitis",
];

export const PatientFormDefaultValues = {
name: "",
email: "",
phone: "",
birthDate: new Date(Date.now()),
gender: "Male" as Gender,
address: "",
occupation: "",
concent: false,
disclosureConsent: false,
privacyConsent: false,
};
50 changes: 37 additions & 13 deletions client/lib/userValidation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,36 @@ import { z } from "zod";
export const signinFormValidation = z.object({
phone: z
.string()
.trim()
.refine(
(phone) => /^\+?[1-9]\d{1,14}$/.test(phone),
"Invalid phone number"
),
password: z.string().min(4, "Password must be at least 4 characters long"),
password: z
.string()
.trim()
.min(4, "Password must be at least 4 characters long"),
});

export const signupFormValidation = z
.object({
name: z
.string()
.trim()
.min(3, "Full Name must be at least 3 characters long")
.max(50, "Name must be at most 50 characters."),
email: z.string().email("Invalid email address"),
email: z
.string()
.trim()
.email("Invalid email address"),
phone: z
.string()
.trim()
.min(10, "Phone number must be at least 10 digits")
.max(15, "Phone number must be at most 15 digits"),
// password: z.string()
// .min(4, "Password must be at least 4 characters long"),
password: z
.string()
.trim()
.min(8, "Password must be at least 8 characters long")
.max(20, "Password must be at most 15 characters long")
.regex(/[A-Z]/, "Password must contain at least one uppercase letter")
Expand All @@ -36,6 +44,7 @@ export const signupFormValidation = z
),
confirmPassword: z
.string()
.trim()
.min(8, "Password must be at least 4 characters long"),
})
.superRefine(({ confirmPassword, password }, ctx) => {
Expand All @@ -52,7 +61,10 @@ export const registerFormValidation = z.object({
birthDate: z.coerce.date(),
gender: z.enum(["Male", "Female", "Other"]),
bloodType: z.enum(["A+", "A-", "B+", "B-", "O+", "O-", "AB+", "AB-"]),
disease: z.string().min(1, "Primary Disease is required"),
disease: z
.string()
.trim()
.min(1, "Primary Disease is required"),
privacyConsent: z
.boolean()
.default(false)
Expand All @@ -71,14 +83,26 @@ export const registerFormValidation = z.object({
.refine((val) => val === true, {
message: "You must consent to disclosure in order to proceed",
}),
address: z.string().min(4, "Address is required"),
occupation: z.string().min(3, "Occupation is required"),
address: z
.string()
.trim()
.min(4, "Address is required"),
occupation: z
.string()
.trim()
.min(3, "Occupation is required"),
});

export const appointmentFormValidation = z.object({
appointmentType:z.enum(['outpatient','inpatient']),
reason: z.string().min(5, "Reason must be at least 5 characters long"),
note:z.string().min(5, "Notes must be at least 5 characters long"),
schedule:z.coerce.date(),
payment:z.enum(['online','payment'])
})
appointmentType: z.enum(['outpatient','inpatient']),
reason: z
.string()
.trim()
.min(5, "Reason must be at least 5 characters long"),
note: z
.string()
.trim()
.min(5, "Notes must be at least 5 characters long"),
schedule: z.coerce.date(),
payment: z.enum(['online','payment']),
});

0 comments on commit e738b3d

Please sign in to comment.