New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details
Socket
Book a DemoSign in
Socket

validy

Package Overview
Dependencies
Maintainers
1
Versions
9
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

validy - npm Package Compare versions

Comparing version
0.0.2
to
0.1.0
+4
lib/formatters.js
'use strict';
exports.flat = require('./formatters/flat');
exports.nested = require('./formatters/nested');
'use strict';
/**
* Errors object has nested structure by default, so we just return object without any changes
*
* @param {Object} errors
*
* @returns {Object}
*/
module.exports = function (errors) {
return errors;
};
'use strict';
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
function _extendableBuiltin(cls) {
function ExtendableBuiltin() {
var instance = Reflect.construct(cls, Array.from(arguments));
Object.setPrototypeOf(instance, Object.getPrototypeOf(this));
return instance;
}
ExtendableBuiltin.prototype = Object.create(cls.prototype, {
constructor: {
value: cls,
enumerable: false,
writable: true,
configurable: true
}
});
if (Object.setPrototypeOf) {
Object.setPrototypeOf(ExtendableBuiltin, cls);
} else {
ExtendableBuiltin.__proto__ = cls;
}
return ExtendableBuiltin;
}
var ValidationError = function (_extendableBuiltin2) {
_inherits(ValidationError, _extendableBuiltin2);
function ValidationError(errors) {
_classCallCheck(this, ValidationError);
var _this = _possibleConstructorReturn(this, (ValidationError.__proto__ || Object.getPrototypeOf(ValidationError)).call(this));
if (Error.captureStackTrace) {
Error.captureStackTrace(_this, _this.constructor);
}
_this.name = _this.constructor.name;
_this.errors = errors;
return _this;
}
return ValidationError;
}(_extendableBuiltin(Error));
module.exports = ValidationError;
+1211
-730
(function(f){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}g.validy = f()}})(function(){var define,module,exports;return (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){
'use strict';
exports.TimeoutError = require('./timeoutError');
},{"./timeoutError":2}],2:[function(require,module,exports){
exports.flat = require('./formatters/flat');
exports.nested = require('./formatters/nested');
},{"./formatters/flat":2,"./formatters/nested":3}],2:[function(require,module,exports){
'use strict';
var util = require('util');
/**
* @param {String} [message=Validation timeout]
*
* @constructor
*/
function TimeoutError() {
var message = arguments.length <= 0 || arguments[0] === undefined ? 'Validation timeout' : arguments[0];
Error.call(this);
Error.captureStackTrace(this, this.constructor);
this.name = this.constructor.name;
this.message = message;
}
util.inherits(TimeoutError, Error);
module.exports = TimeoutError;
},{"util":15}],3:[function(require,module,exports){
'use strict';
/**
* @param {Object} object
* @param {String} [path='']
* @param {Object} [flattenObject={}]
* @param {string} [path='']
* @param {Object} [flatObject={}]
*

@@ -40,4 +18,4 @@ * @returns {Object}

function flatten(object) {
var path = arguments.length <= 1 || arguments[1] === undefined ? '' : arguments[1];
var flattenObject = arguments.length <= 2 || arguments[2] === undefined ? {} : arguments[2];
var path = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : '';
var flatObject = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};

@@ -47,10 +25,10 @@ Object.keys(object).forEach(function (propertyName) {

if (propertyValue instanceof Array) {
flattenObject[path + propertyName] = propertyValue;
if (propertyValue instanceof Object && !(propertyValue instanceof Array)) {
flatten(propertyValue, path + propertyName + '.', flatObject);
} else {
flatten(propertyValue, path + propertyName + '.', flattenObject);
flatObject[path + propertyName] = propertyValue;
}
});
return flattenObject;
return flatObject;
}

@@ -66,40 +44,34 @@

};
},{}],4:[function(require,module,exports){
},{}],3:[function(require,module,exports){
'use strict';
exports.flat = require('./flat');
},{"./flat":3}],5:[function(require,module,exports){
'use strict';
/**
* @param {Object} schema
*
* @returns {Boolean}
* Errors object has nested structure by default, so we just return object without any changes
*
* @param {Object} errors
*
* @returns {Object}
*/
exports.hasSchemaAtLeastOneProperty = function (schema) {
for (var propertyName in schema) {
if (!propertyName.startsWith('$')) {
return true;
}
}
return false;
module.exports = function (errors) {
return errors;
};
},{}],6:[function(require,module,exports){
},{}],4:[function(require,module,exports){
'use strict';
var validateObject = require('./validateObject');
var errors = require('./errors');
var ValidationError = require('./validationError');
var formatters = require('./formatters');
var DEFAULT_TIMEOUT = 10000;
var DEFAULT_OPTIONS = {
format: 'flat',
reject: false
};
/**
* @param {Object} object
* @param {Object} schema
* @param {Object} [options={}]
* @param {Number} [options.timeout=10000]
* @param {String} [options.format] - There is no default formatter
* @param {Number} [options.maxPropertyErrorsCount] - By default all property errors will be returned. Must be >= 1
* @param {Object} object
* @param {Object} schema
* @param {Object} [options={}]
* @param {string} [options.format=flat]
* @param {boolean} [options.reject=false]
*

@@ -109,32 +81,28 @@ * @returns {Promise}

module.exports = function (object, schema) {
var options = arguments.length <= 2 || arguments[2] === undefined ? {} : arguments[2];
var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
options = Object.assign({}, DEFAULT_OPTIONS, options);
return new Promise(function (resolve, reject) {
var timeout = options.timeout || DEFAULT_TIMEOUT;
var format = options.format;
if (format && !formatters[format]) {
return reject(new Error('Unknown format ' + format));
var formatter = formatters[options.format];
if (!formatter) {
return reject(new Error('Unknown format ' + options.format));
}
var timeoutObject = setTimeout(function () {
reject(new errors.TimeoutError());
}, timeout);
validateObject(object, schema, object, []).then(function (validationErrors) {
if (!validationErrors) {
return resolve();
}
validateObject(object, schema, object, [], options).then(function (result) {
clearTimeout(timeoutObject);
var formattedErrors = formatter(validationErrors);
if (format) {
var formattedResult = formatters[format](result);
return resolve(formattedResult);
if (options.reject) {
reject(new ValidationError(formattedErrors));
} else {
resolve(formattedErrors);
}
resolve(result);
}).catch(function (err) {
clearTimeout(timeoutObject);
reject(err);
});
}).catch(reject);
});
};
},{"./errors":1,"./formatters":4,"./validateObject":7}],7:[function(require,module,exports){
},{"./formatters":1,"./validateObject":5,"./validationError":8}],5:[function(require,module,exports){
'use strict';

@@ -147,10 +115,8 @@

* @param {Object} schema
* @param {Object} originalObject
* @param {String[]} path
* @param {Object} options
* @param {Number} [options.maxPropertyErrorsCount]
* @param {Object} fullObject
* @param {string[]} path
*
* @returns {Promise}
*/
module.exports = function (object, schema, originalObject, path, options) {
module.exports = function (object, schema, fullObject, path) {
var validationPromises = [];

@@ -169,7 +135,7 @@ var validationErrors = {};

if (propertySchema instanceof Function) {
propertySchema = propertySchema(propertyValue, object, originalObject, propertyPath);
propertySchema = propertySchema(propertyValue, object, fullObject, propertyPath);
}
if (propertySchema) {
var validationPromise = validateProperty(propertyValue, propertySchema, object, originalObject, propertyPath, options);
if (propertySchema instanceof Object) {
var validationPromise = validateProperty(propertyValue, propertySchema, object, fullObject, propertyPath);

@@ -192,9 +158,6 @@ validationPromise.then(function (validationError) {

};
},{"./validateProperty":8}],8:[function(require,module,exports){
},{"./validateProperty":6}],6:[function(require,module,exports){
'use strict';
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol ? "symbol" : typeof obj; };
var validateValue = require('./validateValue');
var utils = require('./utils');

@@ -205,21 +168,15 @@ /**

* @param {Object} object
* @param {Object} originalObject
* @param {String[]} path
* @param {Object} options
* @param {Number} [options.maxPropertyErrorsCount]
* @param {Object} fullObject
* @param {string[]} path
*
* @returns {Promise}
*
*/
module.exports = function (value, schema, object, originalObject, path, options) {
var validatorsOptions = schema.$validators;
module.exports = function (value, schema, object, fullObject, path) {
var validatorsOptions = schema.$validate;
if (validatorsOptions instanceof Function) {
validatorsOptions = validatorsOptions(value, object, originalObject, path);
validatorsOptions = validatorsOptions(value, object, fullObject, path);
}
var promise = void 0;
if (validatorsOptions) {
promise = validateValue(value, validatorsOptions, object, originalObject, path, options);
} else {
promise = Promise.resolve();
}
var promise = validatorsOptions instanceof Object ? validateValue(value, validatorsOptions, object, fullObject, path) : Promise.resolve();

@@ -233,35 +190,35 @@ return promise.then(function (validationErrors) {

if (schema.$items || schema instanceof Array) {
var _ret = function () {
if (!(value instanceof Array)) {
return {
v: Promise.resolve(['Must be an array'])
};
}
if (schema.$items || schema[0]) {
if (!(value instanceof Array)) {
return Promise.resolve([{
error: 'array',
message: 'must be an array'
}]);
}
var propertiesSchema = {};
var itemSchema = schema.$items || schema[0];
var propertiesSchema = {};
var itemSchema = schema.$items || schema[0];
value.forEach(function (item, index) {
propertiesSchema[index] = itemSchema;
});
value.forEach(function (item, index) {
return propertiesSchema[index] = itemSchema;
});
return {
v: validateObject(value, propertiesSchema, originalObject, path, options)
};
}();
if ((typeof _ret === 'undefined' ? 'undefined' : _typeof(_ret)) === "object") return _ret.v;
return validateObject(value, propertiesSchema, fullObject, path);
}
if (utils.hasSchemaAtLeastOneProperty(schema)) {
if (Object.keys(schema).some(function (propertyName) {
return !propertyName.startsWith('$');
})) {
if (!(value instanceof Object)) {
return Promise.resolve(['Must be an object']);
return Promise.resolve([{
error: 'object',
message: 'must be an object'
}]);
}
return validateObject(value, schema, originalObject, path, options);
return validateObject(value, schema, fullObject, path);
}
});
};
},{"./utils":5,"./validateObject":7,"./validateValue":9}],9:[function(require,module,exports){
},{"./validateObject":5,"./validateValue":7}],7:[function(require,module,exports){
'use strict';

@@ -275,10 +232,8 @@

* @param {Object} object
* @param {Object} originalObject
* @param {String[]} path
* @param {Object} options
* @param {Number} [options.maxPropertyErrorsCount]
* @param {Object} fullObject
* @param {string[]} path
*
* @returns {Promise}
*/
module.exports = function (value, validatorsOptions, object, originalObject, path, options) {
module.exports = function (value, validatorsOptions, object, fullObject, path) {
var validatorsResultsPromises = [];

@@ -295,10 +250,10 @@ var validationErrors = [];

if (validatorOptions instanceof Function) {
validatorOptions = validatorOptions(value, object, originalObject, path);
validatorOptions = validatorOptions(value, object, fullObject, path);
}
if (!validatorOptions) {
if (validatorOptions === false || validatorOptions === null || validatorOptions === undefined) {
return;
}
var validatorResult = validator.call(validator, value, validatorOptions, object, originalObject, path);
var validatorResult = validator(value, validatorOptions, object, fullObject, path);

@@ -317,6 +272,2 @@ var validatorResultPromise = Promise.resolve(validatorResult);

if (validationErrors.length) {
if (options.maxPropertyErrorsCount) {
return validationErrors.slice(0, options.maxPropertyErrorsCount);
}
return validationErrors;

@@ -326,735 +277,1265 @@ }

};
},{"./validators":10}],10:[function(require,module,exports){
},{"./validators":9}],8:[function(require,module,exports){
'use strict';
module.exports = {};
},{}],11:[function(require,module,exports){
'use strict';
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
var validate = require('./validate');
function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
validate.validators = require('./validators');
validate.formatters = require('./formatters');
validate.errors = require('./errors');
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
module.exports = validate;
},{"./errors":1,"./formatters":4,"./validate":6,"./validators":10}],12:[function(require,module,exports){
if (typeof Object.create === 'function') {
// implementation from standard node.js 'util' module
module.exports = function inherits(ctor, superCtor) {
ctor.super_ = superCtor
ctor.prototype = Object.create(superCtor.prototype, {
constructor: {
value: ctor,
enumerable: false,
writable: true,
configurable: true
}
function _extendableBuiltin(cls) {
function ExtendableBuiltin() {
var instance = Reflect.construct(cls, Array.from(arguments));
Object.setPrototypeOf(instance, Object.getPrototypeOf(this));
return instance;
}
ExtendableBuiltin.prototype = Object.create(cls.prototype, {
constructor: {
value: cls,
enumerable: false,
writable: true,
configurable: true
}
});
};
} else {
// old school shim for old browsers
module.exports = function inherits(ctor, superCtor) {
ctor.super_ = superCtor
var TempCtor = function () {}
TempCtor.prototype = superCtor.prototype
ctor.prototype = new TempCtor()
ctor.prototype.constructor = ctor
}
if (Object.setPrototypeOf) {
Object.setPrototypeOf(ExtendableBuiltin, cls);
} else {
ExtendableBuiltin.__proto__ = cls;
}
return ExtendableBuiltin;
}
},{}],13:[function(require,module,exports){
// shim for using process in browser
var ValidationError = function (_extendableBuiltin2) {
_inherits(ValidationError, _extendableBuiltin2);
var process = module.exports = {};
var queue = [];
var draining = false;
var currentQueue;
var queueIndex = -1;
function ValidationError(errors) {
_classCallCheck(this, ValidationError);
function cleanUpNextTick() {
if (!draining || !currentQueue) {
return;
var _this = _possibleConstructorReturn(this, (ValidationError.__proto__ || Object.getPrototypeOf(ValidationError)).call(this));
if (Error.captureStackTrace) {
Error.captureStackTrace(_this, _this.constructor);
}
_this.name = _this.constructor.name;
_this.errors = errors;
return _this;
}
draining = false;
if (currentQueue.length) {
queue = currentQueue.concat(queue);
} else {
queueIndex = -1;
return ValidationError;
}(_extendableBuiltin(Error));
module.exports = ValidationError;
},{}],9:[function(require,module,exports){
'use strict';
var validators = require('common-validators');
validators.oneOptionsArg = true;
var originalAdd = validators.add;
validators.add = function (validatorName, validator) {
if (validatorName instanceof Object) {
return originalAdd.call(validators, validatorName, { simpleArgsFormat: true });
}
if (queue.length) {
drainQueue();
}
}
function drainQueue() {
if (draining) {
return;
originalAdd.call(validators, validatorName, validator, { simpleArgsFormat: true });
};
validators.confirmOriginal = validators.confirm;
validators.add({
confirm: function confirm(value, options, object, fullObject, path) {
var confirmOptions = {
key: path[path.length - 1],
comparedKey: options instanceof Object ? options.key : options
};
return this.confirmOriginal(object, confirmOptions);
}
var timeout = setTimeout(cleanUpNextTick);
draining = true;
});
var len = queue.length;
while(len) {
currentQueue = queue;
queue = [];
while (++queueIndex < len) {
if (currentQueue) {
currentQueue[queueIndex].run();
module.exports = validators;
},{"common-validators":12}],10:[function(require,module,exports){
'use strict';
var validate = require('./validate');
validate.validators = require('./validators');
validate.formatters = require('./formatters');
validate.ValidationError = require('./validationError');
module.exports = validate;
},{"./formatters":1,"./validate":4,"./validationError":8,"./validators":9}],11:[function(require,module,exports){
'use strict';
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol ? "symbol" : typeof obj; };
var toDateTime = require('normalize-date');
/* Validators */
var validators = {
custom: function custom(value, arg, options) {
if (typeof arg === 'function') {
return arg(value, options);
}
},
//Isn't empty
required: function required(value) {
if (!exists(value)) {
return "Is required";
}
},
presence: 'required',
notEmpty: function notEmpty(value) {
if (isEmpty(value)) {
return "Can't be blank";
}
},
//Equality
equal: function equal(value, arg, options) {
if (exists(value) && !deepEqual(value, arg, options.strict)) {
return 'Must be equal %{arg}';
}
},
confirm: function confirm(value, options) {
if (exists(value) && !deepEqual(toObject(value)[options.key], toObject(value)[options.comparedKey], options.strict)) {
return '%{key} must be equal %{comparedKey}';
}
},
//Types
object: function object(value) {
if (!isPlainObject(value)) {
return 'Must be an object';
}
},
array: function array(value) {
if (!isArray(value)) {
return 'Must be an array';
}
},
string: function string(value) {
if (!isString(value)) {
return 'Must be a string';
}
},
number: function number(value) {
if (!isNumber(value)) {
return 'Must be a number';
}
},
integer: function integer(value) {
if (!isInteger(value)) {
return 'Must be an integer';
}
},
date: function date(value) {
if (!isDateTime(value)) {
return 'Must be a valid date';
}
},
boolean: function boolean(value) {
if (!isBoolean(value)) {
return 'Must be a boolean';
}
},
function: function _function(value) {
if (!isFunction(value)) {
return 'Must be a function';
}
},
null: function _null(value) {
if (value !== null) {
return 'Must be a null';
}
},
//Number
max: function max(value, arg, options) {
if (exists(value) && !(options.exclusive ? toNumber(value) < arg : toNumber(value) <= arg)) {
return options.exclusive ? 'Must be less %{arg}' : 'Must be less or equal %{arg}';
}
},
min: function min(value, arg, options) {
if (exists(value) && !(options.exclusive ? toNumber(value) > arg : toNumber(value) >= arg)) {
return options.exclusive ? 'Must be more %{arg}' : 'Must be more or equal %{arg}';
}
},
range: function range(value, options) {
if (exists(value)) {
if (!(options.exclusiveFrom || options.exclusive ? toNumber(value) > options.from : toNumber(value) >= options.from)) {
return {
error: 'range.less',
message: options.lessMessage || 'Must be from %{from} to %{to}'
};
} else if (!(options.exclusiveTo || options.exclusive ? toNumber(value) < options.to : toNumber(value) <= options.to)) {
return {
error: 'range.many',
message: options.manyMessage || 'Must be from %{from} to %{to}'
};
}
}
queueIndex = -1;
len = queue.length;
}
currentQueue = null;
draining = false;
clearTimeout(timeout);
}
},
process.nextTick = function (fun) {
var args = new Array(arguments.length - 1);
if (arguments.length > 1) {
for (var i = 1; i < arguments.length; i++) {
args[i - 1] = arguments[i];
odd: function odd(value) {
if (exists(value) && toNumber(value) % 2 !== 1) {
return 'Must be odd';
}
}
queue.push(new Item(fun, args));
if (queue.length === 1 && !draining) {
setTimeout(drainQueue, 0);
}
};
},
// v8 likes predictible objects
function Item(fun, array) {
this.fun = fun;
this.array = array;
}
Item.prototype.run = function () {
this.fun.apply(null, this.array);
};
process.title = 'browser';
process.browser = true;
process.env = {};
process.argv = [];
process.version = ''; // empty string to avoid regexp issues
process.versions = {};
even: function even(value) {
if (exists(value) && toNumber(value) % 2 !== 0) {
return 'Must be even';
}
},
function noop() {}
divisible: function divisible(value, arg) {
if (exists(value) && toNumber(value) % arg !== 0) {
return 'Must be divisible by %{arg}';
}
},
process.on = noop;
process.addListener = noop;
process.once = noop;
process.off = noop;
process.removeListener = noop;
process.removeAllListeners = noop;
process.emit = noop;
maxLength: function maxLength(value, arg) {
if (exists(value) && toArray(value).length > arg) {
return 'Length must be less or equal %{arg}';
}
},
process.binding = function (name) {
throw new Error('process.binding is not supported');
};
minLength: function minLength(value, arg) {
if (exists(value) && toArray(value).length < arg) {
return 'Length must be more or equal %{arg}';
}
},
process.cwd = function () { return '/' };
process.chdir = function (dir) {
throw new Error('process.chdir is not supported');
};
process.umask = function() { return 0; };
equalLength: function equalLength(value, arg) {
if (exists(value) && toArray(value).length !== arg) {
return 'Length must be equal %{arg}';
}
},
},{}],14:[function(require,module,exports){
module.exports = function isBuffer(arg) {
return arg && typeof arg === 'object'
&& typeof arg.copy === 'function'
&& typeof arg.fill === 'function'
&& typeof arg.readUInt8 === 'function';
}
},{}],15:[function(require,module,exports){
(function (process,global){
// Copyright Joyent, Inc. and other Node contributors.
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to permit
// persons to whom the Software is furnished to do so, subject to the
// following conditions:
//
// The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
// USE OR OTHER DEALINGS IN THE SOFTWARE.
rangeLength: function rangeLength(value, options) {
if (exists(value)) {
if (toArray(value).length > options.to) {
return {
error: 'rangeLength.many',
message: options.manyMessage || 'Length must be from %{from} to %{to}'
};
} else if (toArray(value).length < options.from) {
return {
error: 'rangeLength.less',
message: options.lessMessage || 'Length must be from %{from} to %{to}'
};
}
}
},
var formatRegExp = /%[sdj%]/g;
exports.format = function(f) {
if (!isString(f)) {
var objects = [];
for (var i = 0; i < arguments.length; i++) {
objects.push(inspect(arguments[i]));
}
return objects.join(' ');
}
//Size
maxSize: function maxSize(value, arg, options) {
var valueSize = byteLength(value, options);
var i = 1;
var args = arguments;
var len = args.length;
var str = String(f).replace(formatRegExp, function(x) {
if (x === '%%') return '%';
if (i >= len) return x;
switch (x) {
case '%s': return String(args[i++]);
case '%d': return Number(args[i++]);
case '%j':
try {
return JSON.stringify(args[i++]);
} catch (_) {
return '[Circular]';
if (exists(value) && valueSize > arg) {
return {
message: 'Size must be less %{arg}',
size: valueSize
};
}
default:
return x;
}
});
for (var x = args[i]; i < len; x = args[++i]) {
if (isNull(x) || !isObject(x)) {
str += ' ' + x;
} else {
str += ' ' + inspect(x);
}
}
return str;
};
},
minSize: function minSize(value, arg, options) {
var valueSize = byteLength(value, options);
// Mark that a method should not be used.
// Returns a modified function which warns once by default.
// If --no-deprecation is set, then it is a no-op.
exports.deprecate = function(fn, msg) {
// Allow for deprecating things in the process of starting up.
if (isUndefined(global.process)) {
return function() {
return exports.deprecate(fn, msg).apply(this, arguments);
};
}
if (exists(value) && valueSize < arg) {
return {
message: 'Size must be more %{arg}',
size: valueSize
};
}
},
if (process.noDeprecation === true) {
return fn;
}
equalSize: function equalSize(value, arg, options) {
var valueSize = byteLength(value, options);
var warned = false;
function deprecated() {
if (!warned) {
if (process.throwDeprecation) {
throw new Error(msg);
} else if (process.traceDeprecation) {
console.trace(msg);
} else {
console.error(msg);
}
warned = true;
}
return fn.apply(this, arguments);
}
if (exists(value) && valueSize !== arg) {
return {
message: 'Length must be equal %{arg}',
size: valueSize
};
}
},
return deprecated;
};
rangeSize: function rangeSize(value, options) {
var valueSize = byteLength(value, options);
if (exists(value)) {
if (valueSize < options.from) {
return {
error: 'rangeSize.less',
message: options.lessMessage || 'Size must be from %{from} to %{to}',
size: valueSize
};
} else if (valueSize > options.to) {
return {
error: 'rangeSize.many',
message: options.manyMessage || 'Size must be from %{from} to %{to}',
size: valueSize
};
}
}
},
var debugs = {};
var debugEnviron;
exports.debuglog = function(set) {
if (isUndefined(debugEnviron))
debugEnviron = process.env.NODE_DEBUG || '';
set = set.toUpperCase();
if (!debugs[set]) {
if (new RegExp('\\b' + set + '\\b', 'i').test(debugEnviron)) {
var pid = process.pid;
debugs[set] = function() {
var msg = exports.format.apply(exports, arguments);
console.error('%s %d: %s', set, pid, msg);
};
} else {
debugs[set] = function() {};
}
}
return debugs[set];
};
//RegExp
pattern: function pattern(value, arg) {
if (exists(value) && !new RegExp(arg).test(toString(value))) {
return 'Does not match the pattern %{arg}';
}
},
//White and black list
inclusion: function inclusion(value, arg) {
if (exists(value) && !contains(arg, value)) {
return '%{value} is not allowed';
}
},
/**
* Echos the value of a value. Trys to print the value out
* in the best way possible given the different types.
*
* @param {Object} obj The object to print out.
* @param {Object} opts Optional options object that alters the output.
*/
/* legacy: obj, showHidden, depth, colors*/
function inspect(obj, opts) {
// default options
var ctx = {
seen: [],
stylize: stylizeNoColor
};
// legacy...
if (arguments.length >= 3) ctx.depth = arguments[2];
if (arguments.length >= 4) ctx.colors = arguments[3];
if (isBoolean(opts)) {
// legacy...
ctx.showHidden = opts;
} else if (opts) {
// got an "options" object
exports._extend(ctx, opts);
}
// set default options
if (isUndefined(ctx.showHidden)) ctx.showHidden = false;
if (isUndefined(ctx.depth)) ctx.depth = 2;
if (isUndefined(ctx.colors)) ctx.colors = false;
if (isUndefined(ctx.customInspect)) ctx.customInspect = true;
if (ctx.colors) ctx.stylize = stylizeWithColor;
return formatValue(ctx, obj, ctx.depth);
}
exports.inspect = inspect;
exclusion: function exclusion(value, arg) {
if (exists(value) && contains(arg, value, true)) {
return '%{value} is restricted';
}
},
//Date and time
maxDateTime: function maxDateTime(value, arg, options) {
if (exists(value) && !(options.exclusive ? toDateTime(value) < toDateTime(arg) : toDateTime(value) <= toDateTime(arg))) {
return 'Must be earlier than %{arg}';
}
},
// http://en.wikipedia.org/wiki/ANSI_escape_code#graphics
inspect.colors = {
'bold' : [1, 22],
'italic' : [3, 23],
'underline' : [4, 24],
'inverse' : [7, 27],
'white' : [37, 39],
'grey' : [90, 39],
'black' : [30, 39],
'blue' : [34, 39],
'cyan' : [36, 39],
'green' : [32, 39],
'magenta' : [35, 39],
'red' : [31, 39],
'yellow' : [33, 39]
};
maxDate: function maxDate(value, arg, options) {
if (exists(value) && !(options.exclusive ? toDate(value) < toDate(arg) : toDate(value) <= toDate(arg))) {
return 'Must be earlier than %{arg}';
}
},
// Don't use 'blue' not visible on cmd.exe
inspect.styles = {
'special': 'cyan',
'number': 'yellow',
'boolean': 'yellow',
'undefined': 'grey',
'null': 'bold',
'string': 'green',
'date': 'magenta',
// "name": intentionally not styling
'regexp': 'red'
};
minDateTime: function minDateTime(value, arg, options) {
if (exists(value) && !(options.exclusive ? toDateTime(value) > toDateTime(arg) : toDateTime(value) >= toDateTime(arg))) {
return 'Must be no earlier than %{arg}';
}
},
minDate: function minDate(value, arg, options) {
if (exists(value) && !(options.exclusive ? toDate(value) > toDate(arg) : toDate(value) >= toDate(arg))) {
return 'Must be no earlier than %{arg}';
}
},
function stylizeWithColor(str, styleType) {
var style = inspect.styles[styleType];
equalDateTime: function equalDateTime(value, arg) {
if (exists(value) && toDateTime(value).valueOf() !== toDateTime(arg).valueOf()) {
return 'Must be equal %{arg}';
}
},
if (style) {
return '\u001b[' + inspect.colors[style][0] + 'm' + str +
'\u001b[' + inspect.colors[style][1] + 'm';
} else {
return str;
}
}
equalDate: function equalDate(value, arg) {
if (exists(value) && toDate(value).valueOf() !== toDate(arg).valueOf()) {
return 'Must be equal %{arg}';
}
},
rangeDateTime: function rangeDateTime(value, options) {
if (exists(value)) {
if (!(options.exclusiveFrom || options.exclusive ? toDateTime(value) > toDateTime(options.from) : toDateTime(value) >= toDateTime(options.from))) {
return {
error: 'rangeDateTime.many',
message: options.manyMessage || 'Must be from %{from} to %{to}'
};
} else if (!(options.exclusiveTo || options.exclusive ? toDateTime(value) < toDateTime(options.to) : toDateTime(value) <= toDateTime(options.to))) {
return {
error: 'rangeDateTime.less',
message: options.lessMessage || 'Must be from %{from} to %{to}'
};
}
}
},
function stylizeNoColor(str, styleType) {
return str;
}
rangeDate: function rangeDate(value, options) {
if (exists(value)) {
if (!(options.exclusiveFrom || options.exclusive ? toDate(value) > toDate(options.from) : toDate(value) >= toDate(options.from))) {
return {
error: 'rangeDate.many',
message: options.manyMessage || 'Must be from %{from} to %{to}'
};
} else if (!(options.exclusiveTo || options.exclusive ? toDate(value) < toDate(options.to) : toDate(value) <= toDate(options.to))) {
return {
error: 'rangeDate.less',
message: options.lessMessage || 'Must be from %{from} to %{to}'
};
}
}
},
//Web
email: function email(value) {
var PATTERN = /^[a-z0-9\u007F-\uffff!#$%&'*+\/=?^_`{|}~-]+(?:\.[a-z0-9\u007F-\uffff!#$%&'*+\/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z]{2,}$/i;
function arrayToHash(array) {
var hash = {};
if (exists(value) && !PATTERN.exec(toString(value))) {
return 'Must be a valid email';
}
},
array.forEach(function(val, idx) {
hash[val] = true;
});
// A URL validator that is used to validate URLs with the ability to
// restrict schemes and some domains.
url: function url(value, options) {
if (exists(value)) {
var protocols = options.protocols || ['http', 'https'];
return hash;
}
// https://gist.github.com/dperini/729294
var regex = '^' +
// schemes
'(?:(?:' + protocols.join('|') + '):\\/\\/)' +
// credentials
'(?:\\S+(?::\\S*)?@)?';
regex += '(?:';
function formatValue(ctx, value, recurseTimes) {
// Provide a hook for user-specified inspect functions.
// Check that value is an object with an inspect function on it
if (ctx.customInspect &&
value &&
isFunction(value.inspect) &&
// Filter out the util module, it's inspect function is special
value.inspect !== exports.inspect &&
// Also filter out any prototype objects using the circular check.
!(value.constructor && value.constructor.prototype === value)) {
var ret = value.inspect(recurseTimes, ctx);
if (!isString(ret)) {
ret = formatValue(ctx, ret, recurseTimes);
}
return ret;
}
var tld = '(?:\\.(?:[a-z\\u00a1-\\uffff]{2,}))';
// Primitive types cannot have properties
var primitive = formatPrimitive(ctx, value);
if (primitive) {
return primitive;
}
// This ia a special case for the localhost hostname
if (options.allowLocal) {
tld += '?';
} else {
// private & local addresses
regex += '(?!10(?:\\.\\d{1,3}){3})' + '(?!127(?:\\.\\d{1,3}){3})' + '(?!169\\.254(?:\\.\\d{1,3}){2})' + '(?!192\\.168(?:\\.\\d{1,3}){2})' + '(?!172' + '\\.(?:1[6-9]|2\\d|3[0-1])' + '(?:\\.\\d{1,3})' + '{2})';
}
// Look up the keys of the object.
var keys = Object.keys(value);
var visibleKeys = arrayToHash(keys);
var hostname = '(?:(?:[a-z\\u00a1-\\uffff0-9]+-?)*[a-z\\u00a1-\\uffff0-9]+)' + '(?:\\.(?:[a-z\\u00a1-\\uffff0-9]+-?)*[a-z\\u00a1-\\uffff0-9]+)*' + tld + ')';
if (ctx.showHidden) {
keys = Object.getOwnPropertyNames(value);
}
// reserved addresses
regex += '(?:[1-9]\\d?|1\\d\\d|2[01]\\d|22[0-3])' + '(?:\\.(?:1?\\d{1,2}|2[0-4]\\d|25[0-5])){2}' + '(?:\\.(?:[1-9]\\d?|1\\d\\d|2[0-4]\\d|25[0-4]))' + '|' + hostname +
// port number
'(?::\\d{2,5})?' +
// path
'(?:\\/[^\\s]*)?' + '$';
// IE doesn't make error fields non-enumerable
// http://msdn.microsoft.com/en-us/library/ie/dww52sbt(v=vs.94).aspx
if (isError(value)
&& (keys.indexOf('message') >= 0 || keys.indexOf('description') >= 0)) {
return formatError(value);
}
var PATTERN = new RegExp(regex, 'i');
// Some type of object without properties can be shortcutted.
if (keys.length === 0) {
if (isFunction(value)) {
var name = value.name ? ': ' + value.name : '';
return ctx.stylize('[Function' + name + ']', 'special');
}
if (isRegExp(value)) {
return ctx.stylize(RegExp.prototype.toString.call(value), 'regexp');
}
if (isDate(value)) {
return ctx.stylize(Date.prototype.toString.call(value), 'date');
}
if (isError(value)) {
return formatError(value);
}
}
if (!PATTERN.exec(toString(value))) {
return 'is not a valid url';
}
}
},
var base = '', array = false, braces = ['{', '}'];
ipAddress: function ipAddress(value, options) {
if (exists(value)) {
var IPV4_REGEXP = /^\s*((([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5]))\s*$/;
var IPV6_REGEXP = /^\s*((([0-9A-Fa-f]{1,4}:){7}([0-9A-Fa-f]{1,4}|:))|(([0-9A-Fa-f]{1,4}:){6}(:[0-9A-Fa-f]{1,4}|((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3})|:))|(([0-9A-Fa-f]{1,4}:){5}(((:[0-9A-Fa-f]{1,4}){1,2})|:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3})|:))|(([0-9A-Fa-f]{1,4}:){4}(((:[0-9A-Fa-f]{1,4}){1,3})|((:[0-9A-Fa-f]{1,4})?:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){3}(((:[0-9A-Fa-f]{1,4}){1,4})|((:[0-9A-Fa-f]{1,4}){0,2}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){2}(((:[0-9A-Fa-f]{1,4}){1,5})|((:[0-9A-Fa-f]{1,4}){0,3}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){1}(((:[0-9A-Fa-f]{1,4}){1,6})|((:[0-9A-Fa-f]{1,4}){0,4}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(:(((:[0-9A-Fa-f]{1,4}){1,7})|((:[0-9A-Fa-f]{1,4}){0,5}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:)))(%.+)?\s*$/;
var HOSTNAME_REGEXP = /^\s*((?=.{1,255}$)(?=.*[A-Za-z].*)[0-9A-Za-z](?:(?:[0-9A-Za-z]|\b-){0,61}[0-9A-Za-z])?(?:\.[0-9A-Za-z](?:(?:[0-9A-Za-z]|\b-){0,61}[0-9A-Za-z])?)*)\s*$/;
// Make Array say that they are Array
if (isArray(value)) {
array = true;
braces = ['[', ']'];
}
var regExps = { ipv4: IPV4_REGEXP, ipv6: IPV6_REGEXP, hostname: HOSTNAME_REGEXP };
// Make functions say that they are functions
if (isFunction(value)) {
var n = value.name ? ': ' + value.name : '';
base = ' [Function' + n + ']';
}
var isError = !Object.keys(regExps).some(function (key) {
if (options[key] || options[key] === undefined) {
return regExps[key].test(toString(value));
}
});
// Make RegExps say that they are RegExps
if (isRegExp(value)) {
base = ' ' + RegExp.prototype.toString.call(value);
}
var ipv4 = options.ipv4;
var ipv6 = options.ipv6;
var hostname = options.hostname;
// Make dates with properties first say the date
if (isDate(value)) {
base = ' ' + Date.prototype.toUTCString.call(value);
}
if (isError) {
if (ipv4 && !ipv6 && !hostname) {
return {
error: 'ip.v4',
message: options.ipv4Message || 'Must be a valid IPv4 address'
};
}
// Make error with message first say the error
if (isError(value)) {
base = ' ' + formatError(value);
}
if (ipv6 && !ipv4 && !hostname) {
return {
error: 'ip.v6',
message: options.ipv6Message || 'Must be a valid IPv6 address'
};
}
if (keys.length === 0 && (!array || value.length == 0)) {
return braces[0] + base + braces[1];
}
if (hostname && !ipv4 && !ipv6) {
return {
error: 'ip.hostname',
message: options.hostnameMessage || 'Must be a valid hostname'
};
}
if (recurseTimes < 0) {
if (isRegExp(value)) {
return ctx.stylize(RegExp.prototype.toString.call(value), 'regexp');
} else {
return ctx.stylize('[Object]', 'special');
if (ipv6 && ipv4 && !hostname) {
return {
error: 'ip.address',
message: options.addressMessage || 'Must be a valid IP address'
};
}
return 'Must be a valid IP address or hostname';
}
}
},
//File
accept: function accept(files, arg, options) {
files = toArray(options.files || files);
if (exists(files)) {
var _ret = function () {
var allowedTypes = (arg || '').split(',').map(function (type) {
return type.trim().replace('*', '');
});
var isError = files.some(function (file) {
return allowedTypes.every(function (type) {
if (type[0] === '.') {
//extension
return '.' + ((file.name || '').split('.').pop() || '').toLowerCase() !== type;
} else {
//mime type
return (file.type || '').indexOf(type) === -1;
}
});
});
if (isError) {
return {
v: 'File must be a %{arg}'
};
}
}();
if ((typeof _ret === 'undefined' ? 'undefined' : _typeof(_ret)) === "object") return _ret.v;
}
},
minFileSize: function minFileSize(files, arg, options) {
files = toArray(options.files || files);
if (exists(files) && !files.every(function (file) {
return toNumber(file.size) >= arg;
})) {
return 'File size must be more or equal %{arg} bytes';
}
},
maxFileSize: function maxFileSize(files, arg, options) {
files = toArray(options.files || files);
if (exists(files) && !files.every(function (file) {
return toNumber(file.size) <= arg;
})) {
return 'File size must be less or equal %{arg} bytes';
}
},
equalFileSize: function equalFileSize(files, arg, options) {
files = toArray(options.files || files);
if (exists(files) && !files.every(function (file) {
return toNumber(file.size) === arg;
})) {
return 'File size must be equal %{arg} bytes';
}
},
minFileSizeAll: function minFileSizeAll(files, arg, options) {
files = toArray(options.files || files);
if (exists(files) && !(files.reduce(function (prev, curr) {
return toNumber(prev.size || prev) + toNumber(curr.size);
}) >= arg)) {
return 'Total files size must be more or equal %{arg} bytes';
}
},
maxFileSizeAll: function maxFileSizeAll(files, arg, options) {
files = toArray(options.files || files);
if (exists(files) && !(files.reduce(function (prev, curr) {
return toNumber(prev.size || prev) + toNumber(curr.size);
}) <= arg)) {
return 'Total files size must be less or equal %{arg} bytes';
}
},
equalFileSizeAll: function equalFileSizeAll(files, arg, options) {
files = toArray(options.files || files);
if (exists(files) && !(files.reduce(function (prev, curr) {
return toNumber(prev.size || prev) + toNumber(curr.size);
}) === arg)) {
return 'Total files size must be equal %{arg} bytes';
}
},
minFileNameLength: function minFileNameLength(files, arg, options) {
files = toArray(options.files || files);
if (exists(files) && files.some(function (file) {
return toArray(file.name).length < arg;
})) {
return 'File name length must be more or equal %{arg}';
}
},
maxFileNameLength: function maxFileNameLength(files, arg, options) {
files = toArray(options.files || files);
if (exists(files) && files.some(function (file) {
return toArray(file.name).length > arg;
})) {
return 'File name length must be less or equal %{arg}';
}
},
//Test
alwaysValid: function alwaysValid() {},
alwaysInvalid: function alwaysInvalid() {
return 'Any value is invalid';
}
}
};
ctx.seen.push(value);
/* Utils */
var util = {
toDateTime: toDateTime,
toDate: toDate,
isNumber: isNumber,
isFunction: isFunction,
isInteger: isInteger,
isBoolean: isBoolean,
isArray: isArray,
isDateTime: isDateTime,
isString: isString,
isObject: isObject,
isPlainObject: isPlainObject,
isDefined: isDefined,
isEmpty: isEmpty,
exists: exists,
contains: contains,
toArray: toArray,
toNumber: toNumber,
toString: toString,
toObject: toObject,
byteLength: byteLength
};
var output;
if (array) {
output = formatArray(ctx, value, recurseTimes, visibleKeys, keys);
} else {
output = keys.map(function(key) {
return formatProperty(ctx, value, recurseTimes, visibleKeys, key, array);
});
}
function toDate(date) {
return toDateTime(date, { noTime: true });
}
ctx.seen.pop();
function isNumber(value) {
return typeof value === 'number' && !isNaN(value);
}
return reduceToSingleString(output, base, braces);
function isFunction(value) {
return typeof value === 'function';
}
function isInteger(value) {
return isNumber(value) && value % 1 === 0;
}
function formatPrimitive(ctx, value) {
if (isUndefined(value))
return ctx.stylize('undefined', 'undefined');
if (isString(value)) {
var simple = '\'' + JSON.stringify(value).replace(/^"|"$/g, '')
.replace(/'/g, "\\'")
.replace(/\\"/g, '"') + '\'';
return ctx.stylize(simple, 'string');
}
if (isNumber(value))
return ctx.stylize('' + value, 'number');
if (isBoolean(value))
return ctx.stylize('' + value, 'boolean');
// For some reason typeof null is "object", so special case here.
if (isNull(value))
return ctx.stylize('null', 'null');
function isBoolean(value) {
return typeof value === 'boolean';
}
function isArray(value) {
return Array.isArray(value);
}
function formatError(value) {
return '[' + Error.prototype.toString.call(value) + ']';
function isDateTime(value) {
return !isArray(value) && !isNaN(Date.parse(value));
}
function isString(value) {
return typeof value === 'string';
}
function formatArray(ctx, value, recurseTimes, visibleKeys, keys) {
var output = [];
for (var i = 0, l = value.length; i < l; ++i) {
if (hasOwnProperty(value, String(i))) {
output.push(formatProperty(ctx, value, recurseTimes, visibleKeys,
String(i), true));
} else {
output.push('');
function isObject(obj) {
return obj === Object(obj);
}
//This is no full plain-object checking, but it is better for validation when you need to know
//that object is no array or hasn't common type. Generally you prefer to consider instance of custom class as object
function isPlainObject(value) {
return (typeof value === 'undefined' ? 'undefined' : _typeof(value)) == 'object' && value !== null && !isArray(value) && !(value instanceof RegExp) && !(value instanceof Date) && !(value instanceof Error) && !(value instanceof Number) && !(value instanceof String) && !(value instanceof Boolean) && (typeof value.toDateTime !== 'function' || value.propertyIsEnumerable('toDateTime')); //Moment.js date
}
// Returns false if the object is `null` of `undefined`
function isDefined(obj) {
return obj != null;
}
//Note! undefined is not empty
function isEmpty(value) {
if (value === null || typeof value === 'number' && isNaN(value)) {
return true;
}
}
keys.forEach(function(key) {
if (!key.match(/^\d+$/)) {
output.push(formatProperty(ctx, value, recurseTimes, visibleKeys,
key, true));
if (isString(value)) {
return (/^\s*$/.test(value)
); //Whitespace only strings are empty
}
});
return output;
if (isArray(value)) {
return value.length === 0;
}
if (isPlainObject(value)) {
//If we find at least one property we consider it non empty
for (var attr in value) {
return false;
}
return true;
}
return value instanceof Date && isNaN(Date.parse(value)); //Invalid date is empty
//Boolean, Date, RegExp, Error, Number, Function etc. are not empty
}
function exists(value) {
return value !== undefined && !isEmpty(value);
}
function formatProperty(ctx, value, recurseTimes, visibleKeys, key, array) {
var name, str, desc;
desc = Object.getOwnPropertyDescriptor(value, key) || { value: value[key] };
if (desc.get) {
if (desc.set) {
str = ctx.stylize('[Getter/Setter]', 'special');
} else {
str = ctx.stylize('[Getter]', 'special');
function contains(collection, value, some) {
some = some ? 'some' : 'every';
if (!isDefined(collection)) {
return false;
}
} else {
if (desc.set) {
str = ctx.stylize('[Setter]', 'special');
if ((typeof value === 'undefined' ? 'undefined' : _typeof(value)) === 'object') {
return toArray(value)[some](function (val) {
return contains(collection, val);
});
}
}
if (!hasOwnProperty(visibleKeys, key)) {
name = '[' + key + ']';
}
if (!str) {
if (ctx.seen.indexOf(desc.value) < 0) {
if (isNull(recurseTimes)) {
str = formatValue(ctx, desc.value, null);
} else {
str = formatValue(ctx, desc.value, recurseTimes - 1);
}
if (str.indexOf('\n') > -1) {
if (array) {
str = str.split('\n').map(function(line) {
return ' ' + line;
}).join('\n').substr(2);
} else {
str = '\n' + str.split('\n').map(function(line) {
return ' ' + line;
}).join('\n');
}
}
return toArray(collection).indexOf(value) !== -1;
}
function deepEqual(actual, expected, strict) {
if (strict !== false) {
strict = true;
}
if (actual === expected) {
return true;
} else if (actual instanceof Date && expected instanceof Date) {
return actual.getTime() === expected.getTime();
} else if (!actual || !expected || (typeof actual === 'undefined' ? 'undefined' : _typeof(actual)) != 'object' && (typeof expected === 'undefined' ? 'undefined' : _typeof(expected)) != 'object') {
return strict ? actual === expected : actual == expected;
} else {
str = ctx.stylize('[Circular]', 'special');
return objEqual(actual, expected, strict);
}
}
if (isUndefined(name)) {
if (array && key.match(/^\d+$/)) {
return str;
}
function objEqual(a, b, strict) {
var i, key;
if (!isDefined(a) || !isDefined(b)) {
return false;
}
name = JSON.stringify('' + key);
if (name.match(/^"([a-zA-Z_][a-zA-Z_0-9]*)"$/)) {
name = name.substr(1, name.length - 2);
name = ctx.stylize(name, 'name');
} else {
name = name.replace(/'/g, "\\'")
.replace(/\\"/g, '"')
.replace(/(^"|"$)/g, "'");
name = ctx.stylize(name, 'string');
if (a.prototype !== b.prototype) return false;
try {
var ka = Object.keys(a),
kb = Object.keys(b);
} catch (e) {
//happens when one is a string literal and the other isn't
return false;
}
}
return name + ': ' + str;
}
if (ka.length !== kb.length) return false;
ka.sort();
kb.sort();
function reduceToSingleString(output, base, braces) {
var numLinesEst = 0;
var length = output.reduce(function(prev, cur) {
numLinesEst++;
if (cur.indexOf('\n') >= 0) numLinesEst++;
return prev + cur.replace(/\u001b\[\d\d?m/g, '').length + 1;
}, 0);
//cheap key test
for (i = ka.length - 1; i >= 0; i--) {
if (ka[i] != kb[i]) return false;
}
if (length > 60) {
return braces[0] +
(base === '' ? '' : base + '\n ') +
' ' +
output.join(',\n ') +
' ' +
braces[1];
}
//possibly expensive deep test
for (i = ka.length - 1; i >= 0; i--) {
key = ka[i];
if (!deepEqual(a[key], b[key], strict)) return false;
}
return braces[0] + base + ' ' + output.join(', ') + ' ' + braces[1];
return (typeof a === 'undefined' ? 'undefined' : _typeof(a)) === (typeof b === 'undefined' ? 'undefined' : _typeof(b));
}
/** Used to compose unicode character classes. */
var rsAstralRange = '\\ud800-\\udfff',
rsComboMarksRange = '\\u0300-\\u036f\\ufe20-\\ufe23',
rsComboSymbolsRange = '\\u20d0-\\u20f0',
rsVarRange = '\\ufe0e\\ufe0f';
// NOTE: These type checking functions intentionally don't use `instanceof`
// because it is fragile and can be easily faked with `Object.create()`.
function isArray(ar) {
return Array.isArray(ar);
}
exports.isArray = isArray;
/** Used to compose unicode capture groups. */
var rsAstral = '[' + rsAstralRange + ']',
rsCombo = '[' + rsComboMarksRange + rsComboSymbolsRange + ']',
rsFitz = '\\ud83c[\\udffb-\\udfff]',
rsModifier = '(?:' + rsCombo + '|' + rsFitz + ')',
rsNonAstral = '[^' + rsAstralRange + ']',
rsRegional = '(?:\\ud83c[\\udde6-\\uddff]){2}',
rsSurrPair = '[\\ud800-\\udbff][\\udc00-\\udfff]',
rsZWJ = '\\u200d';
function isBoolean(arg) {
return typeof arg === 'boolean';
}
exports.isBoolean = isBoolean;
/** Used to compose unicode regexes. */
var reOptMod = rsModifier + '?',
rsOptVar = '[' + rsVarRange + ']?',
rsOptJoin = '(?:' + rsZWJ + '(?:' + [rsNonAstral, rsRegional, rsSurrPair].join('|') + ')' + rsOptVar + reOptMod + ')*',
rsSeq = rsOptVar + reOptMod + rsOptJoin,
rsSymbol = '(?:' + [rsNonAstral + rsCombo + '?', rsCombo, rsRegional, rsSurrPair, rsAstral].join('|') + ')';
function isNull(arg) {
return arg === null;
}
exports.isNull = isNull;
var reHasUnicode = RegExp('[' + rsZWJ + rsAstralRange + rsComboMarksRange + rsComboSymbolsRange + rsVarRange + ']');
var reUnicode = RegExp(rsFitz + '(?=' + rsFitz + ')|' + rsSymbol + rsSeq, 'g');
function isNullOrUndefined(arg) {
return arg == null;
}
exports.isNullOrUndefined = isNullOrUndefined;
function toArray(value) {
if (!value) {
return [];
}
function isNumber(arg) {
return typeof arg === 'number';
}
exports.isNumber = isNumber;
if (isArray(value)) {
return value;
}
function isString(arg) {
return typeof arg === 'string';
}
exports.isString = isString;
if (isString(value)) {
return reHasUnicode.test(value) ? string.match(reUnicode) || [] : value.split('');
}
function isSymbol(arg) {
return typeof arg === 'symbol';
if (Array.from && (value.length || value instanceof Map || value instanceof Set || value[Symbol && Symbol.iterator])) {
return Array.from(value);
}
return Object.keys(value);
}
exports.isSymbol = isSymbol;
function isUndefined(arg) {
return arg === void 0;
function toNumber(value) {
return Number(value);
}
exports.isUndefined = isUndefined;
function isRegExp(re) {
return isObject(re) && objectToString(re) === '[object RegExp]';
function toString(value) {
return value && !isObject(value) ? String(value) : '';
}
exports.isRegExp = isRegExp;
function isObject(arg) {
return typeof arg === 'object' && arg !== null;
function toObject(value) {
return isObject(value) ? value : {};
}
exports.isObject = isObject;
function isDate(d) {
return isObject(d) && objectToString(d) === '[object Date]';
function byteLength(str, options) {
//Note: Node.js has Buffer.byteLength()
var stringify = options && typeof options.stringify === 'function' ? options.stringify : JSON.stringify;
str = str != null ? typeof str === 'string' ? str : stringify(str) : '';
// returns the byte length of an utf8 string
var s = str.length;
for (var i = str.length - 1; i >= 0; i--) {
var code = str.charCodeAt(i);
if (code > 0x7f && code <= 0x7ff) s++;else if (code > 0x7ff && code <= 0xffff) s += 2;
}
return s;
}
exports.isDate = isDate;
function isError(e) {
return isObject(e) &&
(objectToString(e) === '[object Error]' || e instanceof Error);
/* Export */
module.exports = {
validators: validators,
util: util
};
},{"normalize-date":14}],12:[function(require,module,exports){
'use strict';
var validatorsLibrary = require('./common-validators-library');
var validators = require('validators-constructor')();
validators.util = validatorsLibrary.util;
module.exports = validators.add(validatorsLibrary.validators);
},{"./common-validators-library":11,"validators-constructor":13}],13:[function(require,module,exports){
'use strict';
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol ? "symbol" : typeof obj; };
var FORMAT_REGEXP = /(%?)%\{([^\}]+)\}/g; // Finds %{key} style patterns in the given string
var MESSAGE_REGEXP = /message/i;
var hiddenPropertySettings = {
enumerable: false,
configurable: false,
writable: true
};
var RESULT_HANDLER = 'resultHandler';
var EXCEPTION_HANDLER = 'exceptionHandler';
var ERROR_FORMAT = 'errorFormat';
var MESSAGE = 'message';
var SIMPLE_ARGS_FORMAT = 'simpleArgsFormat';
var ONE_OPTIONS_ARG = 'oneOptionsArg';
var ARG = 'arg';
/**
* Add extra advantages to validator
*
* @param {Object} validators - validators library
* @param {String} name - validator's name
* @param {Function} validator - user validator
*
* @returns {Function} extended validator
*/
function validatorWrapper(validators, name, validator) {
return function (value, options) {
var error = void 0,
args = arguments;
var alias = this && this.alias;
var validatorObj = validators[name];
var validatorAliasObj = alias ? validators[alias] : {};
var arg = validatorObj[ARG] || validatorAliasObj[ARG] || validators[ARG];
var isSimpleArgsFormat = validatorObj[SIMPLE_ARGS_FORMAT] || validatorAliasObj[SIMPLE_ARGS_FORMAT] || validators[SIMPLE_ARGS_FORMAT];
options = Object.assign({}, validatorObj.defaultOptions, validatorAliasObj.defaultOptions, options);
if (typeof options.parse === 'function') {
value = options.parse(value);
}
if (options.hasOwnProperty(arg) && !isSimpleArgsFormat) {
args = [value, options[arg]].concat(Array.prototype.slice.call(arguments, 1));
}
try {
var resultHandler = validatorObj[RESULT_HANDLER] || validatorAliasObj[RESULT_HANDLER] || validators[RESULT_HANDLER];
error = resultHandler(validator.apply(validators, args));
} catch (err) {
var exceptionHandler = validatorObj[EXCEPTION_HANDLER] || validatorAliasObj[EXCEPTION_HANDLER] || validators[EXCEPTION_HANDLER];
if (typeof exceptionHandler === 'function') {
error = exceptionHandler(err);
} else {
throw err;
}
}
function handleError(error) {
if (error) {
var _ret = function () {
var errorObj = (typeof error === 'undefined' ? 'undefined' : _typeof(error)) === 'object' ? error : null; //in case if we rewrite message in options and want to use fields from error object in the placeholders
var message = options[MESSAGE] || validatorObj[MESSAGE] || validatorAliasObj[MESSAGE];
if (message) {
error = message;
}
var formattedErrorMessage = validators.formatMessage(error, Object.assign({ validator: alias || name, value: value }, errorObj, options));
var format = validatorObj[ERROR_FORMAT] || validatorAliasObj[ERROR_FORMAT] || validators[ERROR_FORMAT];
if (format) {
if (typeof formattedErrorMessage === 'string') {
formattedErrorMessage = { message: formattedErrorMessage };
}
if (format.$options) {
format = Object.assign({}, format);
Object.keys(options).forEach(function (key) {
if (!MESSAGE_REGEXP.test(key) && typeof options[key] !== 'function') {
format[key] = options[key];
}
});
}
delete format.$options;
if (format.$origin) {
format = Object.assign({}, format, formattedErrorMessage);
}
delete format.$origin;
return {
v: validators.formatMessage(format, Object.assign({ validator: alias || name, value: value }, options, formattedErrorMessage))
};
}
return {
v: formattedErrorMessage
};
}();
if ((typeof _ret === 'undefined' ? 'undefined' : _typeof(_ret)) === "object") return _ret.v;
}
}
return (typeof error === 'undefined' ? 'undefined' : _typeof(error)) === 'object' && typeof error.then === 'function' ? error.then(handleError) : handleError(error);
};
}
exports.isError = isError;
function isFunction(arg) {
return typeof arg === 'function';
/**
* Format string with patterns
*
* @param {String} str ("I'm %{age} years old")
* @param {Object} [values] ({age: 21})
*
* @returns {String} formatted string ("I'm 21 years old")
*/
function formatStr(str, values) {
values = values || {};
if (typeof str !== 'string') {
return str;
}
return str.replace(FORMAT_REGEXP, function (m0, m1, m2) {
return m1 === '%' ? "%{" + m2 + "}" : values[m2];
});
}
exports.isFunction = isFunction;
function isPrimitive(arg) {
return arg === null ||
typeof arg === 'boolean' ||
typeof arg === 'number' ||
typeof arg === 'string' ||
typeof arg === 'symbol' || // ES6 symbol
typeof arg === 'undefined';
/**
* Check that value is plain object
*
* @param {Any} value
*
* @returns {Boolean}
*/
function isPlainObject(value) {
return {}.toString.call(value) === '[object Object]' && (typeof value.toDate !== 'function' || value.propertyIsEnumerable('toDate')); //For moment.js dates
}
exports.isPrimitive = isPrimitive;
exports.isBuffer = require('./support/isBuffer');
/**
* Validators constructor
*
* @param {Object} [params]
* @param {Object} [errorFormat] - format of validators result
* @param {Function} [formatStr] - for format message strings with patterns
* @param {Function} [resultHandler] - handle result of validation
* @param {Function|String} [exceptionHandler] - handle JS exceptions
* @param {String} [simpleArgsFormat] - any non object argument will be transformed to the `{arg: <argument>}`
* @param {String} [oneOptionsArg] - ignore second options argument
* @param {String} [arg] - name of compared value
* @param {Object} [util] - reserved for validator's libraries helpers
*
* @constructor
*/
function Validators(params) {
Object.defineProperties(this, {
errorFormat: hiddenPropertySettings,
formatStr: hiddenPropertySettings,
resultHandler: hiddenPropertySettings,
exceptionHandler: hiddenPropertySettings,
arg: hiddenPropertySettings,
simpleArgsFormat: hiddenPropertySettings,
oneOptionsArg: hiddenPropertySettings,
util: hiddenPropertySettings
});
function objectToString(o) {
return Object.prototype.toString.call(o);
this.errorFormat = {
error: '%{validator}',
message: '%{message}',
$options: true,
$origin: true
};
this.formatStr = formatStr;
this.resultHandler = function (result) {
return result;
};
this.arg = 'arg';
this.util = {};
Object.assign(this, params);
}
/**
* @param {String} name of validator
* @param {Function|String|Array} validator, alias or validators array
* @param {Object} params
*
* @returns {Validators} Validators instance
*/
function addValidator(name, validator, params) {
var _this = this;
var validators = validator instanceof Array ? validator : [validator];
var validate = void 0;
function pad(n) {
return n < 10 ? '0' + n.toString(10) : n.toString(10);
}
if (typeof validator === 'string') {
validate = function validate() /*value, arg, options*/{
return _this[validator].apply({ alias: name, _this: _this }, arguments);
};
} else {
validate = function validate(value /*arg, options*/) {
var args = Array.prototype.slice.call(arguments, 2);
var arg1 = arguments[1];
var arg2 = arguments[2];
var _this2 = this && this._this || _this;
var isSimpleArgsFormat = _this2[name][SIMPLE_ARGS_FORMAT] || _this2[SIMPLE_ARGS_FORMAT];
var isOneOptionsArg = _this2[name][ONE_OPTIONS_ARG] || _this2[ONE_OPTIONS_ARG];
var options = !isOneOptionsArg && isPlainObject(arg2) ? arg2 : {};
if (arg1 != null && typeof arg1 !== 'boolean') {
if (isPlainObject(arg1)) {
options = arg1;
} else {
options[_this2[name][ARG] || _this2[ARG]] = arg1;
var months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep',
'Oct', 'Nov', 'Dec'];
if (!isSimpleArgsFormat) {
args.shift();
}
}
}
// 26 Feb 16:19:34
function timestamp() {
var d = new Date();
var time = [pad(d.getHours()),
pad(d.getMinutes()),
pad(d.getSeconds())].join(':');
return [d.getDate(), months[d.getMonth()], time].join(' ');
for (var i = 0; i < validators.length; i++) {
var base = validators[i];
switch (typeof base === 'undefined' ? 'undefined' : _typeof(base)) {
case 'function':
validator = validatorWrapper(_this2, name, base);break;
case 'string':
validator = _this2[base];break;
case 'object':
validator = _this2[base[0]];
options = Object.assign({}, options, base[1]);
}
var error = validator.apply(this, [value, options].concat(args));
if (error) {
return error;
}
}
};
}
Object.assign(validate, params);
validate.curry = function () /*arg, options*/{
var _arguments = arguments;
//Partial application
return function (value) {
return validate.apply(_this, [value].concat(Array.prototype.slice.call(_arguments)));
};
};
_this[name] = validate;
}
/**
* @param {String|Object} validatorName or validators map like {validator1: validator1Fn, validator2: validator2Fn, ...}
* @param {Object} params for every validator
*
* @returns {Validators} Validators instance
*/
Validators.prototype.add = function (validatorName, validators, params) {
var _this3 = this;
// log is just a thin wrapper to console.log that prepends a timestamp
exports.log = function() {
console.log('%s - %s', timestamp(), exports.format.apply(exports, arguments));
if (typeof validatorName === 'string') {
addValidator.call(this, validatorName, validators, params);
} else {
Object.keys(validatorName).forEach(function (key) {
return addValidator.call(_this3, key, validatorName[key], validators);
});
}
return this;
};
/**
* Inherit the prototype methods from one constructor into another.
* Format any structure which contains pattern strings
*
* The Function.prototype.inherits from lang.js rewritten as a standalone
* function (not on Function.prototype). NOTE: If this file is to be loaded
* during bootstrapping this function needs to be rewritten using some native
* functions as prototype setup using normal JavaScript does not work as
* expected during bootstrapping (see mirror.js in r114903).
* @param {String|Object|Function} message. Object will be processed recursively. Function will be executed
* @param {Object} [values]
*
* @param {function} ctor Constructor function which needs to inherit the
* prototype.
* @param {function} superCtor Constructor function to inherit prototype from.
* @returns {String|Object} formatted string or object
*/
exports.inherits = require('inherits');
Validators.prototype.formatMessage = function (message, values) {
var _this4 = this;
exports._extend = function(origin, add) {
// Don't do anything if add isn't an object
if (!add || !isObject(add)) return origin;
if (typeof message === 'function') {
message = message(values.value, values);
}
var keys = Object.keys(add);
var i = keys.length;
while (i--) {
origin[keys[i]] = add[keys[i]];
}
return origin;
if ((typeof message === 'undefined' ? 'undefined' : _typeof(message)) === 'object') {
var formattedMessage = {};
Object.keys(message).forEach(function (key) {
return formattedMessage[_this4.formatStr(key, values)] = _this4.formatStr(message[key], values);
});
if (message[MESSAGE]) {
//Show not enumerable message of JS exception
formattedMessage[MESSAGE] = this.formatStr(message[MESSAGE], values);
}
return formattedMessage;
}
return this.formatStr(message, values);
};
function hasOwnProperty(obj, prop) {
return Object.prototype.hasOwnProperty.call(obj, prop);
module.exports = function (options) {
return new Validators(options);
};
},{}],14:[function(require,module,exports){
'use strict';
function setTimezoneOffset(date) {
date.setMinutes(date.getMinutes() - date.getTimezoneOffset());
}
}).call(this,require('_process'),typeof global !== "undefined" ? global : typeof self !== "undefined" ? self : typeof window !== "undefined" ? window : {})
},{"./support/isBuffer":14,"_process":13,"inherits":12}]},{},[11])(11)
function normalizeDateTime(date) {
if (!date) {
return new Date(date);
}
if (arguments.length > 1) {
date = Array.prototype.slice.call(arguments);
}
if (Array.isArray(date)) {
date = new (Function.prototype.bind.apply(Date, [null].concat(date)))();
}
var jsDate = new Date(date);
if (date === Object(date)) {
//Native or Moment.js date
var momentBaseDate = date.creationData && date.creationData().input;
if (!(momentBaseDate && (typeof momentBaseDate === 'number' || typeof momentBaseDate === 'string' && /:.+Z|GMT|[+-]\d\d:\d\d/.test(momentBaseDate)))) {
setTimezoneOffset(jsDate); //Any data except moment.js date from timestamp or UTC string (UTC ISO format have to contains time)
}
return jsDate;
}
if (!isNaN(jsDate) && typeof date === 'string') {
//ISO or RFC
if (date.match(/Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec/) && date.indexOf('GMT') === -1) {
//RFC without GMT
setTimezoneOffset(jsDate);
}
} else {
//Timestamp (always in UTC)
jsDate = new Date(Number(String(date).split('.').join('')));
}
return jsDate;
}
function normalizeDate(date, options) {
date = normalizeDateTime(date);
return (options || {}).noTime ? new Date(date.getUTCFullYear(), date.getUTCMonth(), date.getUTCDate()) : date;
}
module.exports = normalizeDate;
},{}]},{},[10])(10)
});

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

(function(f){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}g.validy=f()}})(function(){var define,module,exports;return function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s}({1:[function(require,module,exports){"use strict";exports.TimeoutError=require("./timeoutError")},{"./timeoutError":2}],2:[function(require,module,exports){"use strict";var util=require("util");function TimeoutError(){var message=arguments.length<=0||arguments[0]===undefined?"Validation timeout":arguments[0];Error.call(this);Error.captureStackTrace(this,this.constructor);this.name=this.constructor.name;this.message=message}util.inherits(TimeoutError,Error);module.exports=TimeoutError},{util:15}],3:[function(require,module,exports){"use strict";function flatten(object){var path=arguments.length<=1||arguments[1]===undefined?"":arguments[1];var flattenObject=arguments.length<=2||arguments[2]===undefined?{}:arguments[2];Object.keys(object).forEach(function(propertyName){var propertyValue=object[propertyName];if(propertyValue instanceof Array){flattenObject[path+propertyName]=propertyValue}else{flatten(propertyValue,path+propertyName+".",flattenObject)}});return flattenObject}module.exports=function(object){return flatten(object)}},{}],4:[function(require,module,exports){"use strict";exports.flat=require("./flat")},{"./flat":3}],5:[function(require,module,exports){"use strict";exports.hasSchemaAtLeastOneProperty=function(schema){for(var propertyName in schema){if(!propertyName.startsWith("$")){return true}}return false}},{}],6:[function(require,module,exports){"use strict";var validateObject=require("./validateObject");var errors=require("./errors");var formatters=require("./formatters");var DEFAULT_TIMEOUT=1e4;module.exports=function(object,schema){var options=arguments.length<=2||arguments[2]===undefined?{}:arguments[2];return new Promise(function(resolve,reject){var timeout=options.timeout||DEFAULT_TIMEOUT;var format=options.format;if(format&&!formatters[format]){return reject(new Error("Unknown format "+format))}var timeoutObject=setTimeout(function(){reject(new errors.TimeoutError)},timeout);validateObject(object,schema,object,[],options).then(function(result){clearTimeout(timeoutObject);if(format){var formattedResult=formatters[format](result);return resolve(formattedResult)}resolve(result)}).catch(function(err){clearTimeout(timeoutObject);reject(err)})})}},{"./errors":1,"./formatters":4,"./validateObject":7}],7:[function(require,module,exports){"use strict";var validateProperty=require("./validateProperty");module.exports=function(object,schema,originalObject,path,options){var validationPromises=[];var validationErrors={};Object.keys(schema).forEach(function(propertyName){if(propertyName.startsWith("$")){return}var propertyValue=object[propertyName];var propertyPath=path.concat(propertyName);var propertySchema=schema[propertyName];if(propertySchema instanceof Function){propertySchema=propertySchema(propertyValue,object,originalObject,propertyPath)}if(propertySchema){var validationPromise=validateProperty(propertyValue,propertySchema,object,originalObject,propertyPath,options);validationPromise.then(function(validationError){if(validationError){validationErrors[propertyName]=validationError}});validationPromises.push(validationPromise)}});return Promise.all(validationPromises).then(function(){if(Object.keys(validationErrors).length){return validationErrors}})}},{"./validateProperty":8}],8:[function(require,module,exports){"use strict";var _typeof=typeof Symbol==="function"&&typeof Symbol.iterator==="symbol"?function(obj){return typeof obj}:function(obj){return obj&&typeof Symbol==="function"&&obj.constructor===Symbol?"symbol":typeof obj};var validateValue=require("./validateValue");var utils=require("./utils");module.exports=function(value,schema,object,originalObject,path,options){var validatorsOptions=schema.$validators;if(validatorsOptions instanceof Function){validatorsOptions=validatorsOptions(value,object,originalObject,path)}var promise=void 0;if(validatorsOptions){promise=validateValue(value,validatorsOptions,object,originalObject,path,options)}else{promise=Promise.resolve()}return promise.then(function(validationErrors){if(validationErrors){return validationErrors}var validateObject=require("./validateObject");if(schema.$items||schema instanceof Array){var _ret=function(){if(!(value instanceof Array)){return{v:Promise.resolve(["Must be an array"])}}var propertiesSchema={};var itemSchema=schema.$items||schema[0];value.forEach(function(item,index){propertiesSchema[index]=itemSchema});return{v:validateObject(value,propertiesSchema,originalObject,path,options)}}();if((typeof _ret==="undefined"?"undefined":_typeof(_ret))==="object")return _ret.v}if(utils.hasSchemaAtLeastOneProperty(schema)){if(!(value instanceof Object)){return Promise.resolve(["Must be an object"])}return validateObject(value,schema,originalObject,path,options)}})}},{"./utils":5,"./validateObject":7,"./validateValue":9}],9:[function(require,module,exports){"use strict";var validators=require("./validators");module.exports=function(value,validatorsOptions,object,originalObject,path,options){var validatorsResultsPromises=[];var validationErrors=[];Object.keys(validatorsOptions).forEach(function(validatorName){var validator=validators[validatorName];if(!validator){throw new Error("Unknown validator "+validatorName)}var validatorOptions=validatorsOptions[validatorName];if(validatorOptions instanceof Function){validatorOptions=validatorOptions(value,object,originalObject,path)}if(!validatorOptions){return}var validatorResult=validator.call(validator,value,validatorOptions,object,originalObject,path);var validatorResultPromise=Promise.resolve(validatorResult);validatorResultPromise.then(function(validationError){if(validationError){validationErrors.push(validationError)}});validatorsResultsPromises.push(validatorResultPromise)});return Promise.all(validatorsResultsPromises).then(function(){if(validationErrors.length){if(options.maxPropertyErrorsCount){return validationErrors.slice(0,options.maxPropertyErrorsCount)}return validationErrors}})}},{"./validators":10}],10:[function(require,module,exports){"use strict";module.exports={}},{}],11:[function(require,module,exports){"use strict";var validate=require("./validate");validate.validators=require("./validators");validate.formatters=require("./formatters");validate.errors=require("./errors");module.exports=validate},{"./errors":1,"./formatters":4,"./validate":6,"./validators":10}],12:[function(require,module,exports){if(typeof Object.create==="function"){module.exports=function inherits(ctor,superCtor){ctor.super_=superCtor;ctor.prototype=Object.create(superCtor.prototype,{constructor:{value:ctor,enumerable:false,writable:true,configurable:true}})}}else{module.exports=function inherits(ctor,superCtor){ctor.super_=superCtor;var TempCtor=function(){};TempCtor.prototype=superCtor.prototype;ctor.prototype=new TempCtor;ctor.prototype.constructor=ctor}}},{}],13:[function(require,module,exports){var process=module.exports={};var queue=[];var draining=false;var currentQueue;var queueIndex=-1;function cleanUpNextTick(){if(!draining||!currentQueue){return}draining=false;if(currentQueue.length){queue=currentQueue.concat(queue)}else{queueIndex=-1}if(queue.length){drainQueue()}}function drainQueue(){if(draining){return}var timeout=setTimeout(cleanUpNextTick);draining=true;var len=queue.length;while(len){currentQueue=queue;queue=[];while(++queueIndex<len){if(currentQueue){currentQueue[queueIndex].run()}}queueIndex=-1;len=queue.length}currentQueue=null;draining=false;clearTimeout(timeout)}process.nextTick=function(fun){var args=new Array(arguments.length-1);if(arguments.length>1){for(var i=1;i<arguments.length;i++){args[i-1]=arguments[i]}}queue.push(new Item(fun,args));if(queue.length===1&&!draining){setTimeout(drainQueue,0)}};function Item(fun,array){this.fun=fun;this.array=array}Item.prototype.run=function(){this.fun.apply(null,this.array)};process.title="browser";process.browser=true;process.env={};process.argv=[];process.version="";process.versions={};function noop(){}process.on=noop;process.addListener=noop;process.once=noop;process.off=noop;process.removeListener=noop;process.removeAllListeners=noop;process.emit=noop;process.binding=function(name){throw new Error("process.binding is not supported")};process.cwd=function(){return"/"};process.chdir=function(dir){throw new Error("process.chdir is not supported")};process.umask=function(){return 0}},{}],14:[function(require,module,exports){module.exports=function isBuffer(arg){return arg&&typeof arg==="object"&&typeof arg.copy==="function"&&typeof arg.fill==="function"&&typeof arg.readUInt8==="function"}},{}],15:[function(require,module,exports){(function(process,global){var formatRegExp=/%[sdj%]/g;exports.format=function(f){if(!isString(f)){var objects=[];for(var i=0;i<arguments.length;i++){objects.push(inspect(arguments[i]))}return objects.join(" ")}var i=1;var args=arguments;var len=args.length;var str=String(f).replace(formatRegExp,function(x){if(x==="%%")return"%";if(i>=len)return x;switch(x){case"%s":return String(args[i++]);case"%d":return Number(args[i++]);case"%j":try{return JSON.stringify(args[i++])}catch(_){return"[Circular]"}default:return x}});for(var x=args[i];i<len;x=args[++i]){if(isNull(x)||!isObject(x)){str+=" "+x}else{str+=" "+inspect(x)}}return str};exports.deprecate=function(fn,msg){if(isUndefined(global.process)){return function(){return exports.deprecate(fn,msg).apply(this,arguments)}}if(process.noDeprecation===true){return fn}var warned=false;function deprecated(){if(!warned){if(process.throwDeprecation){throw new Error(msg)}else if(process.traceDeprecation){console.trace(msg)}else{console.error(msg)}warned=true}return fn.apply(this,arguments)}return deprecated};var debugs={};var debugEnviron;exports.debuglog=function(set){if(isUndefined(debugEnviron))debugEnviron=process.env.NODE_DEBUG||"";set=set.toUpperCase();if(!debugs[set]){if(new RegExp("\\b"+set+"\\b","i").test(debugEnviron)){var pid=process.pid;debugs[set]=function(){var msg=exports.format.apply(exports,arguments);console.error("%s %d: %s",set,pid,msg)}}else{debugs[set]=function(){}}}return debugs[set]};function inspect(obj,opts){var ctx={seen:[],stylize:stylizeNoColor};if(arguments.length>=3)ctx.depth=arguments[2];if(arguments.length>=4)ctx.colors=arguments[3];if(isBoolean(opts)){ctx.showHidden=opts}else if(opts){exports._extend(ctx,opts)}if(isUndefined(ctx.showHidden))ctx.showHidden=false;if(isUndefined(ctx.depth))ctx.depth=2;if(isUndefined(ctx.colors))ctx.colors=false;if(isUndefined(ctx.customInspect))ctx.customInspect=true;if(ctx.colors)ctx.stylize=stylizeWithColor;return formatValue(ctx,obj,ctx.depth)}exports.inspect=inspect;inspect.colors={bold:[1,22],italic:[3,23],underline:[4,24],inverse:[7,27],white:[37,39],grey:[90,39],black:[30,39],blue:[34,39],cyan:[36,39],green:[32,39],magenta:[35,39],red:[31,39],yellow:[33,39]};inspect.styles={special:"cyan",number:"yellow","boolean":"yellow",undefined:"grey","null":"bold",string:"green",date:"magenta",regexp:"red"};function stylizeWithColor(str,styleType){var style=inspect.styles[styleType];if(style){return"["+inspect.colors[style][0]+"m"+str+"["+inspect.colors[style][1]+"m"}else{return str}}function stylizeNoColor(str,styleType){return str}function arrayToHash(array){var hash={};array.forEach(function(val,idx){hash[val]=true});return hash}function formatValue(ctx,value,recurseTimes){if(ctx.customInspect&&value&&isFunction(value.inspect)&&value.inspect!==exports.inspect&&!(value.constructor&&value.constructor.prototype===value)){var ret=value.inspect(recurseTimes,ctx);if(!isString(ret)){ret=formatValue(ctx,ret,recurseTimes)}return ret}var primitive=formatPrimitive(ctx,value);if(primitive){return primitive}var keys=Object.keys(value);var visibleKeys=arrayToHash(keys);if(ctx.showHidden){keys=Object.getOwnPropertyNames(value)}if(isError(value)&&(keys.indexOf("message")>=0||keys.indexOf("description")>=0)){return formatError(value)}if(keys.length===0){if(isFunction(value)){var name=value.name?": "+value.name:"";return ctx.stylize("[Function"+name+"]","special")}if(isRegExp(value)){return ctx.stylize(RegExp.prototype.toString.call(value),"regexp")}if(isDate(value)){return ctx.stylize(Date.prototype.toString.call(value),"date")}if(isError(value)){return formatError(value)}}var base="",array=false,braces=["{","}"];if(isArray(value)){array=true;braces=["[","]"]}if(isFunction(value)){var n=value.name?": "+value.name:"";base=" [Function"+n+"]"}if(isRegExp(value)){base=" "+RegExp.prototype.toString.call(value)}if(isDate(value)){base=" "+Date.prototype.toUTCString.call(value)}if(isError(value)){base=" "+formatError(value)}if(keys.length===0&&(!array||value.length==0)){return braces[0]+base+braces[1]}if(recurseTimes<0){if(isRegExp(value)){return ctx.stylize(RegExp.prototype.toString.call(value),"regexp")}else{return ctx.stylize("[Object]","special")}}ctx.seen.push(value);var output;if(array){output=formatArray(ctx,value,recurseTimes,visibleKeys,keys)}else{output=keys.map(function(key){return formatProperty(ctx,value,recurseTimes,visibleKeys,key,array)})}ctx.seen.pop();return reduceToSingleString(output,base,braces)}function formatPrimitive(ctx,value){if(isUndefined(value))return ctx.stylize("undefined","undefined");if(isString(value)){var simple="'"+JSON.stringify(value).replace(/^"|"$/g,"").replace(/'/g,"\\'").replace(/\\"/g,'"')+"'";return ctx.stylize(simple,"string")}if(isNumber(value))return ctx.stylize(""+value,"number");if(isBoolean(value))return ctx.stylize(""+value,"boolean");if(isNull(value))return ctx.stylize("null","null")}function formatError(value){return"["+Error.prototype.toString.call(value)+"]"}function formatArray(ctx,value,recurseTimes,visibleKeys,keys){var output=[];for(var i=0,l=value.length;i<l;++i){if(hasOwnProperty(value,String(i))){output.push(formatProperty(ctx,value,recurseTimes,visibleKeys,String(i),true))}else{output.push("")}}keys.forEach(function(key){if(!key.match(/^\d+$/)){output.push(formatProperty(ctx,value,recurseTimes,visibleKeys,key,true))}});return output}function formatProperty(ctx,value,recurseTimes,visibleKeys,key,array){var name,str,desc;desc=Object.getOwnPropertyDescriptor(value,key)||{value:value[key]};if(desc.get){if(desc.set){str=ctx.stylize("[Getter/Setter]","special")}else{str=ctx.stylize("[Getter]","special")}}else{if(desc.set){str=ctx.stylize("[Setter]","special")}}if(!hasOwnProperty(visibleKeys,key)){name="["+key+"]"}if(!str){if(ctx.seen.indexOf(desc.value)<0){if(isNull(recurseTimes)){str=formatValue(ctx,desc.value,null)}else{str=formatValue(ctx,desc.value,recurseTimes-1)}if(str.indexOf("\n")>-1){if(array){str=str.split("\n").map(function(line){return" "+line}).join("\n").substr(2)}else{str="\n"+str.split("\n").map(function(line){return" "+line}).join("\n")}}}else{str=ctx.stylize("[Circular]","special")}}if(isUndefined(name)){if(array&&key.match(/^\d+$/)){return str}name=JSON.stringify(""+key);if(name.match(/^"([a-zA-Z_][a-zA-Z_0-9]*)"$/)){name=name.substr(1,name.length-2);name=ctx.stylize(name,"name")}else{name=name.replace(/'/g,"\\'").replace(/\\"/g,'"').replace(/(^"|"$)/g,"'");name=ctx.stylize(name,"string")}}return name+": "+str}function reduceToSingleString(output,base,braces){var numLinesEst=0;var length=output.reduce(function(prev,cur){numLinesEst++;if(cur.indexOf("\n")>=0)numLinesEst++;return prev+cur.replace(/\u001b\[\d\d?m/g,"").length+1},0);if(length>60){return braces[0]+(base===""?"":base+"\n ")+" "+output.join(",\n ")+" "+braces[1]}return braces[0]+base+" "+output.join(", ")+" "+braces[1]}function isArray(ar){return Array.isArray(ar)}exports.isArray=isArray;function isBoolean(arg){return typeof arg==="boolean"}exports.isBoolean=isBoolean;function isNull(arg){return arg===null}exports.isNull=isNull;function isNullOrUndefined(arg){return arg==null}exports.isNullOrUndefined=isNullOrUndefined;function isNumber(arg){return typeof arg==="number"}exports.isNumber=isNumber;function isString(arg){return typeof arg==="string"}exports.isString=isString;function isSymbol(arg){return typeof arg==="symbol"}exports.isSymbol=isSymbol;function isUndefined(arg){return arg===void 0}exports.isUndefined=isUndefined;function isRegExp(re){return isObject(re)&&objectToString(re)==="[object RegExp]"}exports.isRegExp=isRegExp;function isObject(arg){return typeof arg==="object"&&arg!==null}exports.isObject=isObject;function isDate(d){return isObject(d)&&objectToString(d)==="[object Date]"}exports.isDate=isDate;function isError(e){return isObject(e)&&(objectToString(e)==="[object Error]"||e instanceof Error)}exports.isError=isError;function isFunction(arg){return typeof arg==="function"}exports.isFunction=isFunction;function isPrimitive(arg){return arg===null||typeof arg==="boolean"||typeof arg==="number"||typeof arg==="string"||typeof arg==="symbol"||typeof arg==="undefined"}exports.isPrimitive=isPrimitive;exports.isBuffer=require("./support/isBuffer");function objectToString(o){return Object.prototype.toString.call(o)}function pad(n){return n<10?"0"+n.toString(10):n.toString(10)}var months=["Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"];function timestamp(){var d=new Date;var time=[pad(d.getHours()),pad(d.getMinutes()),pad(d.getSeconds())].join(":");return[d.getDate(),months[d.getMonth()],time].join(" ")}exports.log=function(){console.log("%s - %s",timestamp(),exports.format.apply(exports,arguments))};exports.inherits=require("inherits");exports._extend=function(origin,add){if(!add||!isObject(add))return origin;var keys=Object.keys(add);var i=keys.length;while(i--){origin[keys[i]]=add[keys[i]]}return origin};function hasOwnProperty(obj,prop){return Object.prototype.hasOwnProperty.call(obj,prop)}}).call(this,require("_process"),typeof global!=="undefined"?global:typeof self!=="undefined"?self:typeof window!=="undefined"?window:{})},{"./support/isBuffer":14,_process:13,inherits:12}]},{},[11])(11)});
(function(f){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}g.validy=f()}})(function(){var define,module,exports;return function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s}({1:[function(require,module,exports){"use strict";exports.flat=require("./formatters/flat");exports.nested=require("./formatters/nested")},{"./formatters/flat":2,"./formatters/nested":3}],2:[function(require,module,exports){"use strict";function flatten(object){var path=arguments.length>1&&arguments[1]!==undefined?arguments[1]:"";var flatObject=arguments.length>2&&arguments[2]!==undefined?arguments[2]:{};Object.keys(object).forEach(function(propertyName){var propertyValue=object[propertyName];if(propertyValue instanceof Object&&!(propertyValue instanceof Array)){flatten(propertyValue,path+propertyName+".",flatObject)}else{flatObject[path+propertyName]=propertyValue}});return flatObject}module.exports=function(object){return flatten(object)}},{}],3:[function(require,module,exports){"use strict";module.exports=function(errors){return errors}},{}],4:[function(require,module,exports){"use strict";var validateObject=require("./validateObject");var ValidationError=require("./validationError");var formatters=require("./formatters");var DEFAULT_OPTIONS={format:"flat",reject:false};module.exports=function(object,schema){var options=arguments.length>2&&arguments[2]!==undefined?arguments[2]:{};options=Object.assign({},DEFAULT_OPTIONS,options);return new Promise(function(resolve,reject){var formatter=formatters[options.format];if(!formatter){return reject(new Error("Unknown format "+options.format))}validateObject(object,schema,object,[]).then(function(validationErrors){if(!validationErrors){return resolve()}var formattedErrors=formatter(validationErrors);if(options.reject){reject(new ValidationError(formattedErrors))}else{resolve(formattedErrors)}}).catch(reject)})}},{"./formatters":1,"./validateObject":5,"./validationError":8}],5:[function(require,module,exports){"use strict";var validateProperty=require("./validateProperty");module.exports=function(object,schema,fullObject,path){var validationPromises=[];var validationErrors={};Object.keys(schema).forEach(function(propertyName){if(propertyName.startsWith("$")){return}var propertyValue=object[propertyName];var propertyPath=path.concat(propertyName);var propertySchema=schema[propertyName];if(propertySchema instanceof Function){propertySchema=propertySchema(propertyValue,object,fullObject,propertyPath)}if(propertySchema instanceof Object){var validationPromise=validateProperty(propertyValue,propertySchema,object,fullObject,propertyPath);validationPromise.then(function(validationError){if(validationError){validationErrors[propertyName]=validationError}});validationPromises.push(validationPromise)}});return Promise.all(validationPromises).then(function(){if(Object.keys(validationErrors).length){return validationErrors}})}},{"./validateProperty":6}],6:[function(require,module,exports){"use strict";var validateValue=require("./validateValue");module.exports=function(value,schema,object,fullObject,path){var validatorsOptions=schema.$validate;if(validatorsOptions instanceof Function){validatorsOptions=validatorsOptions(value,object,fullObject,path)}var promise=validatorsOptions instanceof Object?validateValue(value,validatorsOptions,object,fullObject,path):Promise.resolve();return promise.then(function(validationErrors){if(validationErrors){return validationErrors}var validateObject=require("./validateObject");if(schema.$items||schema[0]){if(!(value instanceof Array)){return Promise.resolve([{error:"array",message:"must be an array"}])}var propertiesSchema={};var itemSchema=schema.$items||schema[0];value.forEach(function(item,index){return propertiesSchema[index]=itemSchema});return validateObject(value,propertiesSchema,fullObject,path)}if(Object.keys(schema).some(function(propertyName){return!propertyName.startsWith("$")})){if(!(value instanceof Object)){return Promise.resolve([{error:"object",message:"must be an object"}])}return validateObject(value,schema,fullObject,path)}})}},{"./validateObject":5,"./validateValue":7}],7:[function(require,module,exports){"use strict";var validators=require("./validators");module.exports=function(value,validatorsOptions,object,fullObject,path){var validatorsResultsPromises=[];var validationErrors=[];Object.keys(validatorsOptions).forEach(function(validatorName){var validator=validators[validatorName];if(!validator){throw new Error("Unknown validator "+validatorName)}var validatorOptions=validatorsOptions[validatorName];if(validatorOptions instanceof Function){validatorOptions=validatorOptions(value,object,fullObject,path)}if(validatorOptions===false||validatorOptions===null||validatorOptions===undefined){return}var validatorResult=validator(value,validatorOptions,object,fullObject,path);var validatorResultPromise=Promise.resolve(validatorResult);validatorResultPromise.then(function(validationError){if(validationError){validationErrors.push(validationError)}});validatorsResultsPromises.push(validatorResultPromise)});return Promise.all(validatorsResultsPromises).then(function(){if(validationErrors.length){return validationErrors}})}},{"./validators":9}],8:[function(require,module,exports){"use strict";function _classCallCheck(instance,Constructor){if(!(instance instanceof Constructor)){throw new TypeError("Cannot call a class as a function")}}function _possibleConstructorReturn(self,call){if(!self){throw new ReferenceError("this hasn't been initialised - super() hasn't been called")}return call&&(typeof call==="object"||typeof call==="function")?call:self}function _inherits(subClass,superClass){if(typeof superClass!=="function"&&superClass!==null){throw new TypeError("Super expression must either be null or a function, not "+typeof superClass)}subClass.prototype=Object.create(superClass&&superClass.prototype,{constructor:{value:subClass,enumerable:false,writable:true,configurable:true}});if(superClass)Object.setPrototypeOf?Object.setPrototypeOf(subClass,superClass):subClass.__proto__=superClass}function _extendableBuiltin(cls){function ExtendableBuiltin(){var instance=Reflect.construct(cls,Array.from(arguments));Object.setPrototypeOf(instance,Object.getPrototypeOf(this));return instance}ExtendableBuiltin.prototype=Object.create(cls.prototype,{constructor:{value:cls,enumerable:false,writable:true,configurable:true}});if(Object.setPrototypeOf){Object.setPrototypeOf(ExtendableBuiltin,cls)}else{ExtendableBuiltin.__proto__=cls}return ExtendableBuiltin}var ValidationError=function(_extendableBuiltin2){_inherits(ValidationError,_extendableBuiltin2);function ValidationError(errors){_classCallCheck(this,ValidationError);var _this=_possibleConstructorReturn(this,(ValidationError.__proto__||Object.getPrototypeOf(ValidationError)).call(this));if(Error.captureStackTrace){Error.captureStackTrace(_this,_this.constructor)}_this.name=_this.constructor.name;_this.errors=errors;return _this}return ValidationError}(_extendableBuiltin(Error));module.exports=ValidationError},{}],9:[function(require,module,exports){"use strict";var validators=require("common-validators");validators.oneOptionsArg=true;var originalAdd=validators.add;validators.add=function(validatorName,validator){if(validatorName instanceof Object){return originalAdd.call(validators,validatorName,{simpleArgsFormat:true})}originalAdd.call(validators,validatorName,validator,{simpleArgsFormat:true})};validators.confirmOriginal=validators.confirm;validators.add({confirm:function confirm(value,options,object,fullObject,path){var confirmOptions={key:path[path.length-1],comparedKey:options instanceof Object?options.key:options};return this.confirmOriginal(object,confirmOptions)}});module.exports=validators},{"common-validators":12}],10:[function(require,module,exports){"use strict";var validate=require("./validate");validate.validators=require("./validators");validate.formatters=require("./formatters");validate.ValidationError=require("./validationError");module.exports=validate},{"./formatters":1,"./validate":4,"./validationError":8,"./validators":9}],11:[function(require,module,exports){"use strict";var _typeof=typeof Symbol==="function"&&typeof Symbol.iterator==="symbol"?function(obj){return typeof obj}:function(obj){return obj&&typeof Symbol==="function"&&obj.constructor===Symbol?"symbol":typeof obj};var toDateTime=require("normalize-date");var validators={custom:function custom(value,arg,options){if(typeof arg==="function"){return arg(value,options)}},required:function required(value){if(!exists(value)){return"Is required"}},presence:"required",notEmpty:function notEmpty(value){if(isEmpty(value)){return"Can't be blank"}},equal:function equal(value,arg,options){if(exists(value)&&!deepEqual(value,arg,options.strict)){return"Must be equal %{arg}"}},confirm:function confirm(value,options){if(exists(value)&&!deepEqual(toObject(value)[options.key],toObject(value)[options.comparedKey],options.strict)){return"%{key} must be equal %{comparedKey}"}},object:function object(value){if(!isPlainObject(value)){return"Must be an object"}},array:function array(value){if(!isArray(value)){return"Must be an array"}},string:function string(value){if(!isString(value)){return"Must be a string"}},number:function number(value){if(!isNumber(value)){return"Must be a number"}},integer:function integer(value){if(!isInteger(value)){return"Must be an integer"}},date:function date(value){if(!isDateTime(value)){return"Must be a valid date"}},boolean:function boolean(value){if(!isBoolean(value)){return"Must be a boolean"}},function:function _function(value){if(!isFunction(value)){return"Must be a function"}},null:function _null(value){if(value!==null){return"Must be a null"}},max:function max(value,arg,options){if(exists(value)&&!(options.exclusive?toNumber(value)<arg:toNumber(value)<=arg)){return options.exclusive?"Must be less %{arg}":"Must be less or equal %{arg}"}},min:function min(value,arg,options){if(exists(value)&&!(options.exclusive?toNumber(value)>arg:toNumber(value)>=arg)){return options.exclusive?"Must be more %{arg}":"Must be more or equal %{arg}"}},range:function range(value,options){if(exists(value)){if(!(options.exclusiveFrom||options.exclusive?toNumber(value)>options.from:toNumber(value)>=options.from)){return{error:"range.less",message:options.lessMessage||"Must be from %{from} to %{to}"}}else if(!(options.exclusiveTo||options.exclusive?toNumber(value)<options.to:toNumber(value)<=options.to)){return{error:"range.many",message:options.manyMessage||"Must be from %{from} to %{to}"}}}},odd:function odd(value){if(exists(value)&&toNumber(value)%2!==1){return"Must be odd"}},even:function even(value){if(exists(value)&&toNumber(value)%2!==0){return"Must be even"}},divisible:function divisible(value,arg){if(exists(value)&&toNumber(value)%arg!==0){return"Must be divisible by %{arg}"}},maxLength:function maxLength(value,arg){if(exists(value)&&toArray(value).length>arg){return"Length must be less or equal %{arg}"}},minLength:function minLength(value,arg){if(exists(value)&&toArray(value).length<arg){return"Length must be more or equal %{arg}"}},equalLength:function equalLength(value,arg){if(exists(value)&&toArray(value).length!==arg){return"Length must be equal %{arg}"}},rangeLength:function rangeLength(value,options){if(exists(value)){if(toArray(value).length>options.to){return{error:"rangeLength.many",message:options.manyMessage||"Length must be from %{from} to %{to}"}}else if(toArray(value).length<options.from){return{error:"rangeLength.less",message:options.lessMessage||"Length must be from %{from} to %{to}"}}}},maxSize:function maxSize(value,arg,options){var valueSize=byteLength(value,options);if(exists(value)&&valueSize>arg){return{message:"Size must be less %{arg}",size:valueSize}}},minSize:function minSize(value,arg,options){var valueSize=byteLength(value,options);if(exists(value)&&valueSize<arg){return{message:"Size must be more %{arg}",size:valueSize}}},equalSize:function equalSize(value,arg,options){var valueSize=byteLength(value,options);if(exists(value)&&valueSize!==arg){return{message:"Length must be equal %{arg}",size:valueSize}}},rangeSize:function rangeSize(value,options){var valueSize=byteLength(value,options);if(exists(value)){if(valueSize<options.from){return{error:"rangeSize.less",message:options.lessMessage||"Size must be from %{from} to %{to}",size:valueSize}}else if(valueSize>options.to){return{error:"rangeSize.many",message:options.manyMessage||"Size must be from %{from} to %{to}",size:valueSize}}}},pattern:function pattern(value,arg){if(exists(value)&&!new RegExp(arg).test(toString(value))){return"Does not match the pattern %{arg}"}},inclusion:function inclusion(value,arg){if(exists(value)&&!contains(arg,value)){return"%{value} is not allowed"}},exclusion:function exclusion(value,arg){if(exists(value)&&contains(arg,value,true)){return"%{value} is restricted"}},maxDateTime:function maxDateTime(value,arg,options){if(exists(value)&&!(options.exclusive?toDateTime(value)<toDateTime(arg):toDateTime(value)<=toDateTime(arg))){return"Must be earlier than %{arg}"}},maxDate:function maxDate(value,arg,options){if(exists(value)&&!(options.exclusive?toDate(value)<toDate(arg):toDate(value)<=toDate(arg))){return"Must be earlier than %{arg}"}},minDateTime:function minDateTime(value,arg,options){if(exists(value)&&!(options.exclusive?toDateTime(value)>toDateTime(arg):toDateTime(value)>=toDateTime(arg))){return"Must be no earlier than %{arg}"}},minDate:function minDate(value,arg,options){if(exists(value)&&!(options.exclusive?toDate(value)>toDate(arg):toDate(value)>=toDate(arg))){return"Must be no earlier than %{arg}"}},equalDateTime:function equalDateTime(value,arg){if(exists(value)&&toDateTime(value).valueOf()!==toDateTime(arg).valueOf()){return"Must be equal %{arg}"}},equalDate:function equalDate(value,arg){if(exists(value)&&toDate(value).valueOf()!==toDate(arg).valueOf()){return"Must be equal %{arg}"}},rangeDateTime:function rangeDateTime(value,options){if(exists(value)){if(!(options.exclusiveFrom||options.exclusive?toDateTime(value)>toDateTime(options.from):toDateTime(value)>=toDateTime(options.from))){return{error:"rangeDateTime.many",message:options.manyMessage||"Must be from %{from} to %{to}"}}else if(!(options.exclusiveTo||options.exclusive?toDateTime(value)<toDateTime(options.to):toDateTime(value)<=toDateTime(options.to))){return{error:"rangeDateTime.less",message:options.lessMessage||"Must be from %{from} to %{to}"}}}},rangeDate:function rangeDate(value,options){if(exists(value)){if(!(options.exclusiveFrom||options.exclusive?toDate(value)>toDate(options.from):toDate(value)>=toDate(options.from))){return{error:"rangeDate.many",message:options.manyMessage||"Must be from %{from} to %{to}"}}else if(!(options.exclusiveTo||options.exclusive?toDate(value)<toDate(options.to):toDate(value)<=toDate(options.to))){return{error:"rangeDate.less",message:options.lessMessage||"Must be from %{from} to %{to}"}}}},email:function email(value){var PATTERN=/^[a-z0-9\u007F-\uffff!#$%&'*+\/=?^_`{|}~-]+(?:\.[a-z0-9\u007F-\uffff!#$%&'*+\/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z]{2,}$/i;if(exists(value)&&!PATTERN.exec(toString(value))){return"Must be a valid email"}},url:function url(value,options){if(exists(value)){var protocols=options.protocols||["http","https"];var regex="^"+"(?:(?:"+protocols.join("|")+"):\\/\\/)"+"(?:\\S+(?::\\S*)?@)?";regex+="(?:";var tld="(?:\\.(?:[a-z\\u00a1-\\uffff]{2,}))";if(options.allowLocal){tld+="?"}else{regex+="(?!10(?:\\.\\d{1,3}){3})"+"(?!127(?:\\.\\d{1,3}){3})"+"(?!169\\.254(?:\\.\\d{1,3}){2})"+"(?!192\\.168(?:\\.\\d{1,3}){2})"+"(?!172"+"\\.(?:1[6-9]|2\\d|3[0-1])"+"(?:\\.\\d{1,3})"+"{2})"}var hostname="(?:(?:[a-z\\u00a1-\\uffff0-9]+-?)*[a-z\\u00a1-\\uffff0-9]+)"+"(?:\\.(?:[a-z\\u00a1-\\uffff0-9]+-?)*[a-z\\u00a1-\\uffff0-9]+)*"+tld+")";regex+="(?:[1-9]\\d?|1\\d\\d|2[01]\\d|22[0-3])"+"(?:\\.(?:1?\\d{1,2}|2[0-4]\\d|25[0-5])){2}"+"(?:\\.(?:[1-9]\\d?|1\\d\\d|2[0-4]\\d|25[0-4]))"+"|"+hostname+"(?::\\d{2,5})?"+"(?:\\/[^\\s]*)?"+"$";var PATTERN=new RegExp(regex,"i");if(!PATTERN.exec(toString(value))){return"is not a valid url"}}},ipAddress:function ipAddress(value,options){if(exists(value)){var IPV4_REGEXP=/^\s*((([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5]))\s*$/;var IPV6_REGEXP=/^\s*((([0-9A-Fa-f]{1,4}:){7}([0-9A-Fa-f]{1,4}|:))|(([0-9A-Fa-f]{1,4}:){6}(:[0-9A-Fa-f]{1,4}|((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3})|:))|(([0-9A-Fa-f]{1,4}:){5}(((:[0-9A-Fa-f]{1,4}){1,2})|:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3})|:))|(([0-9A-Fa-f]{1,4}:){4}(((:[0-9A-Fa-f]{1,4}){1,3})|((:[0-9A-Fa-f]{1,4})?:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){3}(((:[0-9A-Fa-f]{1,4}){1,4})|((:[0-9A-Fa-f]{1,4}){0,2}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){2}(((:[0-9A-Fa-f]{1,4}){1,5})|((:[0-9A-Fa-f]{1,4}){0,3}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){1}(((:[0-9A-Fa-f]{1,4}){1,6})|((:[0-9A-Fa-f]{1,4}){0,4}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(:(((:[0-9A-Fa-f]{1,4}){1,7})|((:[0-9A-Fa-f]{1,4}){0,5}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:)))(%.+)?\s*$/;var HOSTNAME_REGEXP=/^\s*((?=.{1,255}$)(?=.*[A-Za-z].*)[0-9A-Za-z](?:(?:[0-9A-Za-z]|\b-){0,61}[0-9A-Za-z])?(?:\.[0-9A-Za-z](?:(?:[0-9A-Za-z]|\b-){0,61}[0-9A-Za-z])?)*)\s*$/;var regExps={ipv4:IPV4_REGEXP,ipv6:IPV6_REGEXP,hostname:HOSTNAME_REGEXP};var isError=!Object.keys(regExps).some(function(key){if(options[key]||options[key]===undefined){return regExps[key].test(toString(value))}});var ipv4=options.ipv4;var ipv6=options.ipv6;var hostname=options.hostname;if(isError){if(ipv4&&!ipv6&&!hostname){return{error:"ip.v4",message:options.ipv4Message||"Must be a valid IPv4 address"}}if(ipv6&&!ipv4&&!hostname){return{error:"ip.v6",message:options.ipv6Message||"Must be a valid IPv6 address"}}if(hostname&&!ipv4&&!ipv6){return{error:"ip.hostname",message:options.hostnameMessage||"Must be a valid hostname"}}if(ipv6&&ipv4&&!hostname){return{error:"ip.address",message:options.addressMessage||"Must be a valid IP address"}}return"Must be a valid IP address or hostname"}}},accept:function accept(files,arg,options){files=toArray(options.files||files);if(exists(files)){var _ret=function(){var allowedTypes=(arg||"").split(",").map(function(type){return type.trim().replace("*","")});var isError=files.some(function(file){return allowedTypes.every(function(type){if(type[0]==="."){return"."+((file.name||"").split(".").pop()||"").toLowerCase()!==type}else{return(file.type||"").indexOf(type)===-1}})});if(isError){return{v:"File must be a %{arg}"}}}();if((typeof _ret==="undefined"?"undefined":_typeof(_ret))==="object")return _ret.v}},minFileSize:function minFileSize(files,arg,options){files=toArray(options.files||files);if(exists(files)&&!files.every(function(file){return toNumber(file.size)>=arg})){return"File size must be more or equal %{arg} bytes"}},maxFileSize:function maxFileSize(files,arg,options){files=toArray(options.files||files);if(exists(files)&&!files.every(function(file){return toNumber(file.size)<=arg})){return"File size must be less or equal %{arg} bytes"}},equalFileSize:function equalFileSize(files,arg,options){files=toArray(options.files||files);if(exists(files)&&!files.every(function(file){return toNumber(file.size)===arg})){return"File size must be equal %{arg} bytes"}},minFileSizeAll:function minFileSizeAll(files,arg,options){files=toArray(options.files||files);if(exists(files)&&!(files.reduce(function(prev,curr){return toNumber(prev.size||prev)+toNumber(curr.size)})>=arg)){return"Total files size must be more or equal %{arg} bytes"}},maxFileSizeAll:function maxFileSizeAll(files,arg,options){files=toArray(options.files||files);if(exists(files)&&!(files.reduce(function(prev,curr){return toNumber(prev.size||prev)+toNumber(curr.size)})<=arg)){return"Total files size must be less or equal %{arg} bytes"}},equalFileSizeAll:function equalFileSizeAll(files,arg,options){files=toArray(options.files||files);if(exists(files)&&!(files.reduce(function(prev,curr){return toNumber(prev.size||prev)+toNumber(curr.size)})===arg)){return"Total files size must be equal %{arg} bytes"}},minFileNameLength:function minFileNameLength(files,arg,options){files=toArray(options.files||files);if(exists(files)&&files.some(function(file){return toArray(file.name).length<arg})){return"File name length must be more or equal %{arg}"}},maxFileNameLength:function maxFileNameLength(files,arg,options){files=toArray(options.files||files);if(exists(files)&&files.some(function(file){return toArray(file.name).length>arg})){return"File name length must be less or equal %{arg}"}},alwaysValid:function alwaysValid(){},alwaysInvalid:function alwaysInvalid(){return"Any value is invalid"}};var util={toDateTime:toDateTime,toDate:toDate,isNumber:isNumber,isFunction:isFunction,isInteger:isInteger,isBoolean:isBoolean,isArray:isArray,isDateTime:isDateTime,isString:isString,isObject:isObject,isPlainObject:isPlainObject,isDefined:isDefined,isEmpty:isEmpty,exists:exists,contains:contains,toArray:toArray,toNumber:toNumber,toString:toString,toObject:toObject,byteLength:byteLength};function toDate(date){return toDateTime(date,{noTime:true})}function isNumber(value){return typeof value==="number"&&!isNaN(value)}function isFunction(value){return typeof value==="function"}function isInteger(value){return isNumber(value)&&value%1===0}function isBoolean(value){return typeof value==="boolean"}function isArray(value){return Array.isArray(value)}function isDateTime(value){return!isArray(value)&&!isNaN(Date.parse(value))}function isString(value){return typeof value==="string"}function isObject(obj){return obj===Object(obj)}function isPlainObject(value){return(typeof value==="undefined"?"undefined":_typeof(value))=="object"&&value!==null&&!isArray(value)&&!(value instanceof RegExp)&&!(value instanceof Date)&&!(value instanceof Error)&&!(value instanceof Number)&&!(value instanceof String)&&!(value instanceof Boolean)&&(typeof value.toDateTime!=="function"||value.propertyIsEnumerable("toDateTime"))}function isDefined(obj){return obj!=null}function isEmpty(value){if(value===null||typeof value==="number"&&isNaN(value)){return true}if(isString(value)){return/^\s*$/.test(value)}if(isArray(value)){return value.length===0}if(isPlainObject(value)){for(var attr in value){return false}return true}return value instanceof Date&&isNaN(Date.parse(value))}function exists(value){return value!==undefined&&!isEmpty(value)}function contains(collection,value,some){some=some?"some":"every";if(!isDefined(collection)){return false}if((typeof value==="undefined"?"undefined":_typeof(value))==="object"){return toArray(value)[some](function(val){return contains(collection,val)})}return toArray(collection).indexOf(value)!==-1}function deepEqual(actual,expected,strict){if(strict!==false){strict=true}if(actual===expected){return true}else if(actual instanceof Date&&expected instanceof Date){return actual.getTime()===expected.getTime()}else if(!actual||!expected||(typeof actual==="undefined"?"undefined":_typeof(actual))!="object"&&(typeof expected==="undefined"?"undefined":_typeof(expected))!="object"){return strict?actual===expected:actual==expected}else{return objEqual(actual,expected,strict)}}function objEqual(a,b,strict){var i,key;if(!isDefined(a)||!isDefined(b)){return false}if(a.prototype!==b.prototype)return false;try{var ka=Object.keys(a),kb=Object.keys(b)}catch(e){return false}if(ka.length!==kb.length)return false;ka.sort();kb.sort();for(i=ka.length-1;i>=0;i--){if(ka[i]!=kb[i])return false}for(i=ka.length-1;i>=0;i--){key=ka[i];if(!deepEqual(a[key],b[key],strict))return false}return(typeof a==="undefined"?"undefined":_typeof(a))===(typeof b==="undefined"?"undefined":_typeof(b))}var rsAstralRange="\\ud800-\\udfff",rsComboMarksRange="\\u0300-\\u036f\\ufe20-\\ufe23",rsComboSymbolsRange="\\u20d0-\\u20f0",rsVarRange="\\ufe0e\\ufe0f";var rsAstral="["+rsAstralRange+"]",rsCombo="["+rsComboMarksRange+rsComboSymbolsRange+"]",rsFitz="\\ud83c[\\udffb-\\udfff]",rsModifier="(?:"+rsCombo+"|"+rsFitz+")",rsNonAstral="[^"+rsAstralRange+"]",rsRegional="(?:\\ud83c[\\udde6-\\uddff]){2}",rsSurrPair="[\\ud800-\\udbff][\\udc00-\\udfff]",rsZWJ="\\u200d";var reOptMod=rsModifier+"?",rsOptVar="["+rsVarRange+"]?",rsOptJoin="(?:"+rsZWJ+"(?:"+[rsNonAstral,rsRegional,rsSurrPair].join("|")+")"+rsOptVar+reOptMod+")*",rsSeq=rsOptVar+reOptMod+rsOptJoin,rsSymbol="(?:"+[rsNonAstral+rsCombo+"?",rsCombo,rsRegional,rsSurrPair,rsAstral].join("|")+")";var reHasUnicode=RegExp("["+rsZWJ+rsAstralRange+rsComboMarksRange+rsComboSymbolsRange+rsVarRange+"]");var reUnicode=RegExp(rsFitz+"(?="+rsFitz+")|"+rsSymbol+rsSeq,"g");function toArray(value){if(!value){return[]}if(isArray(value)){return value}if(isString(value)){return reHasUnicode.test(value)?string.match(reUnicode)||[]:value.split("")}if(Array.from&&(value.length||value instanceof Map||value instanceof Set||value[Symbol&&Symbol.iterator])){return Array.from(value)}return Object.keys(value)}function toNumber(value){return Number(value)}function toString(value){return value&&!isObject(value)?String(value):""}function toObject(value){return isObject(value)?value:{}}function byteLength(str,options){var stringify=options&&typeof options.stringify==="function"?options.stringify:JSON.stringify;str=str!=null?typeof str==="string"?str:stringify(str):"";var s=str.length;for(var i=str.length-1;i>=0;i--){var code=str.charCodeAt(i);if(code>127&&code<=2047)s++;else if(code>2047&&code<=65535)s+=2}return s}module.exports={validators:validators,util:util}},{"normalize-date":14}],12:[function(require,module,exports){"use strict";var validatorsLibrary=require("./common-validators-library");var validators=require("validators-constructor")();validators.util=validatorsLibrary.util;module.exports=validators.add(validatorsLibrary.validators)},{"./common-validators-library":11,"validators-constructor":13}],13:[function(require,module,exports){"use strict";var _typeof=typeof Symbol==="function"&&typeof Symbol.iterator==="symbol"?function(obj){return typeof obj}:function(obj){return obj&&typeof Symbol==="function"&&obj.constructor===Symbol?"symbol":typeof obj};var FORMAT_REGEXP=/(%?)%\{([^\}]+)\}/g;var MESSAGE_REGEXP=/message/i;var hiddenPropertySettings={enumerable:false,configurable:false,writable:true};var RESULT_HANDLER="resultHandler";var EXCEPTION_HANDLER="exceptionHandler";var ERROR_FORMAT="errorFormat";var MESSAGE="message";var SIMPLE_ARGS_FORMAT="simpleArgsFormat";var ONE_OPTIONS_ARG="oneOptionsArg";var ARG="arg";function validatorWrapper(validators,name,validator){return function(value,options){var error=void 0,args=arguments;var alias=this&&this.alias;var validatorObj=validators[name];var validatorAliasObj=alias?validators[alias]:{};var arg=validatorObj[ARG]||validatorAliasObj[ARG]||validators[ARG];var isSimpleArgsFormat=validatorObj[SIMPLE_ARGS_FORMAT]||validatorAliasObj[SIMPLE_ARGS_FORMAT]||validators[SIMPLE_ARGS_FORMAT];options=Object.assign({},validatorObj.defaultOptions,validatorAliasObj.defaultOptions,options);if(typeof options.parse==="function"){value=options.parse(value)}if(options.hasOwnProperty(arg)&&!isSimpleArgsFormat){args=[value,options[arg]].concat(Array.prototype.slice.call(arguments,1))}try{var resultHandler=validatorObj[RESULT_HANDLER]||validatorAliasObj[RESULT_HANDLER]||validators[RESULT_HANDLER];error=resultHandler(validator.apply(validators,args))}catch(err){var exceptionHandler=validatorObj[EXCEPTION_HANDLER]||validatorAliasObj[EXCEPTION_HANDLER]||validators[EXCEPTION_HANDLER];if(typeof exceptionHandler==="function"){error=exceptionHandler(err)}else{throw err}}function handleError(error){if(error){var _ret=function(){var errorObj=(typeof error==="undefined"?"undefined":_typeof(error))==="object"?error:null;var message=options[MESSAGE]||validatorObj[MESSAGE]||validatorAliasObj[MESSAGE];if(message){error=message}var formattedErrorMessage=validators.formatMessage(error,Object.assign({validator:alias||name,value:value},errorObj,options));var format=validatorObj[ERROR_FORMAT]||validatorAliasObj[ERROR_FORMAT]||validators[ERROR_FORMAT];if(format){if(typeof formattedErrorMessage==="string"){formattedErrorMessage={message:formattedErrorMessage}}if(format.$options){format=Object.assign({},format);Object.keys(options).forEach(function(key){if(!MESSAGE_REGEXP.test(key)&&typeof options[key]!=="function"){format[key]=options[key]}})}delete format.$options;if(format.$origin){format=Object.assign({},format,formattedErrorMessage)}delete format.$origin;return{v:validators.formatMessage(format,Object.assign({validator:alias||name,value:value},options,formattedErrorMessage))}}return{v:formattedErrorMessage}}();if((typeof _ret==="undefined"?"undefined":_typeof(_ret))==="object")return _ret.v}}return(typeof error==="undefined"?"undefined":_typeof(error))==="object"&&typeof error.then==="function"?error.then(handleError):handleError(error)}}function formatStr(str,values){values=values||{};if(typeof str!=="string"){return str}return str.replace(FORMAT_REGEXP,function(m0,m1,m2){return m1==="%"?"%{"+m2+"}":values[m2]})}function isPlainObject(value){return{}.toString.call(value)==="[object Object]"&&(typeof value.toDate!=="function"||value.propertyIsEnumerable("toDate"))}function Validators(params){Object.defineProperties(this,{errorFormat:hiddenPropertySettings,formatStr:hiddenPropertySettings,resultHandler:hiddenPropertySettings,exceptionHandler:hiddenPropertySettings,arg:hiddenPropertySettings,simpleArgsFormat:hiddenPropertySettings,oneOptionsArg:hiddenPropertySettings,util:hiddenPropertySettings});this.errorFormat={error:"%{validator}",message:"%{message}",$options:true,$origin:true};this.formatStr=formatStr;this.resultHandler=function(result){return result};this.arg="arg";this.util={};Object.assign(this,params)}function addValidator(name,validator,params){var _this=this;var validators=validator instanceof Array?validator:[validator];var validate=void 0;if(typeof validator==="string"){validate=function validate(){return _this[validator].apply({alias:name,_this:_this},arguments)}}else{validate=function validate(value){var args=Array.prototype.slice.call(arguments,2);var arg1=arguments[1];var arg2=arguments[2];var _this2=this&&this._this||_this;var isSimpleArgsFormat=_this2[name][SIMPLE_ARGS_FORMAT]||_this2[SIMPLE_ARGS_FORMAT];var isOneOptionsArg=_this2[name][ONE_OPTIONS_ARG]||_this2[ONE_OPTIONS_ARG];var options=!isOneOptionsArg&&isPlainObject(arg2)?arg2:{};if(arg1!=null&&typeof arg1!=="boolean"){if(isPlainObject(arg1)){options=arg1}else{options[_this2[name][ARG]||_this2[ARG]]=arg1;if(!isSimpleArgsFormat){args.shift()}}}for(var i=0;i<validators.length;i++){var base=validators[i];switch(typeof base==="undefined"?"undefined":_typeof(base)){case"function":validator=validatorWrapper(_this2,name,base);break;case"string":validator=_this2[base];break;case"object":validator=_this2[base[0]];options=Object.assign({},options,base[1])}var error=validator.apply(this,[value,options].concat(args));if(error){return error}}}}Object.assign(validate,params);validate.curry=function(){var _arguments=arguments;return function(value){
return validate.apply(_this,[value].concat(Array.prototype.slice.call(_arguments)))}};_this[name]=validate}Validators.prototype.add=function(validatorName,validators,params){var _this3=this;if(typeof validatorName==="string"){addValidator.call(this,validatorName,validators,params)}else{Object.keys(validatorName).forEach(function(key){return addValidator.call(_this3,key,validatorName[key],validators)})}return this};Validators.prototype.formatMessage=function(message,values){var _this4=this;if(typeof message==="function"){message=message(values.value,values)}if((typeof message==="undefined"?"undefined":_typeof(message))==="object"){var formattedMessage={};Object.keys(message).forEach(function(key){return formattedMessage[_this4.formatStr(key,values)]=_this4.formatStr(message[key],values)});if(message[MESSAGE]){formattedMessage[MESSAGE]=this.formatStr(message[MESSAGE],values)}return formattedMessage}return this.formatStr(message,values)};module.exports=function(options){return new Validators(options)}},{}],14:[function(require,module,exports){"use strict";function setTimezoneOffset(date){date.setMinutes(date.getMinutes()-date.getTimezoneOffset())}function normalizeDateTime(date){if(!date){return new Date(date)}if(arguments.length>1){date=Array.prototype.slice.call(arguments)}if(Array.isArray(date)){date=new(Function.prototype.bind.apply(Date,[null].concat(date)))}var jsDate=new Date(date);if(date===Object(date)){var momentBaseDate=date.creationData&&date.creationData().input;if(!(momentBaseDate&&(typeof momentBaseDate==="number"||typeof momentBaseDate==="string"&&/:.+Z|GMT|[+-]\d\d:\d\d/.test(momentBaseDate)))){setTimezoneOffset(jsDate)}return jsDate}if(!isNaN(jsDate)&&typeof date==="string"){if(date.match(/Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec/)&&date.indexOf("GMT")===-1){setTimezoneOffset(jsDate)}}else{jsDate=new Date(Number(String(date).split(".").join("")))}return jsDate}function normalizeDate(date,options){date=normalizeDateTime(date);return(options||{}).noTime?new Date(date.getUTCFullYear(),date.getUTCMonth(),date.getUTCDate()):date}module.exports=normalizeDate},{}]},{},[10])(10)});

@@ -5,4 +5,4 @@ 'use strict';

* @param {Object} object
* @param {String} [path='']
* @param {Object} [flattenObject={}]
* @param {string} [path='']
* @param {Object} [flatObject={}]
*

@@ -13,4 +13,4 @@ * @returns {Object}

function flatten(object) {
var path = arguments.length <= 1 || arguments[1] === undefined ? '' : arguments[1];
var flattenObject = arguments.length <= 2 || arguments[2] === undefined ? {} : arguments[2];
var path = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : '';
var flatObject = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};

@@ -20,10 +20,10 @@ Object.keys(object).forEach(function (propertyName) {

if (propertyValue instanceof Array) {
flattenObject[path + propertyName] = propertyValue;
if (propertyValue instanceof Object && !(propertyValue instanceof Array)) {
flatten(propertyValue, path + propertyName + '.', flatObject);
} else {
flatten(propertyValue, path + propertyName + '.', flattenObject);
flatObject[path + propertyName] = propertyValue;
}
});
return flattenObject;
return flatObject;
}

@@ -30,0 +30,0 @@

'use strict';
var validateObject = require('./validateObject');
var errors = require('./errors');
var ValidationError = require('./validationError');
var formatters = require('./formatters');
var DEFAULT_TIMEOUT = 10000;
var DEFAULT_OPTIONS = {
format: 'flat',
reject: false
};
/**
* @param {Object} object
* @param {Object} schema
* @param {Object} [options={}]
* @param {Number} [options.timeout=10000]
* @param {String} [options.format] - There is no default formatter
* @param {Number} [options.maxPropertyErrorsCount] - By default all property errors will be returned. Must be >= 1
* @param {Object} object
* @param {Object} schema
* @param {Object} [options={}]
* @param {string} [options.format=flat]
* @param {boolean} [options.reject=false]
*

@@ -20,30 +22,26 @@ * @returns {Promise}

module.exports = function (object, schema) {
var options = arguments.length <= 2 || arguments[2] === undefined ? {} : arguments[2];
var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
options = Object.assign({}, DEFAULT_OPTIONS, options);
return new Promise(function (resolve, reject) {
var timeout = options.timeout || DEFAULT_TIMEOUT;
var format = options.format;
if (format && !formatters[format]) {
return reject(new Error('Unknown format ' + format));
var formatter = formatters[options.format];
if (!formatter) {
return reject(new Error('Unknown format ' + options.format));
}
var timeoutObject = setTimeout(function () {
reject(new errors.TimeoutError());
}, timeout);
validateObject(object, schema, object, []).then(function (validationErrors) {
if (!validationErrors) {
return resolve();
}
validateObject(object, schema, object, [], options).then(function (result) {
clearTimeout(timeoutObject);
var formattedErrors = formatter(validationErrors);
if (format) {
var formattedResult = formatters[format](result);
return resolve(formattedResult);
if (options.reject) {
reject(new ValidationError(formattedErrors));
} else {
resolve(formattedErrors);
}
resolve(result);
}).catch(function (err) {
clearTimeout(timeoutObject);
reject(err);
});
}).catch(reject);
});
};

@@ -8,10 +8,8 @@ 'use strict';

* @param {Object} schema
* @param {Object} originalObject
* @param {String[]} path
* @param {Object} options
* @param {Number} [options.maxPropertyErrorsCount]
* @param {Object} fullObject
* @param {string[]} path
*
* @returns {Promise}
*/
module.exports = function (object, schema, originalObject, path, options) {
module.exports = function (object, schema, fullObject, path) {
var validationPromises = [];

@@ -30,7 +28,7 @@ var validationErrors = {};

if (propertySchema instanceof Function) {
propertySchema = propertySchema(propertyValue, object, originalObject, propertyPath);
propertySchema = propertySchema(propertyValue, object, fullObject, propertyPath);
}
if (propertySchema) {
var validationPromise = validateProperty(propertyValue, propertySchema, object, originalObject, propertyPath, options);
if (propertySchema instanceof Object) {
var validationPromise = validateProperty(propertyValue, propertySchema, object, fullObject, propertyPath);

@@ -37,0 +35,0 @@ validationPromise.then(function (validationError) {

'use strict';
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol ? "symbol" : typeof obj; };
var validateValue = require('./validateValue');
var utils = require('./utils');

@@ -12,21 +9,15 @@ /**

* @param {Object} object
* @param {Object} originalObject
* @param {String[]} path
* @param {Object} options
* @param {Number} [options.maxPropertyErrorsCount]
* @param {Object} fullObject
* @param {string[]} path
*
* @returns {Promise}
*
*/
module.exports = function (value, schema, object, originalObject, path, options) {
var validatorsOptions = schema.$validators;
module.exports = function (value, schema, object, fullObject, path) {
var validatorsOptions = schema.$validate;
if (validatorsOptions instanceof Function) {
validatorsOptions = validatorsOptions(value, object, originalObject, path);
validatorsOptions = validatorsOptions(value, object, fullObject, path);
}
var promise = void 0;
if (validatorsOptions) {
promise = validateValue(value, validatorsOptions, object, originalObject, path, options);
} else {
promise = Promise.resolve();
}
var promise = validatorsOptions instanceof Object ? validateValue(value, validatorsOptions, object, fullObject, path) : Promise.resolve();

@@ -40,33 +31,33 @@ return promise.then(function (validationErrors) {

if (schema.$items || schema instanceof Array) {
var _ret = function () {
if (!(value instanceof Array)) {
return {
v: Promise.resolve(['Must be an array'])
};
}
if (schema.$items || schema[0]) {
if (!(value instanceof Array)) {
return Promise.resolve([{
error: 'array',
message: 'must be an array'
}]);
}
var propertiesSchema = {};
var itemSchema = schema.$items || schema[0];
var propertiesSchema = {};
var itemSchema = schema.$items || schema[0];
value.forEach(function (item, index) {
propertiesSchema[index] = itemSchema;
});
value.forEach(function (item, index) {
return propertiesSchema[index] = itemSchema;
});
return {
v: validateObject(value, propertiesSchema, originalObject, path, options)
};
}();
if ((typeof _ret === 'undefined' ? 'undefined' : _typeof(_ret)) === "object") return _ret.v;
return validateObject(value, propertiesSchema, fullObject, path);
}
if (utils.hasSchemaAtLeastOneProperty(schema)) {
if (Object.keys(schema).some(function (propertyName) {
return !propertyName.startsWith('$');
})) {
if (!(value instanceof Object)) {
return Promise.resolve(['Must be an object']);
return Promise.resolve([{
error: 'object',
message: 'must be an object'
}]);
}
return validateObject(value, schema, originalObject, path, options);
return validateObject(value, schema, fullObject, path);
}
});
};

@@ -9,10 +9,8 @@ 'use strict';

* @param {Object} object
* @param {Object} originalObject
* @param {String[]} path
* @param {Object} options
* @param {Number} [options.maxPropertyErrorsCount]
* @param {Object} fullObject
* @param {string[]} path
*
* @returns {Promise}
*/
module.exports = function (value, validatorsOptions, object, originalObject, path, options) {
module.exports = function (value, validatorsOptions, object, fullObject, path) {
var validatorsResultsPromises = [];

@@ -29,10 +27,10 @@ var validationErrors = [];

if (validatorOptions instanceof Function) {
validatorOptions = validatorOptions(value, object, originalObject, path);
validatorOptions = validatorOptions(value, object, fullObject, path);
}
if (!validatorOptions) {
if (validatorOptions === false || validatorOptions === null || validatorOptions === undefined) {
return;
}
var validatorResult = validator.call(validator, value, validatorOptions, object, originalObject, path);
var validatorResult = validator(value, validatorOptions, object, fullObject, path);

@@ -51,6 +49,2 @@ var validatorResultPromise = Promise.resolve(validatorResult);

if (validationErrors.length) {
if (options.maxPropertyErrorsCount) {
return validationErrors.slice(0, options.maxPropertyErrorsCount);
}
return validationErrors;

@@ -57,0 +51,0 @@ }

'use strict';
module.exports = {};
var validators = require('common-validators');
validators.oneOptionsArg = true;
var originalAdd = validators.add;
validators.add = function (validatorName, validator) {
if (validatorName instanceof Object) {
return originalAdd.call(validators, validatorName, { simpleArgsFormat: true });
}
originalAdd.call(validators, validatorName, validator, { simpleArgsFormat: true });
};
validators.confirmOriginal = validators.confirm;
validators.add({
confirm: function confirm(value, options, object, fullObject, path) {
var confirmOptions = {
key: path[path.length - 1],
comparedKey: options instanceof Object ? options.key : options
};
return this.confirmOriginal(object, confirmOptions);
}
});
module.exports = validators;

@@ -7,4 +7,4 @@ 'use strict';

validate.formatters = require('./formatters');
validate.errors = require('./errors');
validate.ValidationError = require('./validationError');
module.exports = validate;
{
"name": "validy",
"description": "Nested objects validation with support of async validators",
"version": "0.0.2",
"description": "Declarative validation with async validators support",
"version": "0.1.0",
"author": {

@@ -18,2 +18,3 @@ "name": "Dmitry Kirilyuk",

"validator",
"declarative",
"schema",

@@ -24,3 +25,3 @@ "nested",

"engines": {
"node": ">=4.0.0"
"node": ">=6.0.0"
},

@@ -34,14 +35,21 @@ "main": "./lib/validy.js",

],
"dependencies": {
"common-validators": "^0.2.6"
},
"devDependencies": {
"babel-cli": "6.9.0",
"babel-preset-es2015": "6.9.0",
"browserify": "13.0.1",
"uglify-js": "2.6.2",
"babel-cli": "6.22.2",
"babel-preset-es2015": "6.22.0",
"babel-plugin-transform-builtin-extend": "1.1.0",
"browserify": "14.0.0",
"uglify-js": "2.7.5",
"chai": "3.5.0",
"chai-as-promised": "5.3.0",
"mocha": "2.4.5"
"chai-as-promised": "6.0.0",
"mocha": "3.2.0",
"sinon": "1.17.7",
"sinon-chai": "2.8.0",
"require-me": "0.0.4"
},
"scripts": {
"test": "mocha 'tests/**/*.js' --timeout 5000",
"babelify": "babel src --presets es2015 --out-dir lib",
"babelify": "babel src --out-dir lib",
"browserify": "browserify ./lib/validy.js --standalone validy --outfile dist/validy.js",

@@ -48,0 +56,0 @@ "minify": "uglifyjs dist/validy.js --output dist/validy.min.js",

+556
-11
# validy
Nested objects validation with support of async validators
Declarative validation with async validators support

@@ -8,4 +8,26 @@ [![NPM version](https://img.shields.io/npm/v/validy.svg)](https://npmjs.org/package/validy)

**Note:** This module works in browsers and Node.js >= 4.0.
**Note:** This module works in browsers and Node.js >= 6.0.
## Table of Contents
- [Installation](#installation)
- [Node.js](#nodejs)
- [Browser](#browser)
- [Overview](#overview)
- [Usage](#usage)
- [validy(object, schema, [options])](#validyobject-schema-options)
- [Parameters](#parameters)
- [Return value](#return-value)
- [Validators](#validators)
- [Built-in validators](#built-in-validators)
- [Custom validator](#custom-validator)
- [Synchronous validator](#synchronous-validator)
- [Asynchronous validator](#asynchronous-validator)
- [Error format](#error-format)
- [Return value](#return-value-1)
- [Dynamic schema](#dynamic-schema)
- [Build](#build)
- [Tests](#tests)
- [License](#license)
## Installation

@@ -33,2 +55,86 @@

## Overview
`validy` allows you to validate flat and nested objects using collection of default validators and your own validators.
Validators can be asynchronous, you can do DB calls for example and so on.
To validate object you should define schema. It's simple object with your constraints:
```js
const book = { // object to validate
name: 'The Adventures of Tom Sawyer',
author: {
name: 'Mark Twain'
},
reviews: [
{
author: 'Leo Tolstoy',
text: 'Great novel'
},
{
author: 'Fyodor Dostoyevsky',
text: 'Very interesting'
}
]
};
const schema = {
name: {
$validate: {
required: true,
string: true
}
},
author: { // you can omit check that "author" value is object, it will be done by validy internally
name: {
$validate: {
required: true,
string: true
}
}
},
reviews: [{ // define schema for array items
author: {
$validate: {
required: true,
string: true
}
},
text: {
$validate: {
required: true,
string: true
}
}
}]
};
validy(book, schema)
.then(errors => {
if (errors) {
// you have validation errors ("errors" is plain object)
} else {
// no errors ("errors" is undefined)
}
})
.catch(err => {
// application error (something went wrong)
});
// async/await example
async function example() {
try {
const errors = await validy(book, schema);
if (errors) {
// you have validation errors ("errors" is plain object)
} else {
// no errors ("errors" is undefined)
}
} catch(err) {
// application error (something went wrong)
}
}
```
## Usage

@@ -38,15 +144,454 @@

**Parameters**
#### Parameters
* `object` (Object) - Object to validate
* `schema` (Object) - Schema which defines how to validate object
* `[options]` (Object) - Validation options
- `[format]` (String) - Format of object with validation errors
- `[timeout=10000]` (Number) - Validation time limit in ms after which an timeout error will be thrown
- `[maxPropertyErrorsCount]` (Number) - Factory for error creation if object not found
- `object` (Object) - Object to validate
- `schema` (Object) - Schema which defines how to validate object
- `[options]` (Object) - Validation options
- `[format=flat]` (string) - Format of object with validation errors (`flat`, `nested`)
- `[reject=false]` (boolean) - Should return fulfilled promise with errors (`by default`) or rejected with `ValidationError`?
**Return value**
#### Return value
(Promise) - Result of validation
(Promise) - Result of validation. Promise is returned even for synchronous only validation
### Validators
#### Built-in validators
By default `validy` uses collection of simple and useful validators ([common-validators](https://github.com/tamtakoe/common-validators) module).
**Note**:
The basic principle of built-in validators is that many of them (except `required`, `notEmpty` and type validators `object`, `array`, ...) consider empty values as **valid values**.
Empty values are:
- `undefined`
- `null`
- `NaN`
- `''`
- `' '` (whitespace only string)
- `[]`
- `{}`
Also they convert passed value to expected type. For example, `max` validator which checks that value is not greater than some limit will try to convert passed value to number (`Number(value)`).
All non-convertible values will be treated as `NaN` and validator will return validation error.
Some of built-in validators:
- **required (presence)** - validates that the value isn't `undefined`, `null`, `NaN`, empty or whitespace only string, empty array or object
- **notEmpty** - like `required` but `undefined` is valid value. It is useful for PATCH requests
- **object / array / string / number / integer / ...** - value is plain object, array, ...
- **max** - value is less than maximum
- **min** - value is greater than minimum
- **range** - value is in range
- **maxLength** - value length is greater than maximum
- **minLength** - value length is greater than minimum
- **pattern** - value matches the pattern
- **inclusion** - value is contained in white list
- **exclusion** - value is not contained in black list
- **email** - value is email address
- **url** - value is URL
- and many others (see [common-validators#validators](https://github.com/tamtakoe/common-validators#validators))
#### Custom validator
You can add your own validator:
```js
validy.validators.add('greaterThan', function(value, options) {
// validator implementation
});
// or
validy.validators.add({ // this way you can add several validators at once
greaterThan: function(value, options) {
// validator implementation
},
anotherValidator: function(value, options) {
// validator implementation
}
});
```
Although in most cases you will have only two parameters in your own validators (`value` and `options`), some situations will require a bit knowledgeable validator.
So, full signature of validator is:
**validator(value, options, object, fullObject, path)**
- `value` (any) - Validated value
- `options` (Object) - Validator options
- `object` (Object) - Object whose property is validated at the moment
- `fullObject` (Object) - The whole validated object (object which was initially passed to `validy`)
- `path` (string[]) - Path to property
So imagine you wrote `validateSomething` validator:
```js
const book = {
name: 'The Adventures of Tom Sawyer',
author: {
name: 'Mark Twain'
}
};
const schema = {
name: {
$validate: {
required: true,
string: true
}
},
author: {
name: {
$validate: {
required: true,
string: true,
validateSomething: 'someArgument' // <--- and you want to use it here
}
}
}
};
```
`validateSomething` validator will be called with the following arguments:
1) value
Value of `author.name` property.
```js
'Mark Twain'
```
2) options
When you use non-object value as validator options it will be wrapped in object with `arg` property.
```js
{
arg: 'someArgument'
}
```
3) object
Object with `name` property (`author` object).
```js
{
name: 'Mark Twain'
}
```
4) fullObject
The whole validated object (`book` object).
```js
{
name: 'The Adventures of Tom Sawyer',
author: {
name: 'Mark Twain'
}
}
```
5) path
```js
['author', 'name']
```
##### Synchronous validator
Example:
```js
validy.validators.add('russianLettersOnly', function(value) {
// it's ok to consider empty value as valid value
// use "required" validator when this value must not be empty
if (value === '') {
// if value is valid just return nothing or falsy value
return;
}
if (typeof value !== 'string' || !/^[а-яё]+$/i.test(value)) {
// when value is invalid, return string with error
return 'must contain only Russian letters';
}
});
// or
validy.validators.add({ // this way you can add several validators at once
russianLettersOnly: function(value) { /**/ },
anotherValidator: function(value) { /**/ }
});
```
And then just use it as any other validator:
```js
{
name: {
$validate: {
// validator will be called only if its config is not equal to false/null/undefined
russianLettersOnly: true
}
}
}
```
##### Asynchronous validator
Almost the same as synchronous validator, just return fulfilled promise:
```js
validy.validators.add({
/**
* Check using mongoose model that value exists in mongodb
*
* @param {string} value
* @param {Object} options
* @param {Object} options.model - Mongoose model
* @param {string} [options.field] - Which field to use for search
*
* @returns {Promise}
*/
exists: function(value, options) {
const errorMessage = 'does not exist';
if (value === '') {
return Promise.resolve();
}
if (typeof value !== 'string') {
return Promise.resolve(errorMessage);
}
const model = options.model;
const field = options.field || '_id';
return model.count({ [field]: value })
.then(count => {
if (!count) {
// if value is invalid, return fulfilled promise with validation error
return Promise.resolve(errorMessage);
}
});
}
});
```
### Error format
If there are no validation errors `validy` returns `undefined` as fulfillment value:
```js
validy(book, schema)
.then(errors => {
console.log(errors); // undefined
})
```
If you have validation errors:
```js
const book = {
name: '', // empty
author: {
name: 123456789 // not string
}
};
const schema = {
name: {
$validate: {
required: true,
string: true
}
},
author: {
name: {
$validate: {
required: true,
string: true
}
}
}
};
validy(book, schema)
.then(errors => {
console.log(errors);
})
```
`errors` with flat structure:
```js
{
name: [{ // errors are always placed in array
error: 'required', // validator name
message: 'Is required' // error message
}],
'author.name': [{
error: 'string',
message: 'Must be a string'
}]
}
```
But you can use nested structure:
```js
validy(book, schema, { format: 'nested' })
.then(errors => {
console.log(errors);
})
```
`errors` with nested structure:
```js
{
name: [{
error: 'required',
message: 'Is required'
}],
author: {
name: [{
error: 'string',
message: 'Must be a string'
}]
}
}
```
### Return value
By default `validy` returns fulfilled promise when validated object is not valid.
If for some reasons you want to use rejected promise with validation error instead of fulfilled promise, specify `reject` option:
```js
validy(object, schema, { reject: true })
.then(() => {
// no errors, everything is valid
})
.catch(err => {
if (err instanceof validy.ValidationError) {
// err.errors contains validation errors
} else {
// application error (something went wrong)
}
});
```
### Dynamic schema
Sometimes you may need a way to validate some property differently depending on specific conditions.
Example with order of various products:
```js
const order = {
products: [
{
type: 'book',
name: 'The Adventures of Tom Sawyer',
count: 1
},
{
type: 'sugar',
weight: 3000
}
]
};
const productsSchemas = {
book: {
name: {
$validate: {
required: true,
string: true
}
},
count: {
$validate: {
required: true,
integer: true,
min: 1
}
}
},
sugar: {
weight: {
$validate: {
required: true,
integer: true,
min: 1000
}
}
}
};
const schema = {
products: [(product/*, products, order, pathToItem*/) => {
const productSchema = productsSchemas[product.type] || {};
return Object.assign({}, productSchema, {
type: {
$validate: {
required: true,
string: true,
inclusion: Object.keys(productsSchemas)
}
}
});
}]
};
// or you can do like this
const alternativeSchema = {
products: {
$validate: { // validate also "products" before items validation
required: true,
array: true
},
$items: (product/*, products, order, pathToItem*/) => {
const productSchema = productsSchemas[product.type] || {};
return Object.assign({}, productSchema, {
type: {
$validate: {
required: true,
string: true,
inclusion: Object.keys(productsSchemas)
}
}
});
}
}
};
```
You can do similar things with `$validate` and specific validator:
```js
const bookSchema = {
author: {
name: {
$validate: function(name, author, book, pathToName) {
// implement your custom logic
// validation will only run if you return object
// so you can return null for example to skip validation
return {
required: function(name, author, book, pathToName) {
// implement your custom logic
// return undefined, null or false if you want skip validation
},
string: true
};
}
}
}
};
```
## Build

@@ -53,0 +598,0 @@

'use strict';
exports.TimeoutError = require('./timeoutError');
'use strict';
var util = require('util');
/**
* @param {String} [message=Validation timeout]
*
* @constructor
*/
function TimeoutError() {
var message = arguments.length <= 0 || arguments[0] === undefined ? 'Validation timeout' : arguments[0];
Error.call(this);
Error.captureStackTrace(this, this.constructor);
this.name = this.constructor.name;
this.message = message;
}
util.inherits(TimeoutError, Error);
module.exports = TimeoutError;
'use strict';
exports.flat = require('./flat');
'use strict';
/**
* @param {Object} schema
*
* @returns {Boolean}
*/
exports.hasSchemaAtLeastOneProperty = function (schema) {
for (var propertyName in schema) {
if (!propertyName.startsWith('$')) {
return true;
}
}
return false;
};