All files / src/features/experiments/utils experimentValidation.js

100% Statements 63/63
98.36% Branches 60/61
100% Functions 10/10
100% Lines 61/61

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117            33x   33x 2x     33x 2x     33x 2x     33x 2x     33x 2x     33x 2x     33x 2x 31x 1x   30x           30x 2x       33x 2x     33x 2x   31x 63x   31x 1x     31x 61x   31x 3x     31x   23x 31x 1x     31x 54x 26x 54x   26x 1x       31x 52x 25x 52x   25x 52x   25x 52x     25x 1x 24x 1x 23x 1x 22x 1x         33x 2x     33x    
import { getMaxEndDate } from './experimentEndDateLimit';
 
export default function validateExperimentForm(
  formData,
  { totalWeight, userRoles, endDateCapEnabled } = {},
) {
  const errors = {};
 
  if (!formData.env) {
    errors.env = 'Please select an environment';
  }
 
  if (!formData.domainId) {
    errors.domainId = 'Please select a domain';
  }
 
  if (!formData.experimentType) {
    errors.experimentType = 'Please select an experiment type';
  }
 
  if (!formData.name) {
    errors.name = 'Please enter experiment name';
  }
 
  if (!formData.description) {
    errors.description = 'Please enter experiment description';
  }
 
  if (!formData.startDate) {
    errors.startDate = 'Please select start date';
  }
 
  if (!formData.endDate) {
    errors.endDate = 'Please select end date';
  } else if (formData.startDate && formData.endDate < formData.startDate) {
    errors.endDate = 'End Date must be after Start Date';
  } else {
    const maxEnd = getMaxEndDate({
      env: formData.env,
      userRoles,
      recipes: formData.recipes,
      enabled: endDateCapEnabled,
    });
    if (maxEnd && new Date(formData.endDate) > maxEnd) {
      errors.endDate = 'For fully ramped-up production experiments, end date cannot exceed 6 weeks from today.';
    }
  }
 
  if (!formData.targetId) {
    errors.targetId = 'Please select a target';
  }
 
  if (formData.recipes.length === 0) {
    errors.recipes = 'Please add at least one recipe';
  } else {
    const emptyRecipes = formData.recipes.filter(
      (r) => !r.name || !r.name.trim(),
    );
    if (emptyRecipes.length > 0) {
      errors.recipes = 'All recipes must have a name';
    }
 
    const invalidWeights = formData.recipes.some(
      (r) => r.weight < 0 || r.weight > 100,
    );
    if (!errors.recipes && invalidWeights) {
      errors.recipes = 'Each recipe weight must be between 0 and 100';
    }
 
    const total = totalWeight !== undefined
      ? totalWeight
      : formData.recipes.reduce((sum, r) => sum + (r.weight || 0), 0);
    if (!errors.recipes && total !== 100) {
      errors.recipes = `Total recipe weight must equal 100 (current: ${total})`;
    }
 
    if (!errors.recipes) {
      const recipeNames = formData.recipes.map((r) => r.name);
      const duplicateNames = recipeNames.filter(
        (name, index) => recipeNames.indexOf(name) !== index,
      );
      if (duplicateNames.length > 0) {
        errors.recipes = `Recipe names must be unique. Duplicate found: ${duplicateNames[0]}`;
      }
    }
 
    if (!errors.recipes) {
      const recipeTypes = formData.recipes.map((r) => r.recipeType);
      const controlCount = recipeTypes.filter(
        (type) => type === 'Control',
      ).length;
      const controlHoldOutCount = recipeTypes.filter(
        (type) => type === 'ControlHoldOut',
      ).length;
      const treatmentCount = recipeTypes.filter(
        (type) => type === 'Treatment',
      ).length;
 
      if (controlCount > 1) {
        errors.recipes = 'Only one Control recipe is allowed';
      } else if (controlHoldOutCount > 1) {
        errors.recipes = 'Only one Control Hold Out recipe is allowed';
      } else if (controlCount === 0) {
        errors.recipes = 'At least one Control recipe is required';
      } else if (treatmentCount === 0) {
        errors.recipes = 'At least one Treatment recipe is required';
      }
    }
  }
 
  if (!formData.salt) {
    errors.salt = 'Randomization salt is required';
  }
 
  return errors;
}