Repository: freeCodeCamp/freeCodeCamp
File: client/src/components/formHelpers/form.tsx (TypeScript)

Code before:
  errors: ValidationError[];
  invalidValues: (JSX.Element | null)[];
};

const normalizeOptions = {
  stripWWW: false
};

function validateFormValues(
  formValues: FormValues,
  options: FormOptions
): ValidatedValues {
  const {
    isEditorLinkAllowed,
    isLocalLinkAllowed,
    isSourceCodeLinkRequired,
    types
  } = options;
  const validatedValues: ValidatedValues = {
    values: {},
    errors: [],
    invalidValues: []
  };

  const formFields = Object.entries(formValues);

  if (isSourceCodeLinkRequired) {
    const githubLink = formValues['githubLink'];
    if (!githubLink) {
      validatedValues.invalidValues.push(sourceCodeLinkExistsValidator(''));
    }
  }

  const urlValues = formFields.reduce((result, [key, value]) => {
    // NOTE: pathValidator is not used here, because it is only used as a
    // suggestion - should not prevent form submission
    const validators = [fCCValidator, httpValidator];
    const isSolutionLink = key !== 'githubLink';
    if (isSolutionLink && !isEditorLinkAllowed) {
      validators.push(editorValidator);
    }
    if (!isLocalLinkAllowed) {
      validators.push(localhostValidator);
    }
    if (isSourceCodeLinkRequired) {
      validators.push(sourceCodeLinkExistsValidator);
    }

    const nullOrWarning = composeValidators(...validators)(value);
    if (nullOrWarning) {
      validatedValues.invalidValues.push(nullOrWarning);

Review comment (suggestion):
```suggestion
  // We don't always get a githubLink field in formValues, so we can't simply
  // validate that field like the others. We have to handle it separately.
```

Code after:
  errors: ValidationError[];
  invalidValues: (JSX.Element | null)[];
};

const normalizeOptions = {
  stripWWW: false
};

function validateFormValues(
  formValues: FormValues,
  options: FormOptions
): ValidatedValues {
  const {
    isEditorLinkAllowed,
    isLocalLinkAllowed,
    isSourceCodeLinkRequired,
    types
  } = options;
  const validatedValues: ValidatedValues = {
    values: {},
    errors: [],
    invalidValues: []
  };

  const formFields = Object.entries(formValues);
  // We don't always get a githubLink field in formValues, so we can't simply
  // validate that field like the others. We have to handle it separately.
  if (isSourceCodeLinkRequired) {
    const githubLink = formValues['githubLink'];
    if (!githubLink) {
      validatedValues.invalidValues.push(sourceCodeLinkExistsValidator(''));
    }
  }

  const urlValues = formFields.reduce((result, [key, value]) => {
    // NOTE: pathValidator is not used here, because it is only used as a
    // suggestion - should not prevent form submission
    const validators = [fCCValidator, httpValidator];
    const isSolutionLink = key !== 'githubLink';
    if (isSolutionLink && !isEditorLinkAllowed) {
      validators.push(editorValidator);
    }
    if (!isLocalLinkAllowed) {
      validators.push(localhostValidator);
    }
    if (isSourceCodeLinkRequired) {
      validators.push(sourceCodeLinkExistsValidator);
    }

    const nullOrWarning = composeValidators(...validators)(value);
    if (nullOrWarning) {