UNPKG

plivo

Version:

A Node.js SDK to make voice calls and send SMS using Plivo and to generate Plivo XML

130 lines (110 loc) 3.8 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); exports.validate = exports.extend = undefined; var _template = require('../utils/template.js'); var _interactive = require('../utils/interactive.js'); var _location = require('../utils/location.js'); var extend = exports.extend = function extend(instance, data) { data = data || {}; for (var key in data) { if (Object.prototype.hasOwnProperty.call(data, key)) { instance[key] = data[key]; } } }; var validate = exports.validate = function () { var Validators = {}; Validators.isDataType = function () { var regExs = { String: /String/, Number: /Number/, Object: /Object/, Boolean: /Boolean/, Array: /Array/ }; return function (object, type) { return regExs[type].test(Object.prototype.toString.call(object)); }; }(); // Validators.isEmpty = field => { // if (Validators.isDataType(field, 'String')) { // return !field.length; // } // return true; // }; Validators.isRequired = function (field) { return field === null || field === undefined || field === ''; }; Validators.isTemplate = function (field) { var _validateTemplate = (0, _template.validateTemplate)(field), error = _validateTemplate.error, value = _validateTemplate.value; return { error: error, value: value }; }; Validators.isInteractive = function (field) { var _validateInteractive = (0, _interactive.validateInteractive)(field), error = _validateInteractive.error, value = _validateInteractive.value; return { error: error, value: value }; }; Validators.isLocation = function (field) { var _validateLocation = (0, _location.validateLocation)(field), error = _validateLocation.error, value = _validateLocation.value; return { error: error, value: value }; }; return function () { var data = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : []; var errorText = []; data.forEach(function (item) { item.validators.forEach(function (validator) { switch (validator) { case 'isRequired': if (Validators.isRequired(item.value)) { errorText.push('Missing mandatory field: ' + item.field); } break; // case 'isObject': // if (!Validators.isDataType(item.value, 'Object')) { // errorText.push(item.field + ' should be object.'); // } // break; case 'isString': if (!Validators.isDataType(item.value, 'String')) { errorText.push(item.field + ' should be string.'); } break; case 'isTemplate': var _Validators$isTemplat = Validators.isTemplate(item.value), error = _Validators$isTemplat.error, value = _Validators$isTemplat.value; if (error) { error.details.forEach(function (validationError, _) { errorText.push('' + validationError.message); }); break; } case 'isInteractive': var _Validators$isInterac = Validators.isInteractive(item.value), err = _Validators$isInterac.err, val = _Validators$isInterac.val; if (err) { err.details.forEach(function (validationError, _) { errorText.push('' + validationError.message); }); break; } default: } }); }); if (errorText.length) { return new Promise(function (resolve, reject) { reject(new Error(errorText.join(', '))); }); } return false; }; }();