Files
hounam-submit-form-frontend/ui/forms/workExperience/validation/index.ts
2026-06-02 17:08:52 +03:30

62 lines
2.2 KiB
TypeScript

import * as Yup from "yup";
export const WorkExperienceValidationSchema = Yup.object({
workExperiences: Yup.array()
.of(
Yup.object({
id: Yup.mixed().optional(),
hasNoExperience: Yup.boolean().required(),
companyName: Yup.string().when("hasNoExperience", {
is: false,
then: (schema) => schema.required("نام شرکت الزامی است"),
otherwise: (schema) => schema.optional(),
}),
lastPosition: Yup.string().when("hasNoExperience", {
is: false,
then: (schema) => schema.required("آخرین سمت الزامی است"),
otherwise: (schema) => schema.optional(),
}),
startYear: Yup.string().when("hasNoExperience", {
is: false,
then: (schema) =>
schema
.required("سال شروع الزامی است")
.matches(/^\d{4}$/, "سال شروع باید 4 رقم باشد"),
otherwise: (schema) => schema.optional(),
}),
endYear: Yup.string().when("hasNoExperience", {
is: false,
then: (schema) =>
schema
.required("سال پایان الزامی است")
.matches(/^\d{4}$/, "سال پایان باید 4 رقم باشد"),
otherwise: (schema) => schema.optional(),
}),
leavingReason: Yup.string().when("hasNoExperience", {
is: false,
then: (schema) => schema.required("علت ترک کار الزامی است"),
otherwise: (schema) => schema.optional(),
}),
description: Yup.string().optional(),
}),
)
.min(1, "حداقل یک رکورد باید وجود داشته باشد")
.test(
"single-no-experience-item",
"در صورت انتخاب فاقد سابقه، فقط یک رکورد مجاز است",
(value) => {
if (!value || value.length === 0) return false;
const hasNoExperience = value.some((item) => item.hasNoExperience);
if (!hasNoExperience) return true;
return value.length === 1 && value[0].hasNoExperience === true;
},
)
.required("ثبت سابقه کاری الزامی است"),
});