Files
clean-architecture-frontend…/src/contracts/validation-issues.ts
T

57 lines
1.7 KiB
TypeScript

export const MAX_VALIDATION_ISSUES = 50;
export const MAX_VALIDATION_ISSUE_CANDIDATES = 200;
export const MAX_VALIDATION_PATH_LENGTH = 120;
export const MAX_VALIDATION_CODE_LENGTH = 80;
export type ValidationIssue = Readonly<{
path: string;
code: string;
}>;
function normalizePath(value: unknown): string | null {
if (typeof value === "string") return value;
if (!Array.isArray(value)) return null;
try {
return value.map((segment) => String(segment)).join(".");
} catch {
return null;
}
}
/**
* Projects validation metadata into a fixed-size, copy-only representation.
* Invalid entries are skipped, and candidate scanning is itself bounded so a
* hostile issue array cannot turn failure handling into unbounded work.
*/
export function projectValidationIssues(value: unknown): readonly ValidationIssue[] {
if (!Array.isArray(value)) return Object.freeze([]);
const projected: ValidationIssue[] = [];
const candidateCount = Math.min(
value.length,
MAX_VALIDATION_ISSUE_CANDIDATES,
);
for (let index = 0; index < candidateCount; index += 1) {
if (projected.length >= MAX_VALIDATION_ISSUES) break;
try {
const candidate = value[index];
if (!candidate || typeof candidate !== "object") continue;
const record = candidate as Readonly<Record<string, unknown>>;
const path = normalizePath(record.path);
const code = record.code;
if (
path === null ||
typeof code !== "string" ||
path.length > MAX_VALIDATION_PATH_LENGTH ||
code.length > MAX_VALIDATION_CODE_LENGTH
) {
continue;
}
projected.push(Object.freeze({ path, code }));
} catch {
continue;
}
}
return Object.freeze(projected);
}