UNPKG

oa-jira

Version:

Octet Agile's JIRA connectivity project.

37 lines (32 loc) 1.51 kB
const errors = require('../errors'); const arrayUtils = require('./array.utils'); const promiseUtils = require('./promise.utils'); const check = ( value, { name = 'string', required = true, minLength = 0, maxLength = 4096, trim = true, accepted = [] } = {} ) => { if (!value && value !== '') { if (required === true) throw errors.missing.new(name); return null; } if (typeof value !== 'string') throw errors.invalid.type.new(name, 'string'); const clean = trim === true ? value.trim() : value; if (Number.isInteger(minLength) && clean.length < minLength) throw errors.too.small.new(name, minLength); if (Number.isInteger(maxLength) && clean.length > maxLength) throw errors.too.big.new(name, maxLength); if (Array.isArray(accepted) && accepted.length > 0 && !accepted.includes(clean)) { throw errors.prohibited.new(name, clean); } return clean; }; exports.check = check; exports.checkBulk = (values, { name = 'strings', required, minLength, maxLength, trim, accepted } = {}) => { const cleanValues = []; if (arrayUtils.not(values)) throw errors.invalid.type.new(name, 'array'); for (const value of values) { cleanValues.push(this.check(value, { name, required, minLength, maxLength, trim, accepted })); } return cleanValues; }; exports.resolve = (value, { name, required, minLength, maxLength, trim, accepted } = {}) => { return promiseUtils.async(check, value, { name, required, minLength, maxLength, trim, accepted }); };