oa-jira
Version:
Octet Agile's JIRA connectivity project.
33 lines (28 loc) • 1.32 kB
JavaScript
const errors = require('../errors');
const promiseUtils = require('./promise.utils');
const arrayUtils = require('./array.utils');
const check = (value, { name = 'integer', required = true, min, max } = {}) => {
if (!value && value !== 0) {
if (required === true) throw errors.missing.new(name);
return null;
}
if (!Number.isInteger(value)) throw errors.invalid.type.new(name, 'integer');
if (Number.isInteger(min) && value < min) throw errors.too.small.new(name, min);
if (Number.isInteger(max) && value > max) throw errors.too.big.new(name, max);
return value;
};
exports.check = check;
const resolve = (value, { name, required, min, max } = {}) => {
return promiseUtils.async(check, value, { name, required, min, max });
};
exports.resolve = resolve;
exports.checkBulk = (values, { name = 'integers', required, min, max } = {}) => {
if (arrayUtils.not(values)) throw errors.invalid.type.new(name, 'array');
for (const value of values) this.check(value, { name, required, min, max });
return values;
};
exports.resolveBulk = (values, { name = 'integers', required, min, max } = {}) => {
return arrayUtils.is(values)
? Promise.all(values.map(value => resolve(value, { name, required, min, max })))
: errors.invalid.type.reject(name, 'array');
};