roc
Version:
Build modern web applications easily
306 lines (258 loc) • 8.19 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.isArray = isArray;
exports.isObject = isObject;
exports.isArrayOrSingle = isArrayOrSingle;
exports.isPromise = isPromise;
exports.isRegExp = isRegExp;
exports.isString = isString;
exports.isBoolean = isBoolean;
exports.isInteger = isInteger;
exports.isPath = isPath;
exports.isFunction = isFunction;
exports.oneOf = oneOf;
exports.required = required;
exports.infoObject = infoObject;
var _lodash = require('lodash');
var _isPromise = require('is-promise');
var _isPromise2 = _interopRequireDefault(_isPromise);
var _index = require('./index.js');
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
/**
* Validates an array using a validator.
*
* @param {function|RegExp} validator - The validator to use on the elements in the array.
* @return {function} - A function that takes a value and that returns true or false if valid or not.
*/
function isArray(validator) {
return (input, info) => {
if (info) {
return infoObject(validator, wrap => `[${ wrap }]`);
}
if (!(0, _lodash.isArray)(input)) {
return 'Was not an array!';
}
return isArrayOrSingle(validator)(input);
};
}
/**
* Validates an object using a validator.
*
* @param {function|RegExp} validator - The validator to use on the elements in the object
* @return {function} - A function that takes a value and that returns true or false if valid or not.
*/
function isObject(validator) {
return (input, info) => {
if (info) {
return infoObject(validator, wrap => `{${ wrap }}`);
}
if (!(0, _lodash.isPlainObject)(input)) {
return 'Was not an object!';
}
if (!validator) {
return true;
}
return Object.keys(input).map(key => (0, _index.isValid)(input[key], validator)).reduce((a, b) => a === true && b === true, true);
};
}
/**
* Validates an pontential array using a validator.
*
* Allows that a single value is given.
*
* @param {function|RegExp} validator - The validator to use on the elements in the array.
* @return {function} - A function that takes a value and that returns true or false if valid or not.
*/
function isArrayOrSingle(validator) {
return (input, info) => {
if (info) {
return infoObject(validator, wrap => `${ wrap } / [${ wrap }]`);
}
const array = [].concat(input);
for (const value of array) {
const result = (0, _index.isValid)(value, validator);
if (result !== true) {
return result;
}
}
return true;
};
}
/**
* Validates a promise.
*
* @param {object} value - Something to validate.
* @param {boolean} info - If type information should be returned.
* @return {infoObject|boolean|string} - Type information or if it is valid.
*/
function isPromise(value, info) {
if (info) {
return infoObject('Promise');
}
if (!(0, _isPromise2.default)(value)) {
return 'Was not a Promise!';
}
return true;
}
/**
* Validates a RegExp.
*
* @param {object} value - Something to validate.
* @param {boolean} info - If type information should be returned.
* @return {infoObject|boolean|string} - Type information or if it is valid.
*/
function isRegExp(value, info) {
if (info) {
return infoObject('RegExp');
}
if (!(0, _lodash.isRegExp)(value)) {
return 'Was not a RegExp!';
}
return true;
}
/**
* Validates an string.
*
* @param {object} value - Something to validate.
* @param {boolean} info - If type information should be returned.
* @return {infoObject|boolean|string} - Type information or if it is valid.
*/
function isString(value, info) {
if (info) {
return infoObject('String');
}
if (!(0, _lodash.isString)(value)) {
return 'Was not a string!';
}
return true;
}
/**
* Validates an boolean.
*
* @param {object} value - Something to validate.
* @param {boolean} info - If type information should be returned.
* @return {infoObject|boolean|string} - Type information or if it is valid.
*/
function isBoolean(value, info) {
if (info) {
return infoObject('Boolean');
}
if (value !== null && !(0, _lodash.isBoolean)(value)) {
return 'Was not a boolean!';
}
return true;
}
/**
* Validates an integer.
*
* @param {object} value - Something to validate.
* @param {boolean} info - If type information should be returned.
* @return {infoObject|boolean|string} - Type information or if it is valid.
*/
function isInteger(value, info) {
if (info) {
return infoObject('Integer');
}
if (!Number.isInteger(value)) {
return 'Was not an integer!';
}
return true;
}
/**
* Validates an path.
*
* @param {object} value - Something to validate.
* @param {boolean} info - If type information should be returned.
* @return {infoObject|boolean|string} - Type information or if it is valid.
*/
function isPath(value, info) {
if (info) {
return infoObject('Filepath');
}
if (isString(value) !== true) {
return 'Was not a filepath!';
}
return true;
}
/**
* Validates an function.
*
* @param {object} value - Something to validate.
* @param {boolean} info - If type information should be returned.
* @return {infoObject|boolean|string} - Type information or if it is valid.
*/
function isFunction(value, info) {
if (info) {
return infoObject('Function');
}
if (value !== null && !(0, _lodash.isFunction)(value)) {
return 'Was not a function!';
}
return true;
}
/**
* Validates against a list of validators.
*
* @param {...function} validators - Validators to validate against.
* @return {function} - A function that takes a value and that returns true or false if valid or not.
*/
function oneOf() {
for (var _len = arguments.length, validators = Array(_len), _key = 0; _key < _len; _key++) {
validators[_key] = arguments[_key];
}
if (!validators.length) {
throw new Error('You need to use at least one validator.');
}
return (input, info) => {
if (info) {
let types = [];
for (const validator of validators) {
types.push(infoObject(validator).type);
}
return infoObject(types.join(' / '));
}
const invalid = [];
for (const validator of validators) {
const result = (0, _index.isValid)(input, validator);
if (result === true) {
return true;
}
invalid.push(validator(null, true).type);
}
return 'Was not any of the possible types:\n' + invalid.reduce((prev, next) => prev + '\n' + next, '');
};
}
/**
* Marks that the value is required
*
* @param {function} validator - Validator to validate against
* @return {function} - A function that takes a value and that returns true or false if valid or not.
*/
function required(validator) {
return (input, info) => {
if (info) {
return infoObject(validator, null, true);
}
if (!input && input !== false || ((0, _lodash.isArray)(input) || (0, _lodash.isString)(input)) && input.length === 0 || (0, _lodash.isPlainObject)(input) && Object.keys(input).length === 0) {
return 'A value was required but none was given!';
}
if (!validator) {
return true;
}
return validator(input);
};
}
function infoObject() {
let validator = arguments.length <= 0 || arguments[0] === undefined ? () => ({ type: '' }) : arguments[0];
let wrapper = arguments[1];
let req = arguments.length <= 2 || arguments[2] === undefined ? false : arguments[2];
const info = (0, _lodash.isFunction)(validator) ? validator(null, true).type : validator.toString();
const type = wrapper ? wrapper(info) : info;
return {
type,
required: req
};
}
//# sourceMappingURL=validators.js.map