Facebook
From Thundering Matamata, 4 Years ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 126
  1. import * as Yup from "yup";
  2. import { calcWorkloadSum } from "./../../services/projectService";
  3.  
  4. const REQUIRED_FIELD_ERROR_MESSAGE = "Field is Required";
  5. const AUTHOR_NAME_PATTERN = /^[A-Z][a-z]+\s[A-Z][a-z]+$/;
  6. const DATE_PATTERN = /^\d{2}\s[A-Z][a-z]{2}\s\d{4}$/;
  7. const VALID_NUMBER_PATTERN = /^\d+(\.\d+)?$/;
  8. const INVALID_DATE_ERROR_MESSAGE =
  9.   "Please provide the date in valid format(dd MMM yyyy)";
  10. const PROVIDE_A_VALID_NUMBER = "Please provide a valid number";
  11. export const VALID_PAYMENT_SCHEMAS = ["40/40/10/10", "70/30", "100"];
  12.  
  13. const isNotEmpty = val => {
  14.   if (!val) return false;
  15.   const trimmed = val.trim();
  16.   return !(trimmed === "" || trimmed === " ");
  17. };
  18.  
  19. // function isBeforeDate(val, dateName, parent) {
  20. //   const date = this.parent[dateName];
  21. //   console.log(date, dateName);
  22. //   if (!date || !val) {
  23. //     return true;
  24. //   }
  25. //   return Date.parse(val) >= Date.parse(date);
  26. // }
  27.  
  28. export const validationSchema = Yup.object({
  29.   release: Yup.string().required(REQUIRED_FIELD_ERROR_MESSAGE),
  30.   date: Yup.string()
  31.     .required(REQUIRED_FIELD_ERROR_MESSAGE)
  32.     .matches(DATE_PATTERN, INVALID_DATE_ERROR_MESSAGE),
  33.   proxiadRefNumber: Yup.string().test(
  34.     "is not empty",
  35.     "Proxiad ref number cannot be empty",
  36.     isNotEmpty
  37.   ),
  38.   boursoramaRefNumber: Yup.string(),
  39.   billingEntity: Yup.string().required(REQUIRED_FIELD_ERROR_MESSAGE),
  40.   author: Yup.string()
  41.     .required(REQUIRED_FIELD_ERROR_MESSAGE)
  42.     .matches(
  43.       AUTHOR_NAME_PATTERN,
  44.       "Please provide a valid name e.g(Ivan Ivanov)"
  45.     ),
  46.   inputDocumentTitle: Yup.string(),
  47.   inputDocumentReceivedOn: Yup.string().matches(
  48.     DATE_PATTERN,
  49.     INVALID_DATE_ERROR_MESSAGE
  50.   ),
  51.   inputDocumentSendBy: Yup.string(),
  52.   stratOfDevelopment: Yup.string().matches(
  53.     DATE_PATTERN,
  54.     INVALID_DATE_ERROR_MESSAGE
  55.   ),
  56.   deliveryInProduction: Yup.string()
  57.     .matches(DATE_PATTERN, INVALID_DATE_ERROR_MESSAGE)
  58.     .test(
  59.       "is after delivery for testing",
  60.       "Date must be after delivery for testing",
  61.       function(val) {
  62.         const { deliveryForTesting } = this.parent;
  63.         if (!deliveryForTesting || !val) {
  64.           return true;
  65.         }
  66.         return Date.parse(val) >= Date.parse(deliveryForTesting);
  67.       }
  68.     ),
  69.   deliveryForTesting: Yup.string()
  70.     .matches(DATE_PATTERN, INVALID_DATE_ERROR_MESSAGE)
  71.     .test(
  72.       "is after start date",
  73.       "Date must be after start of development",
  74.       function(val) {
  75.         const { stratOfDevelopment } = this.parent;
  76.         if (!stratOfDevelopment || !val) {
  77.           return true;
  78.         }
  79.         return Date.parse(val) >= Date.parse(stratOfDevelopment);
  80.       }
  81.     ),
  82.   paymentSchema: Yup.string().oneOf(
  83.     VALID_PAYMENT_SCHEMAS,
  84.     `Payment schema should be one of: ${VALID_PAYMENT_SCHEMAS.join(" ")}`
  85.   ),
  86.   macroEstimationReceivedOn: Yup.string().matches(
  87.     DATE_PATTERN,
  88.     INVALID_DATE_ERROR_MESSAGE
  89.   ),
  90.   macroEstimationSendBy: Yup.string(),
  91.   macroEstimationTitle: Yup.string(),
  92.   requirementsAnalysis: Yup.string(),
  93.   devWorkloadMd: Yup.number()
  94.     .test(
  95.       "is equal to dev workload sum",
  96.       "Should match the sum of dev workload",
  97.       function(val) {
  98.         const { devWorkload } = this.parent;
  99.         return Number(val) === calcWorkloadSum(devWorkload);
  100.       }
  101.     )
  102.     .typeError(PROVIDE_A_VALID_NUMBER),
  103.   workloadDesign: Yup.string()
  104.     .typeError(PROVIDE_A_VALID_NUMBER)
  105.     .matches(VALID_NUMBER_PATTERN, PROVIDE_A_VALID_NUMBER),
  106.   workloadTest: Yup.string()
  107.     .typeError(PROVIDE_A_VALID_NUMBER)
  108.     .matches(VALID_NUMBER_PATTERN, PROVIDE_A_VALID_NUMBER),
  109.   workloadUat: Yup.string()
  110.     .typeError(PROVIDE_A_VALID_NUMBER)
  111.     .matches(VALID_NUMBER_PATTERN, PROVIDE_A_VALID_NUMBER),
  112.   workloadManagement: Yup.string()
  113.     .typeError(PROVIDE_A_VALID_NUMBER)
  114.     .matches(VALID_NUMBER_PATTERN, PROVIDE_A_VALID_NUMBER),
  115.   workloadOther: Yup.string()
  116.     .typeError(PROVIDE_A_VALID_NUMBER)
  117.     .matches(VALID_NUMBER_PATTERN, PROVIDE_A_VALID_NUMBER),
  118.   totalBudget: Yup.string()
  119.     .matches(VALID_NUMBER_PATTERN, PROVIDE_A_VALID_NUMBER)
  120.     .test(
  121.       "is equal to sum of all components",
  122.       "Incorrect Budget value",
  123.       function(val) {
  124.         const {
  125.           devWorkloadMd,
  126.           workloadDesign,
  127.           workloadTest,
  128.           workloadUat,
  129.           workloadManagement,
  130.           workloadOther
  131.         } = this.parent;
  132.  
  133.         const sum =
  134.           Number(devWorkloadMd) +
  135.           Number(workloadDesign) +
  136.           Number(workloadTest) +
  137.           Number(workloadUat) +
  138.           Number(workloadManagement) +
  139.           Number(workloadOther);
  140.  
  141.         if (isNaN(sum)) {
  142.           return true;
  143.         }
  144.  
  145.         return Number(val) === sum;
  146.       }
  147.     )
  148. });
  149.