kist
Version:
Package Pipeline Processor
129 lines (128 loc) • 4.57 kB
JavaScript
;
// ============================================================================
// Import
// ============================================================================
Object.defineProperty(exports, "__esModule", { value: true });
exports.AbstractValidator = void 0;
const AbstractProcess_1 = require("../abstract/AbstractProcess");
// ============================================================================
// Class
// ============================================================================
/**
* AbstractValidator provides a base class for validation.
* Extends AbstractProcess for consistent logging and validation utility
* methods.
* Subclasses should implement the specific `validateProperty` method for
* custom validation logic.
*/
class AbstractValidator extends AbstractProcess_1.AbstractProcess {
// Parameters
// ========================================================================
// Constructor
// ========================================================================
constructor() {
super();
}
// Abstract Methods
// ========================================================================
/**
* Validates an entire object.
* @param target - The object to validate.
* @throws Error if validation fails for any property.
*/
validate(target) {
if (!target || typeof target !== "object") {
throw new Error("Target must be a valid object.");
}
for (const key in target) {
if (Object.prototype.hasOwnProperty.call(target, key)) {
this.validateProperty(key, target[key]);
}
}
this.logInfo("Validation completed successfully.");
}
// Validation Methods
// ========================================================================
/**
* Validates a numeric value.
*
* @param key - The key being validated.
* @param value - The numeric value to validate.
* @throws Error if the value is not a non-negative number.
*/
validateNumber(key, value) {
if (typeof value !== "number" || value < 0) {
this.throwValidationError(key, value, "Must be a non-negative number.");
}
}
/**
* Validates a boolean value.
*
* @param key - The key being validated.
* @param value - The boolean value to validate.
* @throws Error if the value is not a boolean.
*/
validateBoolean(key, value) {
if (typeof value !== "boolean") {
this.throwValidationError(key, value, "Must be a boolean.");
}
}
/**
* Validates a string value.
*
* @param key - The key being validated.
* @param value - The string value to validate.
* @throws Error if the value is not a non-empty string.
*/
validateString(key, value) {
if (typeof value !== "string" || value.trim() === "") {
this.throwValidationError(key, value, "Must be a non-empty string.");
}
}
/**
* Validates an object value.
*
* @param key - The key being validated.
* @param value - The object value to validate.
* @throws Error if the value is not a valid object.
*/
validateObject(key, value) {
if (typeof value !== "object" ||
value === null ||
Array.isArray(value)) {
this.throwValidationError(key, value, "Must be a valid object.");
}
}
// Utility Methods
// ========================================================================
/**
* Logs validation success for a property.
* @param key - The property key.
* @param value - The value of the property.
*/
logValidationSuccess(key, value) {
const message = `
Validation successful for property "${String(key)}"
with value: ${JSON.stringify(value)}
`;
this.logSuccess(message);
}
/**
* Throws a standardized validation error.
*
* @param key - The key being validated.
* @param value - The invalid value.
* @param message - Additional error message.
* @throws Error with a formatted message.
*/
throwValidationError(key, value, message) {
const errorMessage = `
Validation failed for "${String(key)}"
with value "${JSON.stringify(value)}".
${message}
`;
this.logError(errorMessage);
throw new Error(errorMessage);
}
}
exports.AbstractValidator = AbstractValidator;