module.exports.isFormValid = (questions, answers) => {
  const errors = [];
  module.exports.parseTemplate(questions, answers, errors);
  return !(errors.length > 0);
};

module.exports.parseTemplate = (questions, answers, errors) => {
  questions.map((question) => {
    if (question && typeof question === 'object' && question.constructor === Object) {
      if (question.questions) {
        const nestedQuestions = question.questions;
        const nestedAnswers = answers && answers[question.key];
        module.exports.parseTemplate(nestedQuestions, nestedAnswers, errors);
      } else if (question.required && module.exports.hasRequiredError(answers, question.key)) {
        errors.push(true);
      }
    } else if (Array.isArray(question)) {
      module.exports.parseTemplate(question, answers, errors);
    }
    return errors;
  });
};

module.exports.hasRequiredError = (answers, questionKey) => !(answers && answers[questionKey]);
