validate-promise
Advanced tools
Comparing version 2.2.3 to 2.3.0
@@ -1,732 +0,2 @@ | ||
(function webpackUniversalModuleDefinition(root, factory) { | ||
if(typeof exports === 'object' && typeof module === 'object') | ||
module.exports = factory(); | ||
else if(typeof define === 'function' && define.amd) | ||
define("validate-promise", [], factory); | ||
else if(typeof exports === 'object') | ||
exports["validate-promise"] = factory(); | ||
else | ||
root["validate-promise"] = factory(); | ||
})(typeof self !== 'undefined' ? self : this, function() { | ||
return /******/ (function(modules) { // webpackBootstrap | ||
/******/ // The module cache | ||
/******/ var installedModules = {}; | ||
/******/ | ||
/******/ // The require function | ||
/******/ function __webpack_require__(moduleId) { | ||
/******/ | ||
/******/ // Check if module is in cache | ||
/******/ if(installedModules[moduleId]) { | ||
/******/ return installedModules[moduleId].exports; | ||
/******/ } | ||
/******/ // Create a new module (and put it into the cache) | ||
/******/ var module = installedModules[moduleId] = { | ||
/******/ i: moduleId, | ||
/******/ l: false, | ||
/******/ exports: {} | ||
/******/ }; | ||
/******/ | ||
/******/ // Execute the module function | ||
/******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__); | ||
/******/ | ||
/******/ // Flag the module as loaded | ||
/******/ module.l = true; | ||
/******/ | ||
/******/ // Return the exports of the module | ||
/******/ return module.exports; | ||
/******/ } | ||
/******/ | ||
/******/ | ||
/******/ // expose the modules object (__webpack_modules__) | ||
/******/ __webpack_require__.m = modules; | ||
/******/ | ||
/******/ // expose the module cache | ||
/******/ __webpack_require__.c = installedModules; | ||
/******/ | ||
/******/ // define getter function for harmony exports | ||
/******/ __webpack_require__.d = function(exports, name, getter) { | ||
/******/ if(!__webpack_require__.o(exports, name)) { | ||
/******/ Object.defineProperty(exports, name, { | ||
/******/ configurable: false, | ||
/******/ enumerable: true, | ||
/******/ get: getter | ||
/******/ }); | ||
/******/ } | ||
/******/ }; | ||
/******/ | ||
/******/ // getDefaultExport function for compatibility with non-harmony modules | ||
/******/ __webpack_require__.n = function(module) { | ||
/******/ var getter = module && module.__esModule ? | ||
/******/ function getDefault() { return module['default']; } : | ||
/******/ function getModuleExports() { return module; }; | ||
/******/ __webpack_require__.d(getter, 'a', getter); | ||
/******/ return getter; | ||
/******/ }; | ||
/******/ | ||
/******/ // Object.prototype.hasOwnProperty.call | ||
/******/ __webpack_require__.o = function(object, property) { return Object.prototype.hasOwnProperty.call(object, property); }; | ||
/******/ | ||
/******/ // __webpack_public_path__ | ||
/******/ __webpack_require__.p = "/"; | ||
/******/ | ||
/******/ // Load entry module and return exports | ||
/******/ return __webpack_require__(__webpack_require__.s = 0); | ||
/******/ }) | ||
/************************************************************************/ | ||
/******/ ([ | ||
/* 0 */ | ||
/***/ (function(module, exports, __webpack_require__) { | ||
module.exports = __webpack_require__(1); | ||
/***/ }), | ||
/* 1 */ | ||
/***/ (function(module, exports, __webpack_require__) { | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
exports.whitelist = exports.required = exports.regex = exports.notEquals = exports.lessthan = exports.int = exports.greaterthan = exports.float = exports.equalto = exports.equals = exports.email = exports.blacklist = exports.before = exports.after = undefined; | ||
var _after = __webpack_require__(2); | ||
var _after2 = _interopRequireDefault(_after); | ||
var _before = __webpack_require__(3); | ||
var _before2 = _interopRequireDefault(_before); | ||
var _blacklist = __webpack_require__(4); | ||
var _blacklist2 = _interopRequireDefault(_blacklist); | ||
var _email = __webpack_require__(5); | ||
var _email2 = _interopRequireDefault(_email); | ||
var _equals = __webpack_require__(7); | ||
var _equals2 = _interopRequireDefault(_equals); | ||
var _equalto = __webpack_require__(8); | ||
var _equalto2 = _interopRequireDefault(_equalto); | ||
var _float = __webpack_require__(9); | ||
var _float2 = _interopRequireDefault(_float); | ||
var _greaterthan = __webpack_require__(10); | ||
var _greaterthan2 = _interopRequireDefault(_greaterthan); | ||
var _int = __webpack_require__(11); | ||
var _int2 = _interopRequireDefault(_int); | ||
var _lessthan = __webpack_require__(12); | ||
var _lessthan2 = _interopRequireDefault(_lessthan); | ||
var _notEquals = __webpack_require__(13); | ||
var _notEquals2 = _interopRequireDefault(_notEquals); | ||
var _regex = __webpack_require__(14); | ||
var _regex2 = _interopRequireDefault(_regex); | ||
var _required = __webpack_require__(15); | ||
var _required2 = _interopRequireDefault(_required); | ||
var _whitelist = __webpack_require__(16); | ||
var _whitelist2 = _interopRequireDefault(_whitelist); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
/** | ||
* Iterates over an array of promises, unline Promise.all it will not | ||
* stop when one promise is rejected. Instead all promises are run and an | ||
* array of objects describing the promise resolution is returned | ||
*/ | ||
var hashSettled = function hashSettled(promises) { | ||
var keys = Object.keys(promises); | ||
return Promise.all(keys.map(function (k) { | ||
return Promise.resolve(promises[k]).then(function (value) { | ||
var r = { | ||
state: 'fulfilled', | ||
key: k, | ||
value: value | ||
}; | ||
return r; | ||
}, function (reason) { | ||
var r = { | ||
state: 'rejected', | ||
key: k, | ||
reason: reason | ||
}; | ||
return r; | ||
}); | ||
})); | ||
}, | ||
/** | ||
* Validate data againsts fields | ||
* @param {Array} contract Validation rules | ||
* @param {Object} data Form data | ||
* @return {Object|Boolean} true if passed, error object if failed, | ||
* array error messages keyed on field.name | ||
*/ | ||
validate = function validate(contract, data) { | ||
var promises = {}; | ||
contract.forEach(function (validation, cx) { | ||
var name = validation.key, | ||
value = data[name]; | ||
validation.promises.forEach(function (p, i) { | ||
var key = name + '.' + cx + '.' + i, | ||
thisArg = p.arg === undefined ? null : p.arg, | ||
validationMessage = p.msg || validation.msg; | ||
promises[key] = p.rule(value, data, validationMessage, thisArg); | ||
}); | ||
}); | ||
return new Promise(function (resolve, reject) { | ||
hashSettled(promises).then(function (res) { | ||
var rejectedErrors = function rejectedErrors(r) { | ||
return r.state === 'rejected'; | ||
}; | ||
var errors = res.filter(rejectedErrors), | ||
ret = {}; | ||
errors.forEach(function (err) { | ||
var k = err.key.split('.').shift(); | ||
if (!ret[k]) { | ||
ret[k] = []; | ||
} | ||
if (ret[k].indexOf(err.reason) === -1) { | ||
ret[k].push(err.reason); | ||
} | ||
}); | ||
if (errors.length === 0) { | ||
resolve(true); | ||
} | ||
reject(ret); | ||
}); | ||
}); | ||
}; | ||
exports.default = validate; | ||
exports.after = _after2.default; | ||
exports.before = _before2.default; | ||
exports.blacklist = _blacklist2.default; | ||
exports.email = _email2.default; | ||
exports.equals = _equals2.default; | ||
exports.equalto = _equalto2.default; | ||
exports.float = _float2.default; | ||
exports.greaterthan = _greaterthan2.default; | ||
exports.int = _int2.default; | ||
exports.lessthan = _lessthan2.default; | ||
exports.notEquals = _notEquals2.default; | ||
exports.regex = _regex2.default; | ||
exports.required = _required2.default; | ||
exports.whitelist = _whitelist2.default; | ||
/***/ }), | ||
/* 2 */ | ||
/***/ (function(module, exports, __webpack_require__) { | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
/** | ||
* Check if a value is after a given date | ||
* @param {String} value To validate | ||
* @param {Object} row Form data | ||
* @param {Function} msg Error message function | ||
* @param {*} arg Validation arguement | ||
* @return {Promise} . | ||
*/ | ||
exports.default = function (value, row, msg, arg) { | ||
var test = Date.parse(value); | ||
if (typeof arg === 'function') { | ||
arg = arg(value, row); | ||
} | ||
var compare = Date.parse(arg); | ||
if (test > compare) { | ||
return Promise.resolve(); | ||
} | ||
return Promise.reject(msg(value, row, arg)); | ||
}; | ||
/***/ }), | ||
/* 3 */ | ||
/***/ (function(module, exports, __webpack_require__) { | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
/** | ||
* Check if a value is before a given date | ||
* @param {String} value To validate | ||
* @param {Object} row Form data | ||
* @param {Function} msg Error message function | ||
* @param {*} arg Validation arguement | ||
* @return {Promise} . | ||
*/ | ||
exports.default = function (value, row, msg, arg) { | ||
var test = Date.parse(value); | ||
if (typeof arg === 'function') { | ||
arg = arg(value, row); | ||
} | ||
var compare = Date.parse(arg); | ||
if (test < compare) { | ||
return Promise.resolve(); | ||
} | ||
return Promise.reject(msg(value, row, arg)); | ||
}; | ||
/***/ }), | ||
/* 4 */ | ||
/***/ (function(module, exports, __webpack_require__) { | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
/** | ||
* Check if a value is in a blacklist | ||
* @param {String} value To validate | ||
* @param {Object} row Form data | ||
* @param {Function} msg Error message function | ||
* @param {array} arg Validation arguement | ||
* @return {Promise} . | ||
*/ | ||
exports.default = function (value, row, msg, arg) { | ||
if (typeof arg === 'function') { | ||
arg = arg(value, row); | ||
} | ||
if (arg.indexOf(value) === -1) { | ||
return Promise.resolve(); | ||
} | ||
return Promise.reject(msg(value, row, arg)); | ||
}; | ||
/***/ }), | ||
/* 5 */ | ||
/***/ (function(module, exports, __webpack_require__) { | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
var _isEmail = __webpack_require__(6); | ||
var _isEmail2 = _interopRequireDefault(_isEmail); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
/** | ||
* Check if a value is an email | ||
* @param {String} value To validate | ||
* @param {Object} row Form data | ||
* @param {Function} msg Error message function | ||
* @param {*} arg Validation arguement | ||
* @return {Promise} . | ||
*/ | ||
exports.default = function (value, row, msg, arg) { | ||
if ((0, _isEmail2.default)(value)) { | ||
return Promise.resolve(); | ||
}; | ||
return Promise.reject(msg(value, row, arg)); | ||
}; | ||
/***/ }), | ||
/* 6 */ | ||
/***/ (function(module, exports, __webpack_require__) { | ||
"use strict"; | ||
// Email address matcher. | ||
var matcher = /.+\@.+\..+/; | ||
/** | ||
* Loosely validate an email address. | ||
* | ||
* @param {string} string | ||
* @return {boolean} | ||
*/ | ||
function isEmail(string) { | ||
return matcher.test(string); | ||
} | ||
/* | ||
* Exports. | ||
*/ | ||
module.exports = isEmail; | ||
/***/ }), | ||
/* 7 */ | ||
/***/ (function(module, exports, __webpack_require__) { | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
/** | ||
* Check if a value can be coerced to an integer | ||
* @param {String} value To validate | ||
* @param {Object} row Form data | ||
* @param {Function} msg Error message function | ||
* @param {*} arg Validation arguement | ||
* @return {Promise} . | ||
*/ | ||
exports.default = function (value, row, msg, arg) { | ||
if (typeof arg === 'function') { | ||
arg = arg(value, row); | ||
} | ||
if (value == arg) { | ||
return Promise.resolve(); | ||
} | ||
return Promise.reject(msg(value, row, arg)); | ||
}; | ||
/***/ }), | ||
/* 8 */ | ||
/***/ (function(module, exports, __webpack_require__) { | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
/** | ||
* Check if a value matches another fields value | ||
* @param {String} value To validate | ||
* @param {Object} row Form data | ||
* @param {Function} msg Error message function | ||
* @param {*} arg Validation arguement | ||
* @return {Promise} . | ||
*/ | ||
exports.default = function (value, row, msg, arg) { | ||
if (typeof arg === 'function') { | ||
arg = arg(value, row); | ||
} | ||
if (value === arg) { | ||
return Promise.resolve(); | ||
} | ||
return Promise.reject(msg(value, row, arg)); | ||
}; | ||
/***/ }), | ||
/* 9 */ | ||
/***/ (function(module, exports, __webpack_require__) { | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
/** | ||
* Check if a value can be coerced to an integer | ||
* @param {String} value To validate | ||
* @param {Object} row Form data | ||
* @param {Function} msg Error message function | ||
* @param {*} arg Validation arguement | ||
* @return {Promise} . | ||
*/ | ||
exports.default = function (value, row, msg, arg) { | ||
if (typeof arg === 'function') { | ||
arg = arg(value, row); | ||
} | ||
var float = /^(?:[-+]?(?:[0-9]+))?(?:\.[0-9]*)?(?:[eE][\+\-]?(?:[0-9]+))?$/; | ||
if (arg !== null) { | ||
if (arg.min !== undefined && parseInt(value, 10) < parseInt(arg.min, 10)) { | ||
return Promise.reject(msg(value, row, arg)); | ||
} | ||
if (arg.max && parseInt(value, 10) > parseInt(arg.max, 10)) { | ||
return Promise.reject(msg(value, row, arg)); | ||
} | ||
} | ||
if (float.test(value)) { | ||
return Promise.resolve(); | ||
} | ||
return Promise.reject(msg(value, row, arg)); | ||
}; | ||
/***/ }), | ||
/* 10 */ | ||
/***/ (function(module, exports, __webpack_require__) { | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; | ||
/** | ||
* Check if a value is greater than foo | ||
* @param {String} value To validate | ||
* @param {Object} row Form data | ||
* @param {Function} msg Error message function | ||
* @param {*} arg Validation arguement | ||
* @return {Promise} . | ||
*/ | ||
exports.default = function (value, row, msg, arg) { | ||
var compare = arg; | ||
if (typeof arg === 'function') { | ||
compare = arg(value, row); | ||
} | ||
if ((typeof compare === 'undefined' ? 'undefined' : _typeof(compare)) === 'object') { | ||
value = compare.value; | ||
compare = compare.compare; | ||
} | ||
if (parseInt(value, 10) > parseInt(compare, 10)) { | ||
return Promise.resolve(); | ||
} | ||
return Promise.reject(msg(value, row, arg)); | ||
}; | ||
/***/ }), | ||
/* 11 */ | ||
/***/ (function(module, exports, __webpack_require__) { | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
/** | ||
* Check if a value can be coerced to an integer | ||
* @param {String} value To validate | ||
* @param {Object} row Form data | ||
* @param {Function} msg Error message function | ||
* @param {*} arg Validation arguement | ||
* @return {Promise} . | ||
*/ | ||
exports.default = function (value, row, msg, arg) { | ||
if (typeof arg === 'function') { | ||
arg = arg(value, row); | ||
} | ||
var int = /^(?:[-+]?(?:0|[1-9][0-9]*))$/; | ||
if (arg !== null) { | ||
if (arg.min !== undefined && parseInt(value, 10) < parseInt(arg.min, 10)) { | ||
return Promise.reject(msg(value, row, arg)); | ||
} | ||
if (arg.max && parseInt(value, 10) > parseInt(arg.max, 10)) { | ||
return Promise.reject(msg(value, row, arg)); | ||
} | ||
} | ||
if (int.test(value)) { | ||
return Promise.resolve(); | ||
} | ||
return Promise.reject(msg(value, row, arg)); | ||
}; | ||
/***/ }), | ||
/* 12 */ | ||
/***/ (function(module, exports, __webpack_require__) { | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; | ||
/** | ||
* Check if a value is less than foo | ||
* @param {String} value To validate | ||
* @param {Object} row Form data | ||
* @param {Function} msg Error message function | ||
* @param {*} arg Validation arguement | ||
* @return {Promise} . | ||
*/ | ||
exports.default = function (value, row, msg, arg) { | ||
var compare = arg; | ||
if (typeof arg === 'function') { | ||
compare = arg(value, row); | ||
} | ||
if ((typeof compare === 'undefined' ? 'undefined' : _typeof(compare)) === 'object') { | ||
value = compare.value; | ||
compare = compare.compare; | ||
} | ||
if (parseInt(value, 10) < parseInt(compare, 10)) { | ||
return Promise.resolve(); | ||
} | ||
return Promise.reject(msg(value, row, arg)); | ||
}; | ||
/***/ }), | ||
/* 13 */ | ||
/***/ (function(module, exports, __webpack_require__) { | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
/** | ||
* Check if a value can be coerced to an integer | ||
* @param {String} value To validate | ||
* @param {Object} row Form data | ||
* @param {Function} msg Error message function | ||
* @param {*} arg Validation arguement | ||
* @return {Promise} . | ||
*/ | ||
exports.default = function (value, row, msg, arg) { | ||
if (typeof arg === 'function') { | ||
arg = arg(value, row); | ||
} | ||
if (value != arg) { | ||
return Promise.resolve(); | ||
} | ||
return Promise.reject(msg(value, row, arg)); | ||
}; | ||
/***/ }), | ||
/* 14 */ | ||
/***/ (function(module, exports, __webpack_require__) { | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
/** | ||
* Check if a value matches a given regex | ||
* @param {String} value Regex to match | ||
* @param {Object} row Form data | ||
* @param {Function} msg Error message function | ||
* @param {*} arg Validation arguement | ||
* @return {Promise} . | ||
*/ | ||
exports.default = function (value, row, msg, arg) { | ||
var test = typeof arg === 'function' ? arg(value, row) : arg; | ||
var regex = new RegExp(test, 'g'); | ||
if (regex.test(value)) { | ||
return Promise.resolve(); | ||
} | ||
return Promise.reject(msg(value, row, arg)); | ||
}; | ||
/***/ }), | ||
/* 15 */ | ||
/***/ (function(module, exports, __webpack_require__) { | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
/** | ||
* Check if a value is greater than foo | ||
* @param {String} value To validate | ||
* @param {Object} row Form data | ||
* @param {Function} msg Error message function | ||
* @param {*} args Field arguements | ||
* @return {Promise} . | ||
*/ | ||
exports.default = function (value, row, msg) { | ||
return new Promise(function (resolve, reject) { | ||
if (value !== '' && value !== undefined) { | ||
return resolve(); | ||
} | ||
return reject(msg(value, row)); | ||
}); | ||
}; | ||
/***/ }), | ||
/* 16 */ | ||
/***/ (function(module, exports, __webpack_require__) { | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
/** | ||
* Check if a value is in a whitelist | ||
* @param {String} value To validate | ||
* @param {Object} row Form data | ||
* @param {Function} msg Error message function | ||
* @param {array} arg Validation arguement | ||
* @return {Promise} . | ||
*/ | ||
exports.default = function (value, row, msg, arg) { | ||
if (typeof arg === 'function') { | ||
arg = arg(value, row); | ||
} | ||
if (arg.indexOf(value) !== -1) { | ||
return Promise.resolve(); | ||
} | ||
return Promise.reject(msg(value, row, arg)); | ||
}; | ||
/***/ }) | ||
/******/ ]); | ||
}); | ||
!function(e,r){"object"==typeof exports&&"object"==typeof module?module.exports=r():"function"==typeof define&&define.amd?define("validate-promise",[],r):"object"==typeof exports?exports["validate-promise"]=r():e["validate-promise"]=r()}(window,function(){return function(e){var r={};function t(n){if(r[n])return r[n].exports;var o=r[n]={i:n,l:!1,exports:{}};return e[n].call(o.exports,o,o.exports,t),o.l=!0,o.exports}return t.m=e,t.c=r,t.d=function(e,r,n){t.o(e,r)||Object.defineProperty(e,r,{enumerable:!0,get:n})},t.r=function(e){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})},t.t=function(e,r){if(1&r&&(e=t(e)),8&r)return e;if(4&r&&"object"==typeof e&&e&&e.__esModule)return e;var n=Object.create(null);if(t.r(n),Object.defineProperty(n,"default",{enumerable:!0,value:e}),2&r&&"string"!=typeof e)for(var o in e)t.d(n,o,function(r){return e[r]}.bind(null,o));return n},t.n=function(e){var r=e&&e.__esModule?function(){return e.default}:function(){return e};return t.d(r,"a",r),r},t.o=function(e,r){return Object.prototype.hasOwnProperty.call(e,r)},t.p="/",t(t.s=1)}([function(e,r,t){"use strict";var n=/.+\@.+\..+/;e.exports=function(e){return n.test(e)}},function(e,r,t){e.exports=t(2)},function(e,r,t){"use strict";t.r(r);var n=function(e,r,t,n){var o=Date.parse(e);return"function"==typeof n&&(n=n(e,r)),o>Date.parse(n)?Promise.resolve():Promise.reject(t(e,r,n))},o=function(e,r,t,n){var o=Date.parse(e);return"function"==typeof n&&(n=n(e,r)),o<Date.parse(n)?Promise.resolve():Promise.reject(t(e,r,n))},u=function(e,r,t,n){return"function"==typeof n&&(n=n(e,r)),-1===n.indexOf(e)?Promise.resolve():Promise.reject(t(e,r,n))},i=t(0),f=t.n(i),c=function(e,r,t,n){return f()(e)?Promise.resolve():Promise.reject(t(e,r,n))},s=function(e,r,t,n){return"function"==typeof n&&(n=n(e,r)),e==n?Promise.resolve():Promise.reject(t(e,r,n))},a=function(e,r,t,n){return"function"==typeof n&&(n=n(e,r)),e===n?Promise.resolve():Promise.reject(t(e,r,n))},p=function(e,r,t,n){"function"==typeof n&&(n=n(e,r));if(null!==n){if(void 0!==n.min&&parseInt(e,10)<parseInt(n.min,10))return Promise.reject(t(e,r,n));if(n.max&&parseInt(e,10)>parseInt(n.max,10))return Promise.reject(t(e,r,n))}return/^(?:[-+]?(?:[0-9]+))?(?:\.[0-9]*)?(?:[eE][\+\-]?(?:[0-9]+))?$/.test(e)?Promise.resolve():Promise.reject(t(e,r,n))};function l(e){return(l="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol&&e!==Symbol.prototype?"symbol":typeof e})(e)}var m=function(e,r,t,n){var o=n;return"function"==typeof n&&(o=n(e,r)),"object"===l(o)&&(e=o.value,o=o.compare),parseInt(e,10)>parseInt(o,10)?Promise.resolve():Promise.reject(t(e,r,n))},y=function(e,r,t,n){"function"==typeof n&&(n=n(e,r));if(null!==n){if(void 0!==n.min&&parseInt(e,10)<parseInt(n.min,10))return Promise.reject(t(e,r,n));if(n.max&&parseInt(e,10)>parseInt(n.max,10))return Promise.reject(t(e,r,n))}return/^(?:[-+]?(?:0|[1-9][0-9]*))$/.test(e)?Promise.resolve():Promise.reject(t(e,r,n))};function d(e){return(d="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol&&e!==Symbol.prototype?"symbol":typeof e})(e)}var v=function(e,r,t,n){var o=n;return"function"==typeof n&&(o=n(e,r)),"object"===d(o)&&(e=o.value,o=o.compare),parseInt(e,10)<parseInt(o,10)?Promise.resolve():Promise.reject(t(e,r,n))},P=function(e,r,t,n){return"function"==typeof n&&(n=n(e,r)),e!=n?Promise.resolve():Promise.reject(t(e,r,n))},b=function(e,r,t,n){var o="function"==typeof n?n(e,r):n;return new RegExp(o,"g").test(e)?Promise.resolve():Promise.reject(t(e,r,n))},j=function(e,r,t){return new Promise(function(n,o){return""!==e&&void 0!==e?n():o(t(e,r))})},x=function(e,r,t,n){return"function"==typeof n&&(n=n(e,r)),-1!==n.indexOf(e)?Promise.resolve():Promise.reject(t(e,r,n))};t.d(r,"after",function(){return n}),t.d(r,"before",function(){return o}),t.d(r,"blacklist",function(){return u}),t.d(r,"email",function(){return c}),t.d(r,"equals",function(){return s}),t.d(r,"equalto",function(){return a}),t.d(r,"float",function(){return p}),t.d(r,"greaterthan",function(){return m}),t.d(r,"int",function(){return y}),t.d(r,"lessthan",function(){return v}),t.d(r,"notEquals",function(){return P}),t.d(r,"regex",function(){return b}),t.d(r,"required",function(){return j}),t.d(r,"whitelist",function(){return x});r.default=function(e,r){var t=[];return e.forEach(function(e,n){var o=Array.isArray(e.key)?e.key:[e.key],u=Array.isArray(o)?o.reduce(function(e,r){return e[r]},r):r[o];t=t.concat(e.promises.map(function(t){return{propPath:o,rule:t.rule(u,r,t.msg||e.msg,void 0===t.arg?null:t.arg)}}))}),new Promise(function(e,r){(function(e){return Promise.all(e.map(function(e){var r=e.propPath,t=e.rule;return Promise.resolve(t).then(function(e){return{state:"fulfilled",propPath:r,value:e}},function(e){return{state:"rejected",propPath:r,reason:e}})}))})(t).then(function(t){var n=t.filter(function(e){return"rejected"===e.state}),o={};n.forEach(function(e){var r=e.propPath,t=e.reason;o=function(e,r,t){return r.reduce(function(e,n,o){return o===r.length-1?e[n]?e[n].push(t):e[n]=[t]:e[n]||(e[n]={}),e[n]},e),e}(o,r,t)}),0===n.length&&e(!0),r(o)})})}}])}); | ||
//# sourceMappingURL=index.js.map |
{ | ||
"name": "validate-promise", | ||
"version": "2.2.3", | ||
"version": "2.3.0", | ||
"description": "Promised based validation library", | ||
@@ -9,5 +9,4 @@ "main": "dist/index.js", | ||
"flow": "flow; test $? -eq 0 -o $? -eq 2", | ||
"test": "snyk test && istanbul cover _mocha --recursive -x '**/*.spec.js' -x '**/config/**' -x '**/node_modules/**' -g './{,!(node_modules)/**/}*.spec.js'", | ||
"snyk-protect": "snyk protect", | ||
"prepublish": "npm run snyk-protect" | ||
"test": "nyc mocha", | ||
"prepublish": "npm run build" | ||
}, | ||
@@ -20,22 +19,17 @@ "keywords": [ | ||
"devDependencies": { | ||
"babel-core": "^6.26.0", | ||
"babel-eslint": "^8.2.2", | ||
"babel-loader": "^7.1.4", | ||
"babel-plugin-transform-flow-strip-types": "^6.22.0", | ||
"babel-preset-es2015": "^6.24.1", | ||
"chai": "^4.1.2", | ||
"eslint-plugin-flowtype": "^2.46.1", | ||
"flow-bin": "^0.67.1", | ||
"istanbul": "^0.4.5", | ||
"mocha": "^5.0.4", | ||
"webpack": "^4.1.1" | ||
"@babel/core": "^7.2.0", | ||
"@babel/plugin-transform-flow-strip-types": "^7.2.0", | ||
"@babel/preset-env": "^7.2.0", | ||
"@babel/register": "^7.0.0", | ||
"babel-eslint": "^10.0.1", | ||
"babel-loader": "^8.0.4", | ||
"chai": "^4.2.0", | ||
"eslint-plugin-flowtype": "^3.2.0", | ||
"flow-bin": "^0.88.0", | ||
"jsdom": "^13.0.0", | ||
"mocha": "^5.2.0", | ||
"nyc": "^13.1.0", | ||
"webpack": "^4.27.1", | ||
"webpack-cli": "^3.1.2" | ||
}, | ||
"babel": { | ||
"plugins": [ | ||
"transform-flow-strip-types" | ||
], | ||
"presets": [ | ||
"es2015" | ||
] | ||
}, | ||
"repository": { | ||
@@ -46,6 +40,27 @@ "type": "git", | ||
"dependencies": { | ||
"is-email": "^1.0.0", | ||
"snyk": "^1.69.11" | ||
"is-email": "^1.0.0" | ||
}, | ||
"snyk": true | ||
"nyc": { | ||
"include": [ | ||
"test/**/*.js" | ||
], | ||
"exclude": [ | ||
"coverage", | ||
"node_modules", | ||
"src" | ||
], | ||
"extension": [ | ||
".js" | ||
], | ||
"require": [ | ||
"@babel/plugin-transform-flow-strip-types", | ||
"@babel/register", | ||
"./test/setup.js" | ||
], | ||
"reporter": [ | ||
"lcov", | ||
"text" | ||
], | ||
"all": true | ||
} | ||
} |
@@ -9,3 +9,3 @@ export function ValidationPromise( | ||
promises: Array<(value: string, row: Object, arg: any) => string>; | ||
key: string; | ||
key: string | string[]; | ||
msg: (value: string, row: Object, arg: any) => string | ||
@@ -16,4 +16,4 @@ }; | ||
state: string; | ||
key: string; | ||
propPath: string[]; | ||
value: string | ||
} | ||
} |
@@ -20,2 +20,24 @@ // @flow | ||
/** | ||
* Sets a value at a nested point within an object | ||
*/ | ||
const setNestedValue = (object: Object, propPath: string[], value): Object => { | ||
propPath.reduce((acc, next, i) => { | ||
if (i === propPath.length - 1) { | ||
if (acc[next]) { | ||
acc[next].push(value); | ||
} else { | ||
acc[next] = [value]; | ||
} | ||
} else if (!acc[next]) { | ||
acc[next] = {}; | ||
} | ||
return acc[next]; | ||
}, object); | ||
return object; | ||
}; | ||
/** | ||
* Iterates over an array of promises, unline Promise.all it will not | ||
@@ -25,20 +47,19 @@ * stop when one promise is rejected. Instead all promises are run and an | ||
*/ | ||
const hashSettled = (promises: Object): Promise<Object[]> => { | ||
let keys: string[] = Object.keys(promises); | ||
return Promise.all(keys.map((k: string) => Promise.resolve(promises[k]) | ||
.then((value: string): ValidationResponse => { | ||
let r: ValidationResponse = { | ||
state: 'fulfilled', | ||
key: k, | ||
value | ||
}; | ||
return r; | ||
}, (reason: string): ValidationResponse => { | ||
let r: ValidationResponse = { | ||
state: 'rejected', | ||
key: k, | ||
reason | ||
}; | ||
return r; | ||
}))); | ||
const hashSettled = (promises: Object[]): Promise<Object[]> => { | ||
return Promise.all(promises.map(({ propPath, rule }) => Promise.resolve(rule) | ||
.then((value: string): ValidationResponse => { | ||
let r: ValidationResponse = { | ||
state: 'fulfilled', | ||
propPath, | ||
value | ||
}; | ||
return r; | ||
}, (reason: string): ValidationResponse => { | ||
let r: ValidationResponse = { | ||
state: 'rejected', | ||
propPath, | ||
reason | ||
}; | ||
return r; | ||
}))); | ||
}, | ||
@@ -54,13 +75,13 @@ | ||
validate = (contract: Array<Validation>, data: Object): boolean | Object => { | ||
let promises: Object = {}; | ||
let promises = []; | ||
contract.forEach((validation: Validation, cx: number) => { | ||
let name = validation.key, | ||
value = data[name]; | ||
validation.promises.forEach((p: ValidationPromise, i: number) => { | ||
let key = name + '.' + cx + '.' + i, | ||
thisArg = p.arg === undefined ? null : p.arg, | ||
validationMessage = p.msg || validation.msg; | ||
const propPath = Array.isArray(validation.key) ? validation.key : [validation.key]; | ||
const value = Array.isArray(propPath) ? propPath.reduce((acc, next) => acc[next], data) : data[propPath]; | ||
promises[key] = p.rule(value, data, validationMessage, thisArg); | ||
}); | ||
promises = promises.concat( | ||
validation.promises.map((p) => ({ | ||
propPath, | ||
rule: p.rule(value, data, p.msg || validation.msg, p.arg === undefined ? null : p.arg), | ||
})), | ||
); | ||
}); | ||
@@ -72,12 +93,6 @@ | ||
const rejectedErrors = (r: ValidationResponse): boolean => r.state === 'rejected'; | ||
let errors = res.filter(rejectedErrors), | ||
ret = {}; | ||
errors.forEach((err: ValidationResponse) => { | ||
let k: string = err.key.split('.').shift(); | ||
if (!ret[k]) { | ||
ret[k] = []; | ||
} | ||
if (ret[k].indexOf(err.reason) === -1) { | ||
ret[k].push(err.reason); | ||
} | ||
const errors = res.filter(rejectedErrors); | ||
let ret = {}; | ||
errors.forEach(({ propPath, reason }: ValidationResponse) => { | ||
ret = setNestedValue(ret, propPath, reason); | ||
}); | ||
@@ -84,0 +99,0 @@ |
@@ -8,3 +8,3 @@ import {expect} from 'chai'; | ||
{ | ||
key: 'email', | ||
key: ['user', 'email'], | ||
promises: [ | ||
@@ -16,7 +16,19 @@ { | ||
msg: (value, row, arg) => value + ' is not an email' | ||
}]; | ||
}, | ||
{ | ||
key: ['user', 'email2'], | ||
promises: [ | ||
{ | ||
rule: email | ||
} | ||
], | ||
msg: (value, row, arg) => value + ' is not an email' | ||
}, | ||
]; | ||
describe('email success?', (done) => { | ||
beforeEach((done) => { | ||
let data = { | ||
email: 'test@test.com' | ||
user: { | ||
email: 'test@test.com', | ||
}, | ||
}; | ||
@@ -37,6 +49,9 @@ | ||
describe('email failed', (done) => { | ||
describe.only('email failed', (done) => { | ||
beforeEach((done) => { | ||
let data = { | ||
email: 'dooo' | ||
user: { | ||
email: 'dooo', | ||
email2: 'test' | ||
}, | ||
}; | ||
@@ -49,2 +64,3 @@ | ||
.catch((error) => { | ||
console.log(error); | ||
failed = error; | ||
@@ -57,5 +73,5 @@ done(); | ||
expect(failed).to.be.an('object'); | ||
expect(failed).to.have.key('email'); | ||
expect(failed.email).to.be.an('array'); | ||
expect(failed.email[0]).to.equal('dooo is not an email'); | ||
expect(failed).to.have.key('user'); | ||
expect(failed.user).to.have.property('email'); | ||
expect(failed.user.email[0]).to.equal('dooo is not an email'); | ||
}); | ||
@@ -62,0 +78,0 @@ }); |
@@ -21,3 +21,3 @@ import {expect} from 'chai'; | ||
test: 'hello', | ||
equalto: 'hello' | ||
equalto: 'test' | ||
}; | ||
@@ -30,3 +30,3 @@ | ||
}) | ||
.catch((error) => done()); | ||
.catch((error) => console.log(error) || done()); | ||
}); | ||
@@ -33,0 +33,0 @@ |
@@ -1,15 +0,13 @@ | ||
var webpack = require('webpack'), | ||
libraryName = 'validate-promise', | ||
path = require('path'), | ||
entry = [ | ||
'./src/index.js' | ||
], | ||
plugins = [ | ||
new webpack.NoErrorsPlugin() | ||
], | ||
loaders = [ | ||
{test: /\.js$/, exclude: /(node_modules)/, loader: 'babel-loader'} | ||
]; | ||
const webpack = require('webpack'); | ||
const libraryName = 'validate-promise'; | ||
const path = require('path'); | ||
const entry = [ | ||
'./src/index.js' | ||
]; | ||
const loaders = [ | ||
{test: /\.js$/, exclude: /(node_modules)/, loader: 'babel-loader'} | ||
]; | ||
// plugins.push(new webpack.optimize.UglifyJsPlugin({ | ||
@@ -25,2 +23,3 @@ // compress: { | ||
entry: entry, | ||
mode: 'production', | ||
output: { | ||
@@ -35,3 +34,3 @@ path: path.join(__dirname, 'dist'), | ||
module: { | ||
loaders: loaders | ||
rules: loaders | ||
}, | ||
@@ -42,3 +41,2 @@ node: { | ||
}, | ||
plugins: plugins, | ||
watchOptions: { | ||
@@ -45,0 +43,0 @@ poll: true |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
1
54812
14
40
1599
- Removedsnyk@^1.69.11
- Removed@sentry-internal/tracing@7.120.2(transitive)
- Removed@sentry/core@7.120.2(transitive)
- Removed@sentry/integrations@7.120.2(transitive)
- Removed@sentry/node@7.120.2(transitive)
- Removed@sentry/types@7.120.2(transitive)
- Removed@sentry/utils@7.120.2(transitive)
- Removedboolean@3.2.0(transitive)
- Removeddefine-data-property@1.1.4(transitive)
- Removeddefine-properties@1.2.1(transitive)
- Removeddetect-node@2.1.0(transitive)
- Removedes-define-property@1.0.1(transitive)
- Removedes-errors@1.3.0(transitive)
- Removedes6-error@4.1.1(transitive)
- Removedescape-string-regexp@4.0.0(transitive)
- Removedglobal-agent@3.0.0(transitive)
- Removedglobalthis@1.0.4(transitive)
- Removedgopd@1.2.0(transitive)
- Removedhas-property-descriptors@1.0.2(transitive)
- Removedimmediate@3.0.6(transitive)
- Removedjson-stringify-safe@5.0.1(transitive)
- Removedlie@3.1.1(transitive)
- Removedlocalforage@1.10.0(transitive)
- Removedmatcher@3.0.0(transitive)
- Removedobject-keys@1.1.1(transitive)
- Removedroarr@2.15.4(transitive)
- Removedsemver@7.6.3(transitive)
- Removedsemver-compare@1.0.0(transitive)
- Removedserialize-error@7.0.1(transitive)
- Removedsnyk@1.1294.3(transitive)
- Removedsprintf-js@1.1.3(transitive)
- Removedtype-fest@0.13.1(transitive)