@blinkk/selective-edit
Version:
Selective structured text editor.
79 lines • 2.56 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.RangeRule = void 0;
const validationRules_1 = require("../validationRules");
const dataType_1 = require("../../utility/dataType");
class RangeRule extends validationRules_1.Rule {
constructor(config) {
super(config);
this.defaultMessage = 'Value needs to be a number in range.';
this.config = config;
}
allowAdd(value) {
// Allow for empty fields.
if (!value) {
return true;
}
value = this.cleanValue(value);
// Do not allow more to be added when at max length.
if (this.config.max && value.length >= this.config.max.value) {
return false;
}
return true;
}
allowRemove(value) {
// Allow for empty fields.
if (!value) {
return true;
}
value = this.cleanValue(value);
// Do not allow more to be removed when at max length.
if (this.config.min && value.length <= this.config.min.value) {
return false;
}
return true;
}
cleanValue(value) {
if (dataType_1.DataType.isString(value)) {
// Do not count whitespace.
value = value.trim();
}
return value;
}
/**
* Field is considered required when there is a min value..
*/
get isRequired() {
return Boolean(this.config.min && this.config.min.value > 0);
}
validate(value) {
// Allow for empty fields.
// Use the required rule for making sure it exists.
if (!value) {
return null;
}
if (dataType_1.DataType.isArray(value)) {
if (this.config.min && value.length < this.config.min.value) {
return this.config.min.message || this.message;
}
if (this.config.max && value.length > this.config.max.value) {
return this.config.max.message || this.message;
}
}
else {
value = parseFloat(value);
if (isNaN(value)) {
return this.message;
}
if (this.config.min && value < this.config.min.value) {
return this.config.min.message || this.message;
}
if (this.config.max && value > this.config.max.value) {
return this.config.max.message || this.message;
}
}
return null;
}
}
exports.RangeRule = RangeRule;
//# sourceMappingURL=range.js.map