UNPKG

@mopinion/survey

Version:

Collect customer feedback with the Mopinion survey library

270 lines (269 loc) 13.1 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.validate = validate; var _constants = require("../../../constants"); var _index = require("./index"); var rules = { required: function required(value) { if (typeof value === 'boolean') { return value; } return String(value).trim().length > 0; }, email: function email(value) { return /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/.test(value); }, phone: function phone(value) { return value.length > 9 && /^[\(\)\s\-\+\d]{10,17}$/.test(value); }, max: function max(value, _max) { return value.length <= _max; }, min: function min(value, _min) { return value.length >= _min; } }; function isRequiredContact(_ref) { var _block$properties$ele, _block$properties; var _ref$block = _ref.block, block = _ref$block === void 0 ? {} : _ref$block, datafieldId = _ref.datafieldId; return Object.keys((_block$properties$ele = (_block$properties = block.properties) === null || _block$properties === void 0 ? void 0 : _block$properties.elements) !== null && _block$properties$ele !== void 0 ? _block$properties$ele : {}).some(function (key) { var element = block.properties.elements[key]; if (element.data_field === datafieldId) { return element.required; } if (!element.combine && element.subelements) { var isSubElement = Object.keys(element.subelements).some(function (subKey) { var subElement = element.subelements[subKey]; return subElement.data_field === datafieldId; }); if (isSubElement) { return element.required; } } return false; }); } function isContactEmail(_ref2) { var _block$properties2; var datafieldId = _ref2.datafieldId, _ref2$block = _ref2.block, block = _ref2$block === void 0 ? {} : _ref2$block; return ((_block$properties2 = block.properties) === null || _block$properties2 === void 0 || (_block$properties2 = _block$properties2.elements) === null || _block$properties2 === void 0 || (_block$properties2 = _block$properties2.email) === null || _block$properties2 === void 0 ? void 0 : _block$properties2.data_field) === datafieldId; } function isContactPhone(_ref3) { var _block$properties3, _block$properties4; var datafieldId = _ref3.datafieldId, _ref3$block = _ref3.block, block = _ref3$block === void 0 ? {} : _ref3$block; return ((_block$properties3 = block.properties) === null || _block$properties3 === void 0 || (_block$properties3 = _block$properties3.elements) === null || _block$properties3 === void 0 || (_block$properties3 = _block$properties3.phone) === null || _block$properties3 === void 0 ? void 0 : _block$properties3.data_field) === datafieldId || ((_block$properties4 = block.properties) === null || _block$properties4 === void 0 || (_block$properties4 = _block$properties4.elements) === null || _block$properties4 === void 0 ? void 0 : _block$properties4.phone2) === datafieldId; } function getExtraInputElement() { var _block$properties5, _block$properties6; var block = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; if (!((_block$properties5 = block.properties) !== null && _block$properties5 !== void 0 && _block$properties5.elements_extra)) { return null; } return block.properties.elements_extra[Object.keys((_block$properties6 = block.properties) === null || _block$properties6 === void 0 ? void 0 : _block$properties6.elements_extra).pop()]; } var reasonToErrorMessageKeyMap = { required: 'required', requiredCheckbox: 'required', requiredContact: 'required', requiredMulti: 'required_multi', requiredExtra: 'required', email: 'invalid_email', businessEmail: 'invalid_business_email', phone: 'invalid_phone', min: 'too_short', max: 'too_long', minExtra: 'too_short', maxExtra: 'too_long', minOptions: 'min_options', maxOptions: 'max_options', recaptcha: 'required' }; function validate(_ref4) { var _ref4$block = _ref4.block, block = _ref4$block === void 0 ? {} : _ref4$block, state = _ref4.state, datafieldId = _ref4.datafieldId, _ref4$allowedEmails = _ref4.allowedEmails, allowedEmails = _ref4$allowedEmails === void 0 ? [] : _ref4$allowedEmails; var validators = { required: function required() { var _block$properties7; if (!datafieldId && (_block$properties7 = block.properties) !== null && _block$properties7 !== void 0 && _block$properties7.required && _constants.requireAllBlockTypes.indexOf(block.typeName) === -1) { return rules.required(state.value); } if (block.typeName === 'contact' && isRequiredContact({ block: block, datafieldId: datafieldId })) { return rules.required(state[datafieldId].value); } return true; }, requiredCheckbox: function requiredCheckbox() { var _block$properties8; if (block.typeName === 'checkbox' && (_block$properties8 = block.properties) !== null && _block$properties8 !== void 0 && _block$properties8.required) { return Object.keys(state).filter(function (key) { return (0, _index.isNestedStateProp)(key); }).some(function (key) { return rules.required(state[key].value); }); } return true; }, requiredContact: function requiredContact() { if (block.typeName === 'contact' && isRequiredContact({ block: block, datafieldId: datafieldId })) { return rules.required(state[datafieldId].value); } return true; }, requiredMulti: function requiredMulti() { var _block$properties9; if ((_block$properties9 = block.properties) !== null && _block$properties9 !== void 0 && _block$properties9.required && _constants.requireAllBlockTypes.indexOf(block.typeName) > -1) { var filtered = Object.keys(state).filter(function (key) { return (0, _index.isNestedStateProp)(key); }); return filtered.every(function (key) { return rules.required(state[key].value); }); } return true; }, requiredExtra: function requiredExtra() { var _block$properties0; var extraInputElement = getExtraInputElement(block); if ((_block$properties0 = block.properties) !== null && _block$properties0 !== void 0 && _block$properties0.required && extraInputElement && state[extraInputElement.data_field].isVisible) { return rules.required(state[extraInputElement.data_field].value); } return true; }, email: function email() { var _block$properties1; if (block.typeName === 'email' || ((_block$properties1 = block.properties) === null || _block$properties1 === void 0 ? void 0 : _block$properties1.format) === 'email') { return !state.value || rules.email(state.value); } if (block.typeName === 'contact' && isContactEmail({ block: block, datafieldId: datafieldId })) { return !state[datafieldId].value || rules.email(state[datafieldId].value); } return true; }, businessEmail: function businessEmail() { var _block$properties10, _block$properties11; if ((_block$properties10 = block.properties) !== null && _block$properties10 !== void 0 && _block$properties10.business_only || (_block$properties11 = block.properties) !== null && _block$properties11 !== void 0 && (_block$properties11 = _block$properties11.elements) !== null && _block$properties11 !== void 0 && (_block$properties11 = _block$properties11.email) !== null && _block$properties11 !== void 0 && _block$properties11.business_only && isContactEmail({ block: block, datafieldId: datafieldId })) { var value = datafieldId ? state[datafieldId].value : state.value; return rules.email(value) && allowedEmails.indexOf(value) !== -1; } return true; }, phone: function phone() { var _block$properties12; if (block.typeName === 'phone' || ((_block$properties12 = block.properties) === null || _block$properties12 === void 0 ? void 0 : _block$properties12.format) === 'tel') { return !state.value || rules.phone(state.value); } if (block.typeName === 'contact' && isContactPhone({ block: block, datafieldId: datafieldId })) { return !state[datafieldId].value || rules.phone(state[datafieldId].value); } return true; }, min: function min() { var _block$properties13; if ((_block$properties13 = block.properties) !== null && _block$properties13 !== void 0 && (_block$properties13 = _block$properties13.boundary) !== null && _block$properties13 !== void 0 && _block$properties13.limitMin && _constants.textInputBlocks.indexOf(block.typeName) > -1) { var _block$properties14; return rules.min(state.value, Number((_block$properties14 = block.properties) === null || _block$properties14 === void 0 || (_block$properties14 = _block$properties14.boundary) === null || _block$properties14 === void 0 ? void 0 : _block$properties14.limitMin)); } return true; }, max: function max() { var _block$properties15; if ((_block$properties15 = block.properties) !== null && _block$properties15 !== void 0 && (_block$properties15 = _block$properties15.boundary) !== null && _block$properties15 !== void 0 && _block$properties15.limitMax && _constants.textInputBlocks.indexOf(block.typeName) > -1) { var _block$properties16; return rules.max(state.value, Number((_block$properties16 = block.properties) === null || _block$properties16 === void 0 || (_block$properties16 = _block$properties16.boundary) === null || _block$properties16 === void 0 ? void 0 : _block$properties16.limitMax)); } return true; }, minExtra: function minExtra() { var _block$properties17; var extraInputElement = getExtraInputElement(block); if ((_block$properties17 = block.properties) !== null && _block$properties17 !== void 0 && (_block$properties17 = _block$properties17.boundary) !== null && _block$properties17 !== void 0 && _block$properties17.limitMin && extraInputElement && state[extraInputElement.data_field].isVisible) { var _block$properties18; return rules.min(state[extraInputElement.data_field].value, Number((_block$properties18 = block.properties) === null || _block$properties18 === void 0 || (_block$properties18 = _block$properties18.boundary) === null || _block$properties18 === void 0 ? void 0 : _block$properties18.limitMin)); } return true; }, maxExtra: function maxExtra() { var _block$properties19; var extraInputElement = getExtraInputElement(block); if ((_block$properties19 = block.properties) !== null && _block$properties19 !== void 0 && (_block$properties19 = _block$properties19.boundary) !== null && _block$properties19 !== void 0 && _block$properties19.limitMax && extraInputElement && state[extraInputElement.data_field].isVisible) { var _block$properties20; return rules.max(state[extraInputElement.data_field].value, Number((_block$properties20 = block.properties) === null || _block$properties20 === void 0 || (_block$properties20 = _block$properties20.boundary) === null || _block$properties20 === void 0 ? void 0 : _block$properties20.limitMax)); } return true; }, minOptions: function minOptions() { var _block$properties21; if ((_block$properties21 = block.properties) !== null && _block$properties21 !== void 0 && _block$properties21.minSelected) { var selected = Object.keys(state).filter(function (key) { return (0, _index.isNestedStateProp)(key) && rules.required(state[key].value) && typeof state[key].value === 'boolean'; }); return selected.length >= block.properties.minSelected; } return true; }, maxOptions: function maxOptions() { var _block$properties22; if ((_block$properties22 = block.properties) !== null && _block$properties22 !== void 0 && _block$properties22.maxSelected) { var selected = Object.keys(state).filter(function (key) { return (0, _index.isNestedStateProp)(key) && rules.required(state[key].value) && typeof state[key].value === 'boolean'; }); return selected.length <= block.properties.maxSelected; } return true; }, recaptcha: function recaptcha() { if (state.id === 'recaptcha') { return rules.required(state.value); } return true; } }; if (!state.isVisible) { return { valid: true }; } var valid = true; var reason = null; for (var key in validators) { var validator = validators[key]; if (!validator()) { valid = false; reason = reasonToErrorMessageKeyMap[key]; break; } } return { valid: valid, reason: reason, block: block }; } //# sourceMappingURL=validation.js.map