waigo
Version:
Node.js ES6 framework for reactive, data-driven apps and APIs (Koa, RethinkDB)
44 lines (35 loc) • 1.13 kB
JavaScript
const waigo = global.waigo,
_ = waigo._,
FieldValidationError = waigo.load('support/forms/field').FieldValidationError;
/**
* Validator to check whether number is within given range.
*
* Expect atleast one of `min` or `max`.
*
* @param options {Object} Options.
* @param options.min {Object} Minimum length.
* @param options.max {Object} Maximum length.
*
* @throws Error If not an email address.
*/
module.exports = function(options) {
options = (options || {});
let min = options.min,
max = options.max;
return function*(context, field, value) {
value = Number(value);
if ( (undefined !== min && value < min)
|| (undefined !== max && value > max) ) {
if (undefined !== min) {
if (undefined !== max) {
throw new FieldValidationError(`Must be between ${min} and ${max} inclusive`);
} else {
throw new FieldValidationError(`Must be greater than or equal to ${min}`);
}
} else {
throw new FieldValidationError(`Must be less than or equal to ${max}`);
}
}
}
};
;