sure-guard
Version:
Node library for data validation
87 lines (86 loc) • 2.23 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.NumberValidator = void 0;
/**
* Class for validating numbers.
*/
class NumberValidator {
/**
* Creates a new NumberValidator instance.
* @param {any} value - The value to be validated.
*/
constructor(value) {
this.value = value;
this.isValid = true;
}
/**
* Checks if the value is of type number.
* @returns {this} - The NumberValidator instance.
*/
isNumber() {
if (typeof this.value !== 'number') {
this.isValid = false;
}
return this;
}
/**
* Checks if the value is greater than or equal to a minimum.
* @param {number} minValue - The minimum value allowed.
* @returns {this} - The NumberValidator instance.
*/
isMinValue(minValue) {
if (this.value < minValue) {
this.isValid = false;
}
return this;
}
/**
* Checks if the value is less than or equal to a maximum.
* @param {number} maxValue - The maximum value allowed.
* @returns {this} - The NumberValidator instance.
*/
isMaxValue(maxValue) {
if (this.value > maxValue) {
this.isValid = false;
}
return this;
}
/**
* Checks if the value is positive.
* @returns {this} - The NumberValidator instance.
*/
isPositive() {
if (this.value <= 0) {
this.isValid = false;
}
return this;
}
/**
* Checks if the value is negative.
* @returns {this} - The NumberValidator instance.
*/
isNegative() {
if (this.value >= 0) {
this.isValid = false;
}
return this;
}
/**
* Checks if the value is an integer number.
* @returns {this} - The NumberValidator instance.
*/
isInteger() {
if (!Number.isInteger(this.value)) {
this.isValid = false;
}
return this;
}
/**
* Returns the result of the validation.
* @returns {boolean} - The result of the validation.
*/
getResult() {
return this.isValid;
}
}
exports.NumberValidator = NumberValidator;