Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: improve mapping validation logic #180

Merged
merged 1 commit into from
Nov 25, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 14 additions & 4 deletions src/lib/components/media-file-selector.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@
...extractSeasonEpisode(containerFile.filename)
};
});
validateMappings();
step = 4;
}
function extractSeasonEpisode(filename: string): { season?: number; episode?: number } {
Expand All @@ -166,16 +167,24 @@
return {};
}

let isValidMapping = false;

function validateMappings(): boolean {
if (mediaType === 'movie') return true;
if (mediaType === 'movie') {
isValidMapping = true;
return true;
}

return selectedFilesMappings.every(
const isValid = selectedFilesMappings.every(
(file) =>
typeof file.season === 'number' &&
typeof file.episode === 'number' &&
file.season > 0 &&
file.episode > 0
);

isValidMapping = isValid;
return isValid;
Comment on lines +173 to +187
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick (assertive)

Consider enhancing the validation logic

While the current validation works, consider these improvements:

  1. Add upper bounds validation for season/episode numbers
  2. Consider validating for duplicate season/episode combinations
  3. Add validation for negative numbers (currently only checks > 0)
 function validateMappings(): boolean {
   if (mediaType === 'movie') {
     isValidMapping = true;
     return true;
   }

+  // Check for duplicates
+  const episodeMap = new Set();
   const isValid = selectedFilesMappings.every(
-    (file) =>
-      typeof file.season === 'number' &&
-      typeof file.episode === 'number' &&
-      file.season > 0 &&
-      file.episode > 0
+    (file) => {
+      if (
+        typeof file.season !== 'number' ||
+        typeof file.episode !== 'number' ||
+        file.season <= 0 ||
+        file.episode <= 0 ||
+        file.season > 100 || // reasonable upper bound
+        file.episode > 100   // reasonable upper bound
+      ) {
+        return false;
+      }
+      const key = `${file.season}-${file.episode}`;
+      if (episodeMap.has(key)) {
+        return false; // duplicate episode
+      }
+      episodeMap.add(key);
+      return true;
+    }
   );

   isValidMapping = isValid;
   return isValid;
 }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
if (mediaType === 'movie') {
isValidMapping = true;
return true;
}
return selectedFilesMappings.every(
const isValid = selectedFilesMappings.every(
(file) =>
typeof file.season === 'number' &&
typeof file.episode === 'number' &&
file.season > 0 &&
file.episode > 0
);
isValidMapping = isValid;
return isValid;
if (mediaType === 'movie') {
isValidMapping = true;
return true;
}
// Check for duplicates
const episodeMap = new Set();
const isValid = selectedFilesMappings.every(
(file) => {
if (
typeof file.season !== 'number' ||
typeof file.episode !== 'number' ||
file.season <= 0 ||
file.episode <= 0 ||
file.season > 100 || // reasonable upper bound
file.episode > 100 // reasonable upper bound
) {
return false;
}
const key = `${file.season}-${file.episode}`;
if (episodeMap.has(key)) {
return false; // duplicate episode
}
episodeMap.add(key);
return true;
}
);
isValidMapping = isValid;
return isValid;

}

async function updateAttributes() {
Expand Down Expand Up @@ -484,14 +493,15 @@
<Card.Root class="w-full min-w-0">
<Card.Content class="p-4">
<div class="grid gap-4">
<div class="flex items-center gap-2">
<div class="flex items-center gap-2 overflow-auto">
<FileIcon class="h-4 w-4 flex-shrink-0" />
<span class="flex-1 truncate">{file.filename}</span>
<button
on:click={() => {
selectedFilesMappings = selectedFilesMappings.filter(
(f) => f.id !== file.id
);
validateMappings();
}}
>
<CircleX
Expand Down Expand Up @@ -530,7 +540,7 @@

<Button
on:click={updateAttributes}
disabled={loading || !validateMappings()}
disabled={loading || !isValidMapping}
class="mt-4 w-full"
>
{#if loading}
Expand Down