Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

validate-promise

Package Overview
Dependencies
Maintainers
1
Versions
51
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

validate-promise - npm Package Compare versions

Comparing version 2.0.2 to 2.0.3

481

dist/index.js

@@ -1,2 +0,481 @@

!function(e,t){"object"==typeof exports&&"object"==typeof module?module.exports=t():"function"==typeof define&&define.amd?define("validate-promise",[],t):"object"==typeof exports?exports["validate-promise"]=t():e["validate-promise"]=t()}(this,function(){return function(e){function t(n){if(r[n])return r[n].exports;var o=r[n]={exports:{},id:n,loaded:!1};return e[n].call(o.exports,o,o.exports,t),o.loaded=!0,o.exports}var r={};return t.m=e,t.c=r,t.p="/",t(0)}([function(e,t,r){e.exports=r(1)},function(e,t,r){"use strict";function n(e){return e&&e.__esModule?e:{default:e}}Object.defineProperty(t,"__esModule",{value:!0}),t.whitelist=t.required=t.lessthan=t.int=t.greaterthan=t.equals=t.blacklist=t.before=t.after=void 0;var o=r(2),u=n(o),i=r(3),s=n(i),f=r(4),a=n(f),c=r(5),l=n(c),d=r(6),p=n(d),v=r(7),m=n(v),P=r(8),y=n(P),j=r(9),_=n(j),b=r(10),h=n(b),x=function(e){var t=Object.keys(e);return Promise.all(t.map(function(t){return Promise.resolve(e[t]).then(function(e){var r={state:"fulfilled",key:t,value:e};return r},function(e){var r={state:"rejected",key:t,reason:e};return r})}))},O=function(e,t){var r={};return e.forEach(function(e,n){var o=e.key,u=t[o];e.promises.forEach(function(i,s){var f=o+"."+n+"."+s,a=void 0===i.arg?null:i.arg;r[f]=i.rule(u,t,e.msg,a)})}),new Promise(function(e,t){x(r).then(function(r){var n=r.filter(function(e){return"rejected"===e.state}),o={};n.forEach(function(e){var t=e.key.split(".").shift();o[t]||(o[t]=[]),o[t].push(e.reason)}),0===n.length&&e(!0),t(o)})})};t.default=O,t.after=u.default,t.before=s.default,t.blacklist=a.default,t.equals=l.default,t.greaterthan=p.default,t.int=m.default,t.lessthan=y.default,t.required=_.default,t.whitelist=h.default},function(e,t){"use strict";Object.defineProperty(t,"__esModule",{value:!0}),t.default=function(e,t,r,n){var o=Date.parse(e);"function"==typeof n&&(n=n(e,t));var u=Date.parse(n);return o>u?Promise.resolve():Promise.reject(r(e,t,n))}},function(e,t){"use strict";Object.defineProperty(t,"__esModule",{value:!0}),t.default=function(e,t,r,n){var o=Date.parse(e);"function"==typeof n&&(n=n(e,t));var u=Date.parse(n);return o<u?Promise.resolve():Promise.reject(r(e,t,n))}},function(e,t){"use strict";Object.defineProperty(t,"__esModule",{value:!0}),t.default=function(e,t,r,n){return"function"==typeof n&&(n=n(e,t)),n.indexOf(e)===-1?Promise.resolve():Promise.reject(r(e,t,n))}},function(e,t){"use strict";Object.defineProperty(t,"__esModule",{value:!0}),t.default=function(e,t,r,n){return"function"==typeof n&&(n=n(e,t)),e==n?Promise.resolve():Promise.reject(r(e,t,n))}},function(e,t){"use strict";Object.defineProperty(t,"__esModule",{value:!0}),t.default=function(e,t,r,n){return"function"==typeof n&&(n=n(e,t)),parseInt(e,10)>n?Promise.resolve():Promise.reject(r(e,t,n))}},function(e,t){"use strict";Object.defineProperty(t,"__esModule",{value:!0}),t.default=function(e,t,r,n){"function"==typeof n&&(n=n(e,t));var o=/^(?:[-+]?(?:0|[1-9][0-9]*))$/;if(null!==n){if(void 0!==n.min&&parseInt(e,10)<parseInt(n.min,10))return Promise.reject(r(e,t,n));if(n.max&&parseInt(e,10)>parseInt(n.max,10))return Promise.reject(r(e,t,n))}return o.test(e)?Promise.resolve():Promise.reject(r(e,t,n))}},function(e,t){"use strict";Object.defineProperty(t,"__esModule",{value:!0}),t.default=function(e,t,r,n){return"function"==typeof n&&(n=n(e,t)),parseInt(e,10)<n?Promise.resolve():Promise.reject(r(e,t,n))}},function(e,t){"use strict";Object.defineProperty(t,"__esModule",{value:!0}),t.default=function(e,t,r){return new Promise(function(n,o){return""!==e&&void 0!==e?n():o(r(e,t))})}},function(e,t){"use strict";Object.defineProperty(t,"__esModule",{value:!0}),t.default=function(e,t,r,n){return"function"==typeof n&&(n=n(e,t)),n.indexOf(e)!==-1?Promise.resolve():Promise.reject(r(e,t,n))}}])});
(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();
})(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] = {
/******/ exports: {},
/******/ id: moduleId,
/******/ loaded: false
/******/ };
/******/ // Execute the module function
/******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__);
/******/ // Flag the module as loaded
/******/ module.loaded = 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;
/******/ // __webpack_public_path__
/******/ __webpack_require__.p = "/";
/******/ // Load entry module and return exports
/******/ return __webpack_require__(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.lessthan = exports.int = exports.greaterthan = exports.equals = 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 _equals = __webpack_require__(5);
var _equals2 = _interopRequireDefault(_equals);
var _greaterthan = __webpack_require__(6);
var _greaterthan2 = _interopRequireDefault(_greaterthan);
var _int = __webpack_require__(7);
var _int2 = _interopRequireDefault(_int);
var _lessthan = __webpack_require__(8);
var _lessthan2 = _interopRequireDefault(_lessthan);
var _required = __webpack_require__(9);
var _required2 = _interopRequireDefault(_required);
var _whitelist = __webpack_require__(10);
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;
promises[key] = p.rule(value, data, validation.msg, thisArg);
});
});
return new Promise(function (resolve, reject) {
hashSettled(promises).then(function (res) {
var errors = res.filter(function (r) {
return r.state === 'rejected';
}),
ret = {};
errors.forEach(function (err) {
var k = err.key.split('.').shift();
if (!ret[k]) {
ret[k] = [];
}
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.equals = _equals2.default;
exports.greaterthan = _greaterthan2.default;
exports.int = _int2.default;
exports.lessthan = _lessthan2.default;
exports.required = _required2.default;
exports.whitelist = _whitelist2.default;
/***/ },
/* 2 */
/***/ function(module, exports) {
'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) {
'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) {
'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) {
'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));
};
/***/ },
/* 6 */
/***/ function(module, exports) {
'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));
};
/***/ },
/* 7 */
/***/ function(module, exports) {
'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));
};
/***/ },
/* 8 */
/***/ function(module, exports) {
'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;
}
console.log('less than,', parseInt(value, 10), ' < ', parseInt(compare, 10));
if (parseInt(value, 10) < parseInt(compare, 10)) {
console.log('----resolve');
return Promise.resolve();
}
console.log('-----reject');
return Promise.reject(msg(value, row, arg));
};
/***/ },
/* 9 */
/***/ function(module, exports) {
'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));
});
};
/***/ },
/* 10 */
/***/ function(module, exports) {
'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));
};
/***/ }
/******/ ])
});
;
//# sourceMappingURL=index.js.map

2

package.json
{
"name": "validate-promise",
"version": "2.0.2",
"version": "2.0.3",
"description": "Promised based validaation library",

@@ -5,0 +5,0 @@ "main": "dist/index.js",

@@ -130,4 +130,21 @@ # validation-promise

```
Tests a value is greater than the supplied argument
Tests a value is greater than the supplied argument.
Alternatively you can compare two arbitrary values with the following contract:
```javascript
contract = [
{
key: 'sales',
promises: [
{
rule: greaterthan,
arg: (value, row) => ({compare: 0, value: 10})
}
],
msg: (value, row, arg) => 'Sales must be greater than 0'
}]
```
This tests if value (10) is greater than the compare (0) value.
### Int

@@ -167,3 +184,20 @@

Test a value is less than the supplied argument.
Alternatively you can compare two arbitrary values with the following contract:
```javascript
contract = [
{
key: 'age',
promises: [
{
rule: lessthan,
arg: (value, row) => ({compare: 0, value: 10})
}
],
msg: (value, row, arg) => 'Age must be greater than 0'
}]
```
This tests if value (10) is less than than the compare (0) value.
### Required

@@ -170,0 +204,0 @@

export function ValidationPromise(
value: string,
row: Object,
value: string;
row: Object;
arg: any[]

@@ -14,5 +14,5 @@ ): Promise<?string>;

export type ValidationResponse = {
state: string,
key: string,
state: string;
key: string;
value: string
}
// @flow
type ArgFunc = (value: string, row: Object) => number;
type CompareSet = {
value: string;
compare: string
};

@@ -13,7 +17,12 @@ /**

*/
export default (value: string, row: Object, msg: Function, arg: number|ArgFunc) : Promise<?string> => {
export default (value: string, row: Object, msg: Function, arg: number|ArgFunc|CompareSet) : Promise<?string> => {
let compare = arg;
if (typeof arg === 'function') {
arg = arg(value, row);
compare = arg(value, row);
}
if (parseInt(value, 10) > arg) {
if (typeof compare === 'object') {
value = compare.value;
compare = compare.compare;
}
if (parseInt(value, 10) > parseInt(compare, 10)) {
return Promise.resolve();

@@ -20,0 +29,0 @@ }

// @flow
type ArgFunc = (value: string, row: Object) => number;
type CompareSet = {
value: string;
compare: string
};

@@ -13,10 +17,17 @@ /**

*/
export default (value: string, row: Object, msg: Function, arg: number|ArgFunc) : Promise<?string> => {
export default (value: string, row: Object, msg: Function, arg: number|ArgFunc|CompareSet) : Promise<?string> => {
let compare = arg;
if (typeof arg === 'function') {
arg = arg(value, row);
compare = arg(value, row);
}
if (parseInt(value, 10) < arg) {
if (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));
};

@@ -5,5 +5,6 @@ import {expect} from 'chai';

describe('validates', () => {
let res, failed,
let res, failed, resReturnObject,
contract = [
{key: 'age',
{
key: 'age',
promises: [

@@ -16,3 +17,16 @@ {

msg: (value, row, arg) => 'age greater than 18'
}],
contractReturnObject = [
{
key: 'age',
promises: [
{
rule: greaterthan,
arg: () => ({compare: 18, value: 19})
}
],
msg: (value, row, arg) => 'age (19) greater than 18'
}];
describe('greater than success', done => {

@@ -37,2 +51,21 @@ beforeEach(done => {

describe('greater than success: return object', done => {
beforeEach(done => {
let data = {
age: '19'
};
validate(contractReturnObject, data)
.then(data => {
resReturnObject = data;
done();
})
.catch(error => done());
});
it('passes the validation', () => {
expect(resReturnObject).to.equal(true);
});
});
describe('greater than failed', done => {

@@ -39,0 +72,0 @@ beforeEach(done => {

@@ -5,3 +5,3 @@ import {expect} from 'chai';

describe('validates', () => {
let res, failed,
let res, failed, resLessThanSuccess,
contract = [

@@ -16,3 +16,16 @@ {key: 'age',

msg: (value, row, arg) => 'age less than 18'
}],
contractReturnObject = [
{
key: 'age',
promises: [
{
rule: lessthan,
arg: () => ({compare: 21, value: 19})
}
],
msg: (value, row, arg) => 'age (19) less than 21'
}];
describe('less than success', done => {

@@ -37,2 +50,21 @@ beforeEach(done => {

describe('less than success: return object', done => {
beforeEach(done => {
let data = {
age: '17'
};
validate(contractReturnObject, data)
.then(data => {
resLessThanSuccess = data;
done();
})
.catch(error => done());
});
it('passes the validation', () => {
expect(resLessThanSuccess).to.equal(true);
});
});
describe('less than failed', done => {

@@ -39,0 +71,0 @@ beforeEach(done => {

@@ -15,8 +15,8 @@ var webpack = require('webpack'),

plugins.push(new webpack.optimize.UglifyJsPlugin({
compress: {
warnings: false
}
}
));
// plugins.push(new webpack.optimize.UglifyJsPlugin({
// compress: {
// warnings: false
// }
// }
// ));

@@ -23,0 +23,0 @@ module.exports = {

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc