This commit is contained in:
znetsixe
2026-03-11 11:13:05 +01:00
parent c60aa40666
commit 27a6d3c709
20 changed files with 1555 additions and 229 deletions

View File

@@ -39,8 +39,21 @@ class ValidationUtils {
const loggerEnabled = IloggerEnabled ?? true;
const loggerLevel = IloggerLevel ?? "warn";
this.logger = new Logger(loggerEnabled, loggerLevel, 'ValidationUtils');
this._onceLogCache = new Set();
}
_logOnce(level, onceKey, message) {
if (onceKey && this._onceLogCache.has(onceKey)) {
return;
}
if (onceKey) {
this._onceLogCache.add(onceKey);
}
if (typeof this.logger?.[level] === "function") {
this.logger[level](message);
}
}
constrain(value, min, max) {
if (typeof value !== "number") {
this.logger?.warn(`Value '${value}' is not a number. Defaulting to ${min}.`);
@@ -96,7 +109,7 @@ class ValidationUtils {
continue;
}
} else {
this.logger.info(`There is no value provided for ${name}.${key}. Using default value.`);
this.logger.debug(`No value provided for ${name}.${key}. Using default value.`);
configValue = fieldSchema.default;
}
//continue;
@@ -390,19 +403,52 @@ class ValidationUtils {
return fieldSchema.default;
}
const keyString = `${name}.${key}`;
const normalizeMode = rules.normalize || this._resolveStringNormalizeMode(keyString);
const preserveCase = normalizeMode !== "lowercase";
// Check for uppercase characters and convert to lowercase if present
if (newConfigValue !== newConfigValue.toLowerCase()) {
this.logger.warn(`${name}.${key} contains uppercase characters. Converting to lowercase: ${newConfigValue} -> ${newConfigValue.toLowerCase()}`);
if (!preserveCase && newConfigValue !== newConfigValue.toLowerCase()) {
this._logOnce(
"info",
`normalize-lowercase:${keyString}`,
`${name}.${key} normalized to lowercase: ${newConfigValue} -> ${newConfigValue.toLowerCase()}`
);
newConfigValue = newConfigValue.toLowerCase();
}
return newConfigValue;
}
_isUnitLikeField(path) {
const normalized = String(path || "").toLowerCase();
if (!normalized) return false;
return /(^|\.)([a-z0-9]*unit|units)(\.|$)/.test(normalized)
|| normalized.includes(".curveunits.");
}
_resolveStringNormalizeMode(path) {
const normalized = String(path || "").toLowerCase();
if (!normalized) return "none";
if (this._isUnitLikeField(normalized)) return "none";
if (normalized.endsWith(".name")) return "none";
if (normalized.endsWith(".model")) return "none";
if (normalized.endsWith(".supplier")) return "none";
if (normalized.endsWith(".role")) return "none";
if (normalized.endsWith(".description")) return "none";
if (normalized.endsWith(".softwaretype")) return "lowercase";
if (normalized.endsWith(".type")) return "lowercase";
if (normalized.endsWith(".category")) return "lowercase";
return "lowercase";
}
validateSet(configValue, rules, fieldSchema, name, key) {
// 1. Ensure we have a Set. If not, use default.
if (!(configValue instanceof Set)) {
this.logger.info(`${name}.${key} is not a Set. Converting to one using default value.`);
this.logger.debug(`${name}.${key} is not a Set. Converting to one using default value.`);
return new Set(fieldSchema.default);
}
@@ -426,9 +472,10 @@ class ValidationUtils {
.slice(0, rules.maxLength || Infinity);
// 4. Check if the filtered array meets the minimum length.
if (validatedArray.length < (rules.minLength || 1)) {
const minLength = Number.isInteger(rules.minLength) ? rules.minLength : 0;
if (validatedArray.length < minLength) {
this.logger.warn(
`${name}.${key} contains fewer items than allowed (${rules.minLength}). Using default value.`
`${name}.${key} contains fewer items than allowed (${minLength}). Using default value.`
);
return new Set(fieldSchema.default);
}
@@ -439,7 +486,7 @@ class ValidationUtils {
validateArray(configValue, rules, fieldSchema, name, key) {
if (!Array.isArray(configValue)) {
this.logger.info(`${name}.${key} is not an array. Using default value.`);
this.logger.debug(`${name}.${key} is not an array. Using default value.`);
return fieldSchema.default;
}
@@ -460,9 +507,10 @@ class ValidationUtils {
})
.slice(0, rules.maxLength || Infinity);
if (validatedArray.length < (rules.minLength || 1)) {
const minLength = Number.isInteger(rules.minLength) ? rules.minLength : 0;
if (validatedArray.length < minLength) {
this.logger.warn(
`${name}.${key} contains fewer items than allowed (${rules.minLength}). Using default value.`
`${name}.${key} contains fewer items than allowed (${minLength}). Using default value.`
);
return fieldSchema.default;
}