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

feat: validate Data and Message models on construction #1139

Merged
merged 4 commits into from
Jan 5, 2025

Conversation

ZauberNerd
Copy link
Contributor

@ZauberNerd ZauberNerd commented Jan 4, 2025

This PR implements validation of Data and Message inputs and sets sane defaults for missing values.

Summary by Sourcery

Implement validation for Data and Message models during construction, setting default values for missing fields.

New Features:

  • Validate Data and Message model inputs upon creation.

Tests:

  • No tests were added in this change, but existing tests now cover the validation logic.

Copy link
Contributor

sourcery-ai bot commented Jan 4, 2025

Reviewer's Guide by Sourcery

This PR implements validation for the Data and Message models during object construction and sets default values for optional fields.

Sequence diagram for JSON validation flow

sequenceDiagram
    participant Client
    participant Data
    participant Message
    Client->>Data: fromJson(jsonMap)
    activate Data
    Data->>Data: Validate messages key exists
    Data->>Data: Validate messages is list
    Data->>Data: Validate messages not empty
    loop For each message
        Data->>Message: fromJson(messageJson)
        activate Message
        Message->>Message: Validate required keys
        Message->>Message: Set default values
        Message-->>Data: Return Message instance
        deactivate Message
    end
    Data-->>Client: Return Data instance
    deactivate Data
Loading

Class diagram showing updated Data and Message models with validation

classDiagram
    class Data {
        +List~Message~ messages
        +fromJson(Map json)
        note for Data "Added validation:
        - messages key exists
        - messages is a list
        - messages not empty"
    }
    class Message {
        +List~String~ text
        +bool flash
        +bool marquee
        +Speed speed
        +Mode mode
        +fromJson(Map json)
        note for Message "Added validation:
        - text, speed, mode keys exist
        - text is a list
        - Default values for flash/marquee"
    }
    Data *-- Message : contains
Loading

File-Level Changes

Change Details Files
Added validation for required fields in Message and Data models.
  • Message model now throws exceptions if required keys (text, speed, mode) are missing from JSON input.
  • Data model now throws exceptions if the messages key is missing, not a list, or an empty list.
  • Message.fromJson now validates the type of text field to ensure it's a list.
lib/bademagic_module/models/messages.dart
lib/bademagic_module/models/data.dart
Set default values for optional fields in Message model.
  • Default values for flash and marquee fields are now set to false if not provided in JSON input.
lib/bademagic_module/models/messages.dart

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time. You can also use
    this command to specify where the summary should be inserted.

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

Copy link
Contributor

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

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

Hey @ZauberNerd - I've reviewed your changes - here's some feedback:

Overall Comments:

  • Consider using custom exception classes (e.g., InvalidMessageFormatException) instead of generic Exception for more specific error handling
Here's what I looked at during the review
  • 🟡 General issues: 3 issues found
  • 🟢 Security: all looks good
  • 🟢 Testing: all looks good
  • 🟢 Complexity: all looks good
  • 🟢 Documentation: all looks good

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

lib/bademagic_module/models/messages.dart Show resolved Hide resolved
lib/bademagic_module/models/data.dart Outdated Show resolved Hide resolved
lib/bademagic_module/models/messages.dart Outdated Show resolved Hide resolved
@Jhalakupadhyay
Copy link
Contributor

Hey @ZauberNerd, it would be best if you could raise PRs by adding the appropriate issue in them. If an issue is not present for the same, raise an issue for it, mentioning whether it is a bug or an enhancement, etc., and then raise a PR for it.

Copy link
Collaborator

@adityastic adityastic left a comment

Choose a reason for hiding this comment

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

Awesome

@adityastic adityastic enabled auto-merge (squash) January 5, 2025 07:55
ZauberNerd and others added 4 commits January 5, 2025 07:55
This is a review suggestion by sourcery-ai.

Co-authored-by: sourcery-ai[bot] <58596630+sourcery-ai[bot]@users.noreply.github.com>
This is a review suggestion by sourcery-ai.

Co-authored-by: sourcery-ai[bot] <58596630+sourcery-ai[bot]@users.noreply.github.com>
@adityastic adityastic merged commit 093c170 into fossasia:flutter_app Jan 5, 2025
5 checks passed
@ZauberNerd ZauberNerd deleted the model-validation branch January 5, 2025 11:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants