import * as Yup from "yup"; import { calcWorkloadSum } from "./../../services/projectService"; const REQUIRED_FIELD_ERROR_MESSAGE = "Field is Required"; const AUTHOR_NAME_PATTERN = /^[A-Z][a-z]+\s[A-Z][a-z]+$/; const DATE_PATTERN = /^\d{2}\s[A-Z][a-z]{2}\s\d{4}$/; const VALID_NUMBER_PATTERN = /^\d+(\.\d+)?$/; const INVALID_DATE_ERROR_MESSAGE = "Please provide the date in valid format(dd MMM yyyy)"; const PROVIDE_A_VALID_NUMBER = "Please provide a valid number"; export const VALID_PAYMENT_SCHEMAS = ["40/40/10/10", "70/30", "100"]; const isNotEmpty = val => { if (!val) return false; const trimmed = val.trim(); return !(trimmed === "" || trimmed === " "); }; // function isBeforeDate(val, dateName, parent) { // const date = this.parent[dateName]; // console.log(date, dateName); // if (!date || !val) { // return true; // } // return Date.parse(val) >= Date.parse(date); // } export const validationSchema = Yup.object({ release: Yup.string().required(REQUIRED_FIELD_ERROR_MESSAGE), date: Yup.string() .required(REQUIRED_FIELD_ERROR_MESSAGE) .matches(DATE_PATTERN, INVALID_DATE_ERROR_MESSAGE), proxiadRefNumber: Yup.string().test( "is not empty", "Proxiad ref number cannot be empty", isNotEmpty ), boursoramaRefNumber: Yup.string(), billingEntity: Yup.string().required(REQUIRED_FIELD_ERROR_MESSAGE), author: Yup.string() .required(REQUIRED_FIELD_ERROR_MESSAGE) .matches( AUTHOR_NAME_PATTERN, "Please provide a valid name e.g(Ivan Ivanov)" ), inputDocumentTitle: Yup.string(), inputDocumentReceivedOn: Yup.string().matches( DATE_PATTERN, INVALID_DATE_ERROR_MESSAGE ), inputDocumentSendBy: Yup.string(), stratOfDevelopment: Yup.string().matches( DATE_PATTERN, INVALID_DATE_ERROR_MESSAGE ), deliveryInProduction: Yup.string() .matches(DATE_PATTERN, INVALID_DATE_ERROR_MESSAGE) .test( "is after delivery for testing", "Date must be after delivery for testing", function(val) { const { deliveryForTesting } = this.parent; if (!deliveryForTesting || !val) { return true; } return Date.parse(val) >= Date.parse(deliveryForTesting); } ), deliveryForTesting: Yup.string() .matches(DATE_PATTERN, INVALID_DATE_ERROR_MESSAGE) .test( "is after start date", "Date must be after start of development", function(val) { const { stratOfDevelopment } = this.parent; if (!stratOfDevelopment || !val) { return true; } return Date.parse(val) >= Date.parse(stratOfDevelopment); } ), paymentSchema: Yup.string().oneOf( VALID_PAYMENT_SCHEMAS, `Payment schema should be one of: ${VALID_PAYMENT_SCHEMAS.join(" ")}` ), macroEstimationReceivedOn: Yup.string().matches( DATE_PATTERN, INVALID_DATE_ERROR_MESSAGE ), macroEstimationSendBy: Yup.string(), macroEstimationTitle: Yup.string(), requirementsAnalysis: Yup.string(), devWorkloadMd: Yup.number() .test( "is equal to dev workload sum", "Should match the sum of dev workload", function(val) { const { devWorkload } = this.parent; return Number(val) === calcWorkloadSum(devWorkload); } ) .typeError(PROVIDE_A_VALID_NUMBER), workloadDesign: Yup.string() .typeError(PROVIDE_A_VALID_NUMBER) .matches(VALID_NUMBER_PATTERN, PROVIDE_A_VALID_NUMBER), workloadTest: Yup.string() .typeError(PROVIDE_A_VALID_NUMBER) .matches(VALID_NUMBER_PATTERN, PROVIDE_A_VALID_NUMBER), workloadUat: Yup.string() .typeError(PROVIDE_A_VALID_NUMBER) .matches(VALID_NUMBER_PATTERN, PROVIDE_A_VALID_NUMBER), workloadManagement: Yup.string() .typeError(PROVIDE_A_VALID_NUMBER) .matches(VALID_NUMBER_PATTERN, PROVIDE_A_VALID_NUMBER), workloadOther: Yup.string() .typeError(PROVIDE_A_VALID_NUMBER) .matches(VALID_NUMBER_PATTERN, PROVIDE_A_VALID_NUMBER), totalBudget: Yup.string() .matches(VALID_NUMBER_PATTERN, PROVIDE_A_VALID_NUMBER) .test( "is equal to sum of all components", "Incorrect Budget value", function(val) { const { devWorkloadMd, workloadDesign, workloadTest, workloadUat, workloadManagement, workloadOther } = this.parent; const sum = Number(devWorkloadMd) + Number(workloadDesign) + Number(workloadTest) + Number(workloadUat) + Number(workloadManagement) + Number(workloadOther); if (isNaN(sum)) { return true; } return Number(val) === sum; } ) });