Skip to content

Improved Object Validations with New ValidateObject #13301

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

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from

Conversation

Abhishek-Punhani
Copy link
Contributor

@Abhishek-Punhani Abhishek-Punhani commented Apr 8, 2025

Summary

Implemented improved object validations using validateObject function

  • Replaced ad-hoc validation logic with validateObject function for consistency and maintainability
  • Updated prop validation in components to use validateObject
  • Ensure all object prop validations are thorough and standardized
  • Manual verification performed to ensure no regressions
  • Issue Addressed : standardize validator functions to all Object props  #8903

References

#8903

Reviewer guidance

1. HybridLearningLessonCard.vue

  • Appears on lesson listing or hybrid learning pages.
  • Navigate to the page listing lessons.
  • Verify each lesson card displays correct title, thumbnail, and progress bar.
  • Observe the console for errors.

2. DownloadButton.vue

  • Often appears on resource detail pages.
  • Go to the relevant resource page or file download section.
  • Check that the download button is visible, clicking triggers the correct download.
  • Observe the console for errors.

3. LibraryAndChannelBrowserMainContent.vue

  • Found in the library or channel browsing view.
  • Navigate to the library/channel page.
  • Confirm channel or resource items display and link properly.
  • Observe the console for errors.

Check for any validation failed or invalid prop type erros in console while navigating Learn App

Summary by CodeRabbit

  • New Features
    • Enhanced validation for lesson, content, and file data in various components, ensuring required fields and correct data types.
  • Refactor
    • Improved prop validation logic for better data integrity across multiple components.
  • Style
    • Removed restrictive validation on view style selection to allow more flexibility.

@github-actions github-actions bot added APP: Learn Re: Learn App (content, quizzes, lessons, etc.) DEV: frontend labels Apr 8, 2025
Copy link
Contributor

github-actions bot commented Apr 8, 2025

Copy link
Member

@rtibbles rtibbles left a comment

Choose a reason for hiding this comment

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

Some possible cleanup here - I think @nucleogenesis has commented on this elsewhere as well, that maybe a content node spec could be standardized?

return validateObject(content, {
id: { type: String, required: true },
title: { type: String, required: true },
thumbnail: { type: String, required: false, default: '' },
Copy link
Member

Choose a reason for hiding this comment

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

We should avoid using the 'default' specification when using this for a VueJS component validator, as it does not update the value, and might mislead a developer to think that the defaulted values are always present on the object.

Copy link
Member

Choose a reason for hiding this comment

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

@Abhishek-Punhani I think that this (and the other example @rtibbles noted in his review) should be addressed before merge to avoid committing unnecessary / potentially confusing code

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@rtibbles , I have removed defaults

id: { type: String, required: true },
title: { type: String, required: true },
is_leaf: { type: Boolean, required: true },
copies: { type: Array, required: false, default: () => [] },
Copy link
Member

Choose a reason for hiding this comment

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

Similarly here.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

removing this default triggers vue-errors in the console

preset: { type: String, required: true },
lang: {
type: Object,
required: false,
Copy link
Member

Choose a reason for hiding this comment

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

For objects, you can nest a further spec for the object under the spec key. See here for an example: https://github.com/learningequality/kolibri/blob/develop/packages/kolibri/styles/internal/themeSpec.js#L49

@@ -56,6 +57,15 @@
content: {
type: Object,
required: true,
validator: function (content) {
Copy link
Member

Choose a reason for hiding this comment

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

Seems like we are already validating a content object elsewhere in the code - it might be worth consolidating a definition, rather than repeating parts of this over and over again:

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I have added spec for nested object validation

Copy link
Member

@nucleogenesis nucleogenesis left a comment

Choose a reason for hiding this comment

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

It'd be nice to have a consistent spec for a base content node. It's worth noting though that we'd want to be sure that in certain cases, such as where the spec in LibraryAndChannelBrowserMainContent.vue expects a copies key. So we'd want to be sure we include fields like that directly in the component.

I'm not super sure where we should put the specs - my first sense was alongside the objectSpecs.js file in packages/kolibri/utils (maybe in a subdir) -- but would be interested in @rtibbles thoughts for where it should live

@Abhishek-Punhani
Copy link
Contributor Author

It'd be nice to have a consistent spec for a base content node. It's worth noting though that we'd want to be sure that in certain cases, such as where the spec in LibraryAndChannelBrowserMainContent.vue expects a copies key. So we'd want to be sure we include fields like that directly in the component.

I'm not super sure where we should put the specs - my first sense was alongside the objectSpecs.js file in packages/kolibri/utils (maybe in a subdir) -- but would be interested in @rtibbles thoughts for where it should live

@nucleogenesis , It seems a good idea to have a function like validateContentNode; it will reduce code replication.
I think we can handle that in a different issue.

@Abhishek-Punhani
Copy link
Contributor Author

@nucleogenesis @rtibbles , How do you think we should proceed? I believe we can separate the spec for contentNode validations to improve consistency and reduce redundancy.
I have opened an issue for that #13315

Copy link
Contributor

coderabbitai bot commented May 4, 2025

Walkthrough

The changes introduce stricter runtime validation for props in several Vue components. The validateObject utility is now used to enforce specific object structures for props that accept objects or arrays of objects. In HybridLearningLessonCard.vue, the content prop must match a defined schema. In LibraryAndChannelBrowserMainContent.vue, each item in the contents array must conform to a required shape, and the validator for currentCardViewStyle has been removed. In DownloadButton.vue, each file in the files prop array is validated for required fields and structure.

Changes

File(s) Change Summary
kolibri/plugins/learn/assets/src/views/HybridLearningLessonCard.vue Added a validator to the content prop using validateObject to enforce required and optional fields.
kolibri/plugins/learn/assets/src/views/LibraryAndChannelBrowserMainContent.vue Added a validator to the contents prop enforcing object shape for each array element; removed validator from currentCardViewStyle.
packages/kolibri/components/DownloadButton.vue Added a validator to the files prop to enforce a strict schema for each file object in the array.

Sequence Diagram(s)

sequenceDiagram
    participant ParentComponent
    participant VueComponent

    ParentComponent->>VueComponent: Passes prop (object or array)
    VueComponent->>VueComponent: Runs custom validator (using validateObject)
    alt Validation passes
        VueComponent-->>ParentComponent: Accepts prop, proceeds as normal
    else Validation fails
        VueComponent-->>ParentComponent: Emits Vue warning/error
    end
Loading

📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 85e257e and 2fc6199.

📒 Files selected for processing (1)
  • kolibri/plugins/learn/assets/src/views/HybridLearningLessonCard.vue (2 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • kolibri/plugins/learn/assets/src/views/HybridLearningLessonCard.vue
⏰ Context from checks skipped due to timeout of 90000ms (3)
  • GitHub Check: All file linting
  • GitHub Check: Frontend tests
  • GitHub Check: Build WHL file / Build WHL

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

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)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@Abhishek-Punhani
Copy link
Contributor Author

Abhishek-Punhani commented May 4, 2025

@nucleogenesis @rtibbles , Will this PR be merged after #13315 is resolved or not?
I have made other requested changes and have updated the PR. Please check at your convenience

@Abhishek-Punhani Abhishek-Punhani requested a review from rtibbles May 5, 2025 07:37
@MisRob
Copy link
Member

MisRob commented May 22, 2025

I'm sorry for delay @Abhishek-Punhani, we will follow-up with you.

Copy link
Member

@nucleogenesis nucleogenesis left a comment

Choose a reason for hiding this comment

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

@Abhishek-Punhani the changes here all LGTM thank you again and I apologize for failing to come back to this sooner.

I think that this is probably good to merge, but we should make note in #13315 that we've implemented some specs here that we can update to use any future consolidated specification.

We're probably going to talk a bit more internally about how to consolidate specs (I particularly like @rtibbles 's suggestion to have the spec track the API endpoint). Thanks again for all of your effort and input on this!

@Abhishek-Punhani
Copy link
Contributor Author

@nucleogenesis , Yes, I completely agree! Once #13315 is finalised, we can clear all the tech debt and update the implementation to use the consolidated contentNodeSpec for all contentNodeValidations. I would like to work on the implementation of #13315

@MisRob
Copy link
Member

MisRob commented Jun 19, 2025

Hi @Abhishek-Punhani, thanks a lot for the offer to work on #13315. Looking at the conversation there, it still needs some decisions and I am rather unsure if / when those can be done. Meanwhile, if it's interesting for you, you're welcome to keep an eye on learningequality/studio#5060 where I am adding new issues on weekly basis specifically for our community, and this work has generally higher priority and so we are able to follow-up on everything. I am glad to have you there particularly for more complex issues - you already have lots of experience in our codebases and I've enjoyed collaborating with you.

@Abhishek-Punhani
Copy link
Contributor Author

Sure @MisRob , thanks for the heads-up!
I'll definitely keep an eye on learningequality/studio#5060 and start working on the open issues

@MisRob
Copy link
Member

MisRob commented Jun 30, 2025

Can this be merged @nucleogenesis?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
APP: Learn Re: Learn App (content, quizzes, lessons, etc.) DEV: frontend
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants