The parsed data to validate
Validation result with errors, warnings, and statistics
export function validateImportedMatchData(
data: unknown,
): ImportValidationResult {
const errors: string[] = [];
const warnings: string[] = [];
let matchCount = 0;
let duplicateCount = 0;
// Check if data is an object
if (!data || typeof data !== "object") {
errors.push("Import data must be a JSON object");
return { valid: false, errors, warnings, matchCount, duplicateCount };
}
const importedData = data as Record<string, unknown>;
// Validate metadata
validateMetadata(importedData, errors);
// Check for matches array
if (!Array.isArray(importedData.matches)) {
errors.push("Missing required field: matches (must be an array)");
return { valid: false, errors, warnings, matchCount, duplicateCount };
}
// Validate each match
const seenIds = new Set<number | string>();
const seenTitles = new Set<string>();
for (let i = 0; i < importedData.matches.length; i++) {
const isValid = validateMatchEntry(
importedData.matches[i],
i,
seenIds,
seenTitles,
errors,
warnings,
);
if (isValid) {
matchCount++;
} else {
duplicateCount++;
}
}
// If we have critical errors, mark as invalid
const valid = errors.length === 0;
return {
valid,
errors,
warnings,
matchCount,
duplicateCount,
};
}
Validates the structure and content of imported match data. Checks for required fields, valid structure, and duplicate detection.