From 6750f69c1aa597ebb687ce705da9aa1d72fde98f Mon Sep 17 00:00:00 2001 From: anantakumarghosh Date: Mon, 29 Jul 2024 17:23:45 +0530 Subject: [PATCH] fix(utils): :white_check_mark: date validation date validation correction Ref: #63 --- app/validations/profile.validation.js | 208 ++++++++++++-------------- 1 file changed, 94 insertions(+), 114 deletions(-) diff --git a/app/validations/profile.validation.js b/app/validations/profile.validation.js index 961fd2d..8e43d41 100644 --- a/app/validations/profile.validation.js +++ b/app/validations/profile.validation.js @@ -2,177 +2,158 @@ import { getFormikRequiredMessage } from "@wrappid/core"; import moment from "moment"; import { string, date, mixed, boolean } from "yup"; +const dateValidation = date() + .typeError("Invalid date") + .test("isValidDate", "Invalid date", (value) => { + return value && moment(value).isValid(); + }); + const profileBaic = { bio: - string() - .trim() - // .required(getFormikRequiredMessage("bio")) - .matches( - /^[a-zA-Z0-9\s.'"@$&-/\\?]+$/, - "All special charecters are not allowed" - ), + string() + .trim() + .matches( + /^[a-zA-Z0-9\s.'"@$&-/\\?]+$/, + "All special characters are not allowed" + ), dob: - date() - .required(getFormikRequiredMessage("dateOfBirth")) - .min(moment().subtract(115, "years"), "MIN_AGE") - .max(moment().endOf("day").subtract(18, "years"), "Min age should be 18"), + dateValidation + .required(getFormikRequiredMessage("dateOfBirth")) + .min(moment().subtract(115, "years"), "MIN_AGE") + .max(moment().endOf("day").subtract(18, "years"), "Min age should be 18"), firstName: string() .trim() .required(getFormikRequiredMessage("firstName")) .matches(/^[a-zA-Z\s]+$/, "Only alphabets are allowed for this field "), gender: string().required("Gender is required"), lastName: - string() - .trim() - .matches(/^[a-zA-Z\s]+$/, "Only alphabets are allowed for this field "), + string() + .trim() + .matches(/^[a-zA-Z\s]+$/, "Only alphabets are allowed for this field "), middleName: - string() - .trim() - .matches(/^[a-zA-Z\s]+$/, "Only alphabets are allowed for this field "), + string() + .trim() + .matches(/^[a-zA-Z\s]+$/, "Only alphabets are allowed for this field "), photo: mixed() .test("fileSize", "Logo size is too large", (value) => { - if (!value) { - return true; // Allow empty value (optional logo) - } - if(typeof value === "string"){ - return true; - } - return value.size <= 5 * 1024 * 1024; // Check if file size is less than 5MB + if (!value || typeof value === "string") return true; + return value.size <= 5 * 1024 * 1024; }) .test("fileType", "Invalid logo format", (value) => { - if (!value) { - return true; // Allow empty value (optional logo) - } - if(typeof value === "string"){ - return true; - } + if (!value || typeof value === "string") return true; const supportedTypes = ["image/jpeg", "image/png"]; return supportedTypes.includes(value.type); }), }; -const profileEducation = { + +const profileEducation = { board: - string() - .trim() - .required("Board name is required") - .matches( - /^[a-zA-Z0-9\s-.,/()[\]]+$/, - "All special charecters are not allowed except - . , / ( ) [ ]" - ), + string() + .trim() + .required("Board name is required") + .matches( + /^[a-zA-Z0-9\s-.,/()[\]]+$/, + "All special characters are not allowed except - . , / ( ) [ ]" + ), degree: string() .trim() .required("Degree is required") .matches( /^[a-zA-Z0-9\s-.,/()[\]]+$/, - "All special charecters are not allowed except - . , / ( ) [ ]" + "All special characters are not allowed except - . , / ( ) [ ]" ), endDate: - date() - .max(new Date(), "Must be today or earlier than today") - .when("isCurrent", { - is : true, - otherwise: () => - date() - .required("End date required") - .test( - "start-end-check", - "End date should be after start date", - (val, props) => { - // -- console.log("kkikikiki", props.parent.startDate, val, moment(props.parent.startDate).diff(moment(val), 'days')); - // -- console.log("HERER", val); - if ( - props.parent.startDate && - val && - moment(val).diff(moment(props.parent.startDate), "days") > 0 - ) { - return true; - } else return false; - } - ), - then: () => date(), - }), + dateValidation + .max(new Date(), "Must be today or earlier than today") + .when("isCurrent", { + is : true, + otherwise: () => + dateValidation + .required("End date required") + .test( + "start-end-check", + "End date should be after start date", + (val, props) => { + return props.parent.startDate && val && moment(val).diff(moment(props.parent.startDate), "days") > 0; + } + ), + then: () => dateValidation, + }), isCurrent: boolean().notRequired(), school : string() .trim() .required("School name is required") .matches( /^[a-zA-Z0-9\s-.,/()[\]]+$/, - "All special charecters are not allowed except - . , / ( ) [ ]" + "All special characters are not allowed except - . , / ( ) [ ]" ), - startDate: date() + startDate: dateValidation .max(new Date(), "Must be today or earlier than today") .required("Start date is required"), }; + const profileExperience = { designation: string() .trim() .required("Designation is required") .matches( /^[a-zA-Z0-9\s-.,/()[\]]+$/, - "All special charecters are not allowed except - . , / ( ) [ ]" + "All special characters are not allowed except - . , / ( ) [ ]" ), endDate: - date() - .max(new Date(), "Must be today or earlier than today") - .when("isCurrent", { - is : true, - otherwise: () => - date() - .required("End date required") - .test( - "start-end-check", - "End date should be after start date", - (val, props) => { - // -- console.log("kkikikiki", props.parent.startDate, val, moment(props.parent.startDate).diff(moment(val), 'days')); - // -- console.log("HERER", val); - if ( - props.parent.startDate && - val && - moment(val).diff(moment(props.parent.startDate), "days") > 0 - ) { - return true; - } else return false; - } - ), - then: () => date(), - }), + dateValidation + .max(new Date(), "Must be today or earlier than today") + .when("isCurrent", { + is : true, + otherwise: () => + dateValidation + .required("End date required") + .test( + "start-end-check", + "End date should be after start date", + (val, props) => { + return props.parent.startDate && val && moment(val).diff(moment(props.parent.startDate), "days") > 0; + } + ), + then: () => dateValidation, + }), isCurrent: boolean().notRequired(), location : string() .trim() .required("Location is required") .matches( /^[a-zA-Z0-9\s-.,/()[\]]+$/, - "All special charecters are not allowed except - . , / ( ) [ ]" - ) - .required(), + "All special characters are not allowed except - . , / ( ) [ ]" + ), organization: - string() - .trim() - .required("Organization name is required") - .matches( - /^[a-zA-Z0-9\s-.,/()[\]]+$/, - "All special charecters are not allowed except - . , / ( ) [ ]" - ), + string() + .trim() + .required("Organization name is required") + .matches( + /^[a-zA-Z0-9\s-.,/()[\]]+$/, + "All special characters are not allowed except - . , / ( ) [ ]" + ), startDate: - date() + dateValidation .max(new Date(), "Must be today or earlier than today") .required("Start date is required"), }; + const profileRegistration = { departmentId: string().required("Department is required"), regDate: - date() - .max(new Date(), "Registration date must be today or earlier than today") - .required("Registration date is required"), + dateValidation + .max(new Date(), "Registration date must be today or earlier than today") + .required("Registration date is required"), regNo: - string() - .trim() - .required("Registration No. is required") - .matches( - /^[a-zA-Z0-9\s-/]+$/, - "Special charecters are not allowed except - and /" - ), + string() + .trim() + .required("Registration No. is required") + .matches( + /^[a-zA-Z0-9\s-/]+$/, + "Special characters are not allowed except - and /" + ), registrationDocument: mixed() .test("fileSize", "File size must be less than 5MB", (value) => { if (!value) return true; @@ -182,8 +163,7 @@ const profileRegistration = { "fileType", "Only PDF and Doc files allowed", (value) => { - if (!value) return true; // Skip validation if no file selected - + if (!value) return true; const supportedTypes = ["application/pdf", "application/msword"]; return supportedTypes.includes(value.type); @@ -196,4 +176,4 @@ export { profileEducation, profileExperience, profileRegistration -}; +}; \ No newline at end of file