UNPKG

@calf/common

Version:
399 lines (398 loc) 12.3 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ValidationResult = void 0; // Enums var boundaries_enum_1 = require("../enums/boundaries.enum"); /** * Validation result * @description Object being handled between function calls */ var ValidationResult = /** @class */ (function () { /** * Constructor * @param data */ function ValidationResult(data) { // Assign data this.data = data; // Set is valid flag this.isValid = true; // Init arrays this.errors = []; this.warnings = []; this.messages = []; } /** * Add error * @param error */ ValidationResult.prototype.addError = function (error) { // Add error this.errors.push(error); // Set validation flag this.isValid = false; }; /** * Add error * @param error */ ValidationResult.prototype.addUniqueError = function (error) { // Check if error already exists if (!this.errors.includes(error)) { // Add error this.errors.push(error); } }; /** * Add warning * @param warning */ ValidationResult.prototype.addUniqueWarning = function (warning) { // Check if warning already exists if (!this.warnings.includes(warning)) { // Add warning this.warnings.push(warning); } }; /** * Add warning * @param warning */ ValidationResult.prototype.addWarning = function (warning) { // Add warning this.warnings.push(warning); }; /** * Add message * @param message */ ValidationResult.prototype.addMessage = function (message) { // Check if message already exists if (!this.messages.includes(message)) { // Add message this.messages.push(message); } }; /** * Add message * @param message */ ValidationResult.prototype.addUniqueMessage = function (message) { // Add message this.messages.push(message); }; /** * Append validations * @param validations */ ValidationResult.prototype.append = function () { var _this = this; var validations = []; for (var _i = 0; _i < arguments.length; _i++) { validations[_i] = arguments[_i]; } // Iterate validations validations.forEach(function (v) { // Update is valid flag _this.isValid = _this.isValid && v.isValid; // Concat lists _this.errors = _this.errors.concat(v.errors); _this.warnings = _this.warnings.concat(v.warnings); _this.messages = _this.messages.concat(v.messages); }); // Return current validation flag return this.isValid; }; /** * Append unique validations * @param validations */ ValidationResult.prototype.appendUnique = function () { var _this = this; var validations = []; for (var _i = 0; _i < arguments.length; _i++) { validations[_i] = arguments[_i]; } // Iterate validations validations.forEach(function (v) { // Update isValid flag _this.isValid = _this.isValid && v.isValid; // Append unique errors v.errors.forEach(function (err) { if (!_this.errors.includes(err)) { _this.errors.push(err); } }); // Append unique warnings v.warnings.forEach(function (warn) { if (!_this.warnings.includes(warn)) { _this.warnings.push(warn); } }); // Append unique messages v.messages.forEach(function (msg) { if (!_this.messages.includes(msg)) { _this.messages.push(msg); } }); }); // Return current validation flag return this.isValid; }; /** * Append as warnings * @param validations * @description Append validations and converts its * errors to warnings. Does not modify validation flag */ ValidationResult.prototype.appendAsWarnings = function () { var _this = this; var validations = []; for (var _i = 0; _i < arguments.length; _i++) { validations[_i] = arguments[_i]; } // Iterate validations validations.forEach(function (v) { // Concat lists _this.warnings = _this.warnings.concat(v.warnings, v.errors); _this.messages = _this.messages.concat(v.messages); }); // Return current validation flag return this.isValid; }; /** * Validate * @description Validate using custom validation function * @param fn * @param error * @returns */ ValidationResult.prototype.validate = function (fn, error) { // Process result from custom function return this.processResult(fn(this.data), error); }; /** * Is true * @param selector * @param error */ ValidationResult.prototype.isTrue = function (selector, error) { return this.processResult(selector(this.data), error); }; /** * Is false * @param selector * @param error */ ValidationResult.prototype.isFalse = function (selector, error) { return this.processResult(!selector(this.data), error); }; /** * Must * @param expression * @param error */ ValidationResult.prototype.must = function (expression, error) { return this.processResult(expression, error); }; /** * Is defined * @param selector * @param error */ ValidationResult.prototype.isDefined = function (selector, error) { // Get value var value = selector(this.data); // Get result return this.processResult(typeof value !== 'undefined' && value !== null, error); }; /** * Is not empty * @param selector * @param error */ ValidationResult.prototype.isNotEmpty = function (selector, error) { return this.processResult(!!selector(this.data).length, error); }; /** * Is defined and not empty * @param selector * @param error */ ValidationResult.prototype.isDefinedAndNotEmpty = function (selector, error) { return this.isDefined(selector, error) && this.isNotEmpty(selector, error); }; /** * Contains * @param selector * @param value * @param error */ ValidationResult.prototype.contains = function (selector, value, error) { return this.processResult(selector(this.data).indexOf(value) !== -1, error); }; /** * Starts with * @param selector * @param substring * @param error */ ValidationResult.prototype.startsWith = function (selector, substring, error) { return this.processResult(selector(this.data).startsWith(substring), error); }; /** * Ends with * @param selector * @param substring * @param error */ ValidationResult.prototype.endsWith = function (selector, substring, error) { return this.processResult(selector(this.data).endsWith(substring), error); }; /** * Is of length * @param selector * @param length * @param error */ ValidationResult.prototype.isOfLength = function (selector, length, error) { return this.processResult(selector(this.data).length === length, error); }; /** * Is longer * @param selector * @param length * @param error */ ValidationResult.prototype.isLonger = function (selector, length, error) { return this.processResult(selector(this.data).length > length, error); }; /** * Is shorter * @param selector * @param length * @param error */ ValidationResult.prototype.isShorter = function (selector, length, error) { return this.processResult(selector(this.data).length < length, error); }; /** * Is longer or of length * @param selector * @param length * @param error */ ValidationResult.prototype.isLongerOrOfLength = function (selector, length, error) { // Get value var value = selector(this.data); // Process result return this.processResult(value.length === length || value.length > length, error); }; /** * Is shorter or of length * @param selector * @param length * @param error */ ValidationResult.prototype.isShorterOfLength = function (selector, length, error) { // Get value var value = selector(this.data); // Process result return this.processResult(value.length === length || value.length < length, error); }; /** * Is equal * @param selector * @param value * @param error */ ValidationResult.prototype.isEqual = function (selector, value, error) { return this.processResult(selector(this.data) === value, error); }; /** * Is greater * @param selector * @param value * @param error */ ValidationResult.prototype.isGreater = function (selector, value, error) { return this.processResult(selector(this.data) > value, error); }; /** * Is between * @param selector * @param start * @param boundaries * @param end * @param error */ ValidationResult.prototype.isBetween = function (selector, min, max, boundaries, error) { // Get value var lValue = selector(this.data); // Check boundaries switch (boundaries) { // Exclusive case boundaries_enum_1.Boundaries.Exclusive: return this.processResult(lValue > min && lValue < max, error); // Inclusive/default case boundaries_enum_1.Boundaries.Inclusive: default: return this.processResult(lValue >= min && lValue <= max, error); } }; /** * Is greater or equal * @param selector * @param value * @param error */ ValidationResult.prototype.isGreaterOrEqual = function (selector, value, error) { // Get value var lValue = selector(this.data); // Process result return this.processResult(lValue === value || lValue > value, error); }; /** * Is lesser * @param selector * @param value * @param error */ ValidationResult.prototype.isLesser = function (selector, value, error) { return this.processResult(selector(this.data) < value, error); }; /** * Is lesser or equal * @param selector * @param value * @param error */ ValidationResult.prototype.isLesserOrEqual = function (selector, value, error) { // Get value var lValue = selector(this.data); // Process result return this.processResult(lValue === value || lValue < value, error); }; /** * Is match * @param selector * @param regexp * @param error */ ValidationResult.prototype.isMatch = function (selector, regexp, error) { return this.processResult(regexp.test(selector(this.data)), error); }; /** * Process result * @param result * @param message */ ValidationResult.prototype.processResult = function (result, message) { // Add error if result was negative !result && this.addError(message); // Return result return result; }; return ValidationResult; }()); exports.ValidationResult = ValidationResult;