{"size": 1298, "ext": "js", "max_stars_repo_path": "client/app/bundles/course/assessment/submission/pages/SubmissionEditIndex/submissionFormValidate.js", "max_stars_repo_name": "my-references/coursemology2", "max_stars_repo_stars_event_min_datetime": "2015-01-14T04:31:12.000Z", "max_stars_repo_stars_event_max_datetime": "2022-03-25T09:14:44.000Z", "max_issues_repo_path": "client/app/bundles/course/assessment/submission/pages/SubmissionEditIndex/submissionFormValidate.js", "max_issues_repo_name": "my-references/coursemology2", "max_issues_repo_issues_event_min_datetime": "2015-01-03T01:33:29.000Z", "max_issues_repo_issues_event_max_datetime": "2022-03-31T18:01:50.000Z", "max_forks_repo_path": "client/app/bundles/course/assessment/submission/pages/SubmissionEditIndex/submissionFormValidate.js", "max_forks_repo_name": "lyskevin/coursemology2", "max_forks_repo_forks_event_min_datetime": "2015-03-05T09:01:02.000Z", "max_forks_repo_forks_event_max_datetime": "2022-01-30T11:45:27.000Z"}
{"max_stars_count": 123, "max_issues_count": 2705, "max_forks_count": 87, "avg_line_length": 27.0416666667, "max_line_length": 110, "alphanum_fraction": 0.6787365177}
import { defineMessages } from 'react-intl';
import { questionTypes } from '../../constants';

const translations = defineMessages({
  pleaseRecordYourVoice: {
    id: 'course.assessment.submission.pages.SubmissionEditIndex.submissionFormValidate.pleaseRecordYourVoice',
    defaultMessage: 'Please record your voice',
  },
});

const validateDefaultQuestion = () => {};

const validateVoiceResponse = (values) => {
  const { file: fileValue } = values;
  const { file, url } = fileValue;
  if (url || file instanceof File) {
    return undefined;
  }
  return { file: translations.pleaseRecordYourVoice };
};

const validateQuestion = (question) => (values) => {
  const { VoiceResponse } = questionTypes;
  switch (question.type) {
    case VoiceResponse:
      return validateVoiceResponse(values);
    default:
      return validateDefaultQuestion(values);
  }
};

const submissionFormValidate = (values, props) => {
  const { questions } = props;
  const errors = {};
  Object.keys(values || {}).forEach((key) => {
    const value = values[key];
    const { questionId } = value;
    const question = questions[questionId];
    const error = validateQuestion(question)(value);
    if (error) {
      errors[key] = error;
    }
  });
  return errors;
};

export default submissionFormValidate;
