-
-
Notifications
You must be signed in to change notification settings - Fork 8
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
Conversation
📝 WalkthroughWalkthroughThe changes in the Changes
Sequence Diagram(s)sequenceDiagram
participant User
participant MediaFileSelector
participant Validation
User->>MediaFileSelector: Select files
MediaFileSelector->>Validation: validateMappings()
Validation-->>MediaFileSelector: Set isValidMapping
MediaFileSelector->>User: Update UI based on isValidMapping
User->>MediaFileSelector: Confirm selection
Warning There were issues while running some tools. Please review the errors and either fix the tool’s configuration or disable the tool if it’s a critical failure. 🔧 eslint (1.23.1)
src/lib/components/media-file-selector.svelteOops! Something went wrong! :( ESLint: 9.15.0 TypeError: Error while loading rule '@typescript-eslint/no-unused-expressions': Cannot read properties of undefined (reading 'allowShortCircuit') Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media? 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
CodeRabbit Configuration File (
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 1
📜 Review details
Configuration used: CodeRabbit UI
Review profile: ASSERTIVE
📒 Files selected for processing (1)
src/lib/components/media-file-selector.svelte
(4 hunks)
🔇 Additional comments (4)
src/lib/components/media-file-selector.svelte (4)
156-156
: LGTM: Immediate validation after file selection
The addition of validateMappings()
call ensures mapping validity is checked right after file selection, providing immediate feedback.
170-170
: LGTM: Well-placed state variable for tracking mapping validity
The isValidMapping
state variable provides a clear way to track and control the mapping validation state.
496-504
: LGTM: Improved file display and removal handling
Good improvements:
- Overflow handling for long filenames improves UI
- Validation after file removal ensures consistency
543-543
: LGTM: Improved button state management
The button's disabled state now correctly depends on both loading and mapping validity, preventing submission of invalid data.
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; |
There was a problem hiding this comment.
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:
- Add upper bounds validation for season/episode numbers
- Consider validating for duplicate season/episode combinations
- 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.
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; |
Summary by CodeRabbit
New Features
Bug Fixes