@ticatec/web-bean-validator
Version:
A TypeScript/JavaScript library for rule-based entity validation with boundary checking for strings, numbers, dates, enums, objects, and arrays.
33 lines (32 loc) • 1.21 kB
JavaScript
import BaseValidator from "./BaseValidator";
import i18nRes from "../i18nRes";
export default class NumberValidator extends BaseValidator {
constructor(field, options = null) {
super(field, options);
this.minValue = options === null || options === void 0 ? void 0 : options.minValue;
this.maxValue = options === null || options === void 0 ? void 0 : options.maxValue;
}
checkField(value, result) {
if (this.minValue != null && value < this.minValue) {
result.setError(this.field, this.formatErrorMessage(i18nRes.validation.numberShortage, {
field: this.name,
min: this.minValue
}));
return false;
}
if (this.maxValue != null && value > this.maxValue) {
result.setError(this.field, this.formatErrorMessage(i18nRes.validation.numberExceed, {
field: this.name,
max: this.maxValue
}));
return false;
}
return true;
}
checkType(value) {
if (!isNaN(value) && typeof value == "string") {
value = parseFloat(value);
}
return isNaN(value) ? null : value;
}
}