vue-forms-builder
Version:
Typescript forms builder package written specifically for Vue
115 lines • 4.08 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.Validators = void 0;
var Validators = /** @class */ (function () {
function Validators() {
}
/**
* Checks if value is empty
* @param value - value to check
* @returns if is empty returns true, if not returns false
*/
Validators._isEmpty = function (value) {
return value === null || ((typeof value === 'string' || value instanceof Array) && value.length === 0);
};
var _a;
_a = Validators;
/**
* Requires the control's value to be non-empty
* @returns null if control's value meets the
* requirements, if not returns an error object with
* required property as true
*/
Validators.required = function (value) {
return !_a._isEmpty(value) ? null : { required: true };
};
/**
* Requires the control's value to be true
* @returns null if control's value meets the
* requirements, if not returns an error object with
* required property as true
*/
Validators.requiredTrue = function (value) {
return value === true ? null : { required: true };
};
/**
* Requires the control's value to match a regex pattern
* @param pattern - provided regex pattern
* @returns null if control's value meets the
* requirements, if not returns an error object with
* pattern property as true
*/
Validators.pattern = function (regex) {
var pattern = function (value) {
if (_a._isEmpty(value))
return null;
return regex.test(value) ? null : { pattern: true };
};
return pattern;
};
/**
* Requires the control's value to be greater than or equal to the provided number.
* @param min - provided min number
* @returns null if control's value meets the
* requirements, if not returns an error object with min
* property as true
*/
Validators.min = function (minValue) {
var min = function (value) {
if (_a._isEmpty(value))
return null;
return value >= minValue ? null : { min: true };
};
return min;
};
/**
* Requires the control's value to be less than or equal to the provided number.
* @param max - provided max number
* @returns null if control's value meets the
* requirements, if not returns an error object with max
* property as true
*/
Validators.max = function (maxValue) {
var max = function (value) {
if (_a._isEmpty(value))
return null;
return value <= maxValue ? null : { max: true };
};
return max;
};
/**
* Requires the length of the control's value to be
* greater than or equal to the provided minimum length
* @param minLength - provided minimum length
* @returns null if control's value meets the
* requirements, if not returns an error object with
* minLength property as true
*/
Validators.minLength = function (minLengthValue) {
var minLength = function (value) {
if (_a._isEmpty(value))
return null;
return value.toString().length >= minLengthValue ? null : { minLength: true };
};
return minLength;
};
/**
* Requires the length of the control's value to be
* less than or equal to the provided maximum length
* @param maxLength - provided maximum length
* @returns null if control's value meets the
* requirements, if not returns an error object with
* maxLength property as true
*/
Validators.maxLength = function (maxLengthValue) {
var maxLength = function (value) {
if (_a._isEmpty(value))
return null;
return value.toString().length <= maxLengthValue ? null : { maxLength: true };
};
return maxLength;
};
return Validators;
}());
exports.Validators = Validators;
//# sourceMappingURL=Validators.js.map