Skip to content

Commit

Permalink
Add Zod validation to prevent creating Bill without payment option
Browse files Browse the repository at this point in the history
  • Loading branch information
ODORA0 committed Jun 18, 2024
1 parent 58f9fed commit 369b873
Showing 1 changed file with 5 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,9 @@ const servicePriceSchema = z.object({
]),
});

const paymentFormSchema = z.object({ payment: z.array(servicePriceSchema) });
const paymentFormSchema = z.object({
payment: z.array(servicePriceSchema).min(1, 'At least one payment option is required'),
});

const DEFAULT_PAYMENT_OPTION = { paymentMode: '', price: 0 };

Expand All @@ -61,7 +63,7 @@ const AddBillableService: React.FC = () => {
formState: { errors },
} = useForm<any>({
mode: 'all',
defaultValues: {},
defaultValues: { payment: [DEFAULT_PAYMENT_OPTION] },
resolver: zodResolver(paymentFormSchema),
});
const { fields, remove, append } = useFieldArray({ name: 'payment', control: control });
Expand Down Expand Up @@ -288,7 +290,7 @@ const AddBillableService: React.FC = () => {
)}
/>
<div className={styles.removeButtonContainer}>
<TrashCan onClick={handleRemovePaymentMode} className={styles.removeButton} size={20} />
<TrashCan onClick={() => handleRemovePaymentMode(index)} className={styles.removeButton} size={20} />
</div>
</div>
))}
Expand Down

0 comments on commit 369b873

Please sign in to comment.