Skip to content

Commit

Permalink
fix(utils): ✅ date validation
Browse files Browse the repository at this point in the history
date validation correction

Ref: #63
  • Loading branch information
anantakumarghosh committed Jul 29, 2024
1 parent d24c895 commit 6750f69
Showing 1 changed file with 94 additions and 114 deletions.
208 changes: 94 additions & 114 deletions app/validations/profile.validation.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
Expand All @@ -196,4 +176,4 @@ export {
profileEducation,
profileExperience,
profileRegistration
};
};

0 comments on commit 6750f69

Please sign in to comment.