UNPKG

@indec/form-builder

Version:

Form builder

106 lines (105 loc) 4.64 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports["default"] = void 0; var _questionTypes = _interopRequireDefault(require("../constants/questionTypes")); var _isObject = _interopRequireDefault(require("./isObject")); var _operations = _interopRequireDefault(require("./operations")); function _interopRequireDefault(e) { return e && e.__esModule ? e : { "default": e }; } var getAnswerValue = function getAnswerValue() { var answer = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; return answer.value; }; var getResult = function getResult(condition, answer, type) { var value = getAnswerValue(answer); var parsedValue = typeof value === 'number' ? value : value || ''; if (type === _questionTypes["default"].DATE && !(0, _isObject["default"])(parsedValue)) { parsedValue = { parsedValue: parsedValue }; } return _operations["default"][condition.type](parsedValue, condition.value, type); }; var evaluateConditions = function evaluateConditions(_ref) { var conditions = _ref.conditions, sectionAnswers = _ref.sectionAnswers, initialValues = _ref.initialValues, currentSection = _ref.currentSection, sections = _ref.sections, questionName = _ref.questionName; return conditions.map(function (condition) { var _question, _answers; if (!sections) { return false; } var section = sections.find(function (_ref2) { var name = _ref2.name; return name === condition.section; }); if (!section) { return false; } var question = section.questions.find(function (_ref3) { var name = _ref3.name; return name === questionName; }); if (condition.subQuestion) { question = question.subQuestions.find(function (_ref4) { var name = _ref4.name; return name === condition.subQuestion; }); } var type = (_question = question) === null || _question === void 0 ? void 0 : _question.type; var isMatchingSection = condition.section === currentSection.name && sectionAnswers; var answers; if (isMatchingSection) { var _sectionAnswers$condi; answers = condition.subQuestion ? (_sectionAnswers$condi = sectionAnswers[condition.question]) === null || _sectionAnswers$condi === void 0 ? void 0 : _sectionAnswers$condi.answer.specifications[condition.subQuestion] : sectionAnswers[condition.question]; } else { answers = ((initialValues === null || initialValues === void 0 ? void 0 : initialValues[condition.section]) || []).map(function (initialValue) { var _initialValue$conditi; return condition.subQuestion ? initialValue === null || initialValue === void 0 || (_initialValue$conditi = initialValue[condition.question]) === null || _initialValue$conditi === void 0 || (_initialValue$conditi = _initialValue$conditi.answer) === null || _initialValue$conditi === void 0 ? void 0 : _initialValue$conditi.specifications[condition.subQuestion] : initialValue === null || initialValue === void 0 ? void 0 : initialValue[condition.question]; }); } if (Array.isArray(answers) || Array.isArray((_answers = answers) === null || _answers === void 0 ? void 0 : _answers.answer)) { var _answers2; var parsedAnswers = Array.isArray(answers) ? answers : (_answers2 = answers) === null || _answers2 === void 0 ? void 0 : _answers2.answer.flatMap(function (currentAnswer) { return { answer: currentAnswer }; }); return parsedAnswers.some(function (answer) { if (Array.isArray(answer === null || answer === void 0 ? void 0 : answer.answer)) { return answer.answer.some(function (currentAnswer) { return getResult(condition, currentAnswer, type); }); } return getResult(condition, answer.answer, type); }); } return getResult(condition, answers.answer, type); }); }; var getValidationRules = function getValidationRules(_ref5) { var validation = _ref5.validation, answers = _ref5.answers, initialValues = _ref5.initialValues, section = _ref5.section, sections = _ref5.sections, questionName = _ref5.questionName; return validation.rules.map(function (rule) { var conditions = evaluateConditions({ conditions: rule.conditions, sectionAnswers: answers, initialValues: initialValues, currentSection: section, sections: sections, questionName: questionName }); return conditions.every(function (condition) { return condition; }); }); }; var _default = exports["default"] = getValidationRules;