kist
Version:
Lightweight Package Pipeline Processor with Plugin Architecture
75 lines • 3.24 kB
JavaScript
import { AbstractValidator } from "../abstract/AbstractValidator.js";
export class OptionsValidator extends AbstractValidator {
constructor() {
super();
this.logDebug("OptionsValidator initialized.");
}
validateProperty(key, value) {
if (value === undefined) {
this.throwValidationError(key, value, `Option "${String(key)}" cannot be undefined.`);
return;
}
const allowedValues = OptionsValidator.allowedValues[key];
if (allowedValues && !allowedValues.includes(value)) {
this.throwValidationError(key, value, `Invalid value "${value}" for option "${String(key)}". Allowed values are: ${allowedValues.join(", ")}.`);
return;
}
this.validateByType(key, value);
this.logValidationSuccess(key, value);
}
validateByType(key, value) {
switch (key) {
case "stepTimeout":
case "maxConcurrentStages":
if (typeof value === "number" && value >= 0) {
this.validateNumber(key, value);
}
else {
this.throwValidationError(key, value, "Must be a non-negative number.");
}
break;
case "haltOnFailure":
case "tags":
if (this.isValidObject(value)) {
this.validateObject(key, value);
}
else {
this.throwValidationError(key, value, "Must be a valid object.");
}
break;
case "live":
this.validateLiveOptions(value);
break;
default:
if (typeof value === "string" && value.trim() !== "") {
this.validateString(key, value);
}
else {
this.throwValidationError(key, value, "Must be a non-empty string.");
}
}
}
validateLiveOptions(value) {
if ((value === null || value === void 0 ? void 0 : value.port) && (value.port < 1 || value.port > 65535)) {
this.throwValidationError("live.port", value.port, "Port must be a number between 1 and 65535.");
}
if ((value === null || value === void 0 ? void 0 : value.root) && typeof value.root !== "string") {
this.throwValidationError("live.root", value.root, "Root must be a valid string path.");
}
if ((value === null || value === void 0 ? void 0 : value.watchPaths) && !Array.isArray(value.watchPaths)) {
this.throwValidationError("live.watchPaths", value.watchPaths, "Must be an array of paths.");
}
if ((value === null || value === void 0 ? void 0 : value.ignoredPaths) && !Array.isArray(value.ignoredPaths)) {
this.throwValidationError("live.ignoredPaths", value.ignoredPaths, "Must be an array of paths.");
}
}
isValidObject(value) {
return (typeof value === "object" &&
value !== null &&
!Array.isArray(value));
}
}
OptionsValidator.allowedValues = {
logLevel: ["debug", "info", "warn", "error"],
};
//# sourceMappingURL=OptionsValidator.js.map