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: validation support filters #549

Merged
merged 9 commits into from
Jul 11, 2024
Merged

feat: validation support filters #549

merged 9 commits into from
Jul 11, 2024

Conversation

devhaozi
Copy link
Member

@devhaozi devhaozi commented Jul 10, 2024

📑 Description

Closes goravel/goravel#416

Summary by CodeRabbit

  • New Features

    • Introduced filtering capability within the validation process, enabling data manipulation such as trimming spaces before validation.
  • Bug Fixes

    • Corrected validation logic to handle filters and prevent duplicate filter names.
  • Tests

    • Added and updated test cases to include filters for data manipulation before validation.

✅ Checks

  • Added test cases for my code

@devhaozi devhaozi self-assigned this Jul 10, 2024
@devhaozi devhaozi requested a review from a team as a code owner July 10, 2024 19:54
Copy link
Contributor

coderabbitai bot commented Jul 10, 2024

Walkthrough

The recent updates introduce a filters parameter in various validation interfaces and methods. This enhancement allows for specifying custom filters for data validation. Test functions were also updated to include these filters. Additionally, the Bind method in Validator was refined to correct an assignment operation. These changes address the feature request for adding custom filters for validation.

Changes

File Change Summary
contracts/validation/validation.go Added filters parameter to the Make method in the Validation interface.
contracts/http/request.go Added filters parameter to the Validate method in the ContextRequest interface.
validation/errors_test.go Updated test functions to include filters field in the test data struct.
validation/validation.go Added filters parameter to the Make method and logic for handling filters and duplicates.
validation/validator.go Corrected assignment operator in Bind method.
validation/validation_test.go Added options for filtering and trimming data in test cases.
validation/validator_test.go Added filters field to test struct and updated test cases accordingly.

Sequence Diagram(s)

sequenceDiagram
    participant Client
    participant ContextRequest
    participant Validation
    participant Validator

    Client->>ContextRequest: Validate(data, rules, filters)
    ContextRequest->>Validation: Make(data, rules, filters)
    Validation->>Validator: ApplyFilters(data, filters)
    Validator->>Validation: Validate(data, rules)
    Validation-->>ContextRequest: Validator
    ContextRequest-->>Client: Validation result
Loading

Assessment against linked issues

Objective Addressed Explanation
Support add custom filter according to [reference link] (#416)
Add a Filter method in goravel/framework:validation/options.go (#416)
Add unit tests for the new logic (#416)

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?

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>.
    • 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 generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @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.

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 as 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 resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configration 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.

@devhaozi devhaozi closed this Jul 10, 2024
@devhaozi devhaozi reopened this Jul 10, 2024
Copy link

codecov bot commented Jul 10, 2024

Codecov Report

Attention: Patch coverage is 90.90909% with 5 lines in your changes missing coverage. Please review.

Project coverage is 70.02%. Comparing base (32e0720) to head (5ec4b1e).

Files Patch % Lines
validation/validation.go 92.59% 2 Missing and 2 partials ⚠️
validation/validator.go 0.00% 0 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master     #549      +/-   ##
==========================================
+ Coverage   69.91%   70.02%   +0.10%     
==========================================
  Files         180      180              
  Lines       11053    11106      +53     
==========================================
+ Hits         7728     7777      +49     
- Misses       2752     2754       +2     
- Partials      573      575       +2     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

Outside diff range, codebase verification and nitpick comments (1)
validation/validation_test.go (1)

23-23: Add a brief comment explaining the filters field.

To improve readability, add a comment explaining what the filters field is used for in the test cases.

+ // filters specify data manipulation rules before validation.
filters            map[string]string
Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between a522419 and 29c2b31.

Files ignored due to path filters (1)
  • mocks/validation/Validation.go is excluded by !mocks/**
Files selected for processing (6)
  • contracts/validation/validation.go (1 hunks)
  • validation/errors_test.go (10 hunks)
  • validation/validation.go (6 hunks)
  • validation/validation_test.go (130 hunks)
  • validation/validator.go (1 hunks)
  • validation/validator_test.go (13 hunks)
Files not summarized due to errors (1)
  • validation/validation_test.go: Error: Message exceeds token limit
Additional comments not posted (58)
contracts/validation/validation.go (1)

7-7: Verify the impact of the new filters parameter in the Make method.

Adding the filters parameter to the Make method changes the interface and requires all implementations to support this new parameter. Ensure that all implementations of the Validation interface are updated accordingly.

validation/validator.go (1)

43-43: LGTM! Proper error handling in the Bind method.

Changing the assignment operator from := to = ensures that the err variable is not re-declared within the method scope, which is crucial for accurate error handling.

validation/errors_test.go (19)

17-17: LGTM! Added filters field to test cases.

The addition of the filters field in test data structs ensures that the new filter functionality is tested.


25-25: LGTM! Updated test cases to include filters.

The test case ensures that the filter trim is correctly applied to the data.


31-31: LGTM! Updated test cases to include filters.

The test case ensures that the filter trim is correctly applied and validation errors are checked.


38-38: LGTM! Updated test cases to include filters.

The test case ensures that the filter trim is correctly applied and custom messages are handled.


48-48: LGTM! Updated test cases to include filters.

The test case ensures that the filter trim is correctly applied and custom attributes are handled.


58-58: LGTM! Updated test cases to include filters.

The test case ensures that the filter trim is correctly applied and both custom messages and attributes are handled.


72-72: LGTM! Verified the new filters parameter in the Make method call.

The test case correctly includes the new filters parameter when calling the Make method.


88-88: LGTM! Added filters field to test cases.

The addition of the filters field in test data structs ensures that the new filter functionality is tested.


96-96: LGTM! Updated test cases to include filters.

The test case ensures that the filters trim are correctly applied to the data.


102-102: LGTM! Updated test cases to include filters.

The test case ensures that the filters trim are correctly applied and validation errors are checked.


113-113: LGTM! Verified the new filters parameter in the Make method call.

The test case correctly includes the new filters parameter when calling the Make method.


131-131: LGTM! Added filters field to test cases.

The addition of the filters field in test data structs ensures that the new filter functionality is tested.


138-138: LGTM! Updated test cases to include filters.

The test case ensures that the filters trim are correctly applied to the data.


145-145: LGTM! Updated test cases to include filters.

The test case ensures that the filters trim are correctly applied and validation errors are checked.


158-158: LGTM! Verified the new filters parameter in the Make method call.

The test case correctly includes the new filters parameter when calling the Make method.


173-173: LGTM! Added filters field to test cases.

The addition of the filters field in test data structs ensures that the new filter functionality is tested.


180-180: LGTM! Updated test cases to include filters.

The test case ensures that the filters trim are correctly applied to the data.


186-186: LGTM! Updated test cases to include filters.

The test case ensures that the filters trim are correctly applied and validation errors are checked.


196-196: LGTM! Verified the new filters parameter in the Make method call.

The test case correctly includes the new filters parameter when calling the Make method.

validation/validation.go (4)

25-25: LGTM! Updated Make method to include filters.

The Make method now includes a filters parameter, enhancing the validation framework's functionality.


54-54: LGTM! Verified the new filters parameter in the Make method call.

The method correctly includes the new filters parameter when calling GenerateOptions.


75-82: LGTM! Added logic to check for duplicate filter names.

The logic ensures that duplicate filter names are not added, enhancing the robustness of the validation framework.


327-378: LGTM! Added method to get existing filter names.

The method existFilterNames provides a list of existing filter names, which is used to check for duplicates.

validation/validator_test.go (22)

28-32: Addition of filters parameter in the test structure.

The inclusion of the filters map in the test structure is appropriate for testing the new filter functionality.


35-41: Test case for trimming whitespace from a map value.

The test case correctly verifies the trimming of whitespace using the new filters parameter.


44-50: Test case for integer validation from a map value.

The test case correctly verifies the integer validation using the new filters parameter.


53-59: Test case for casting a string to an integer without filters.

The test case correctly verifies the casting of a string to an integer without applying any filters.


62-68: Test case for handling validation errors with filters.

The test case correctly verifies the handling of validation errors and the application of filters.


72-78: Test case for trimming whitespace from an uppercase map key.

The test case correctly verifies the trimming of whitespace from an uppercase map key using the new filters parameter.


Line range hint 83-93:
Test case for trimming whitespace from a struct field.

The test case correctly verifies the trimming of whitespace from a struct field using the new filters parameter.


104-111: Test case for handling lowercase struct field.

The test case correctly verifies the handling of a lowercase struct field and the application of filters.


123-131: Test case for handling nested struct fields.

The test case correctly verifies the handling of nested struct fields and the application of filters.


139-147: Test case for trimming whitespace from a GET request parameter.

The test case correctly verifies the trimming of whitespace from a GET request parameter using the new filters parameter.


162-163: Potential issue with filter type.

The filter type for the integer parameter should be reviewed. The trim filter might not be appropriate for an integer.

Ensure that the trim filter is intended for this test case. If not, update the filter to an appropriate one for integers.


171-180: Test case for trimming whitespace from a POST request parameter.

The test case correctly verifies the trimming of whitespace from a POST request parameter using the new filters parameter.


194-195: Test case for POST request with file upload.

The test case correctly verifies the handling of a POST request with a file upload and the application of filters.


Line range hint 210-215:
Integration of filters parameter in the validation logic.

The integration of the filters parameter in the validation logic is appropriate and ensures the filters are applied during the test execution.


228-229: Addition of filters parameter in the test structure.

The inclusion of the filters map in the test structure is appropriate for testing the new filter functionality.


235-235: Test case for validation failure without filters.

The test case correctly verifies the validation failure without applying any filters.


241-241: Test case for validation failure with filters.

The test case correctly verifies the validation failure with filters applied.


251-254: Integration of filters parameter in the validation logic.

The integration of the filters parameter in the validation logic is appropriate and ensures the filters are applied during the test execution.


282-283: Addition of filters parameter in the test structure.

The inclusion of the filters map in the test structure is appropriate for testing the new filter functionality.


335-335: Test case for validation without casting data.

The test case correctly verifies the validation without casting data and ensures the filters are applied.


403-403: Test case for validation with casting data.

The test case correctly verifies the validation with casting data and ensures the filters are applied.


428-431: Integration of filters parameter in the validation logic.

The integration of the filters parameter in the validation logic is appropriate and ensures the filters are applied during the test execution.

validation/validation_test.go (11)

33-35: Ensure comprehensive filter scenarios.

The test case for map[string]any data type includes a filter for trimming whitespace. Ensure that other common filters (e.g., lowercase, uppercase) are also tested in different scenarios.

Would you like to add more test cases to cover additional filters?


41-43: Ensure comprehensive filter scenarios for struct data.

The test case for struct data includes a filter for trimming whitespace. Ensure that other common filters (e.g., lowercase, uppercase) are also tested in different scenarios.

Would you like to add more test cases to cover additional filters for struct data?


49-51: Check error handling for invalid filter usage.

The test case for invalid data types includes a filter. Ensure that the error handling for invalid filter usage is tested.

Would you like to add test cases to cover invalid filter usage scenarios?


58-58: Check error handling for empty data with filters.

The test case for empty data includes a filter. Ensure that the error handling for empty data with filters is tested.

Would you like to add test cases to cover empty data scenarios with different filters?


65-65: Check error handling for empty rules with filters.

The test case for empty rules includes filters. Ensure that the error handling for empty rules with filters is tested.

Would you like to add test cases to cover empty rules scenarios with different filters?


70-72: Check error handling for PrepareForValidation with filters.

The test case for PrepareForValidation includes filters. Ensure that the error handling for PrepareForValidation with filters is tested.

Would you like to add test cases to cover PrepareForValidation scenarios with different filters?


82-84: Ensure comprehensive filter scenarios with PrepareForValidation.

The test case for PrepareForValidation includes a filter. Ensure that other common filters (e.g., lowercase, uppercase) are also tested with PrepareForValidation.

Would you like to add more test cases to cover additional filters with PrepareForValidation?


99-101: Ensure comprehensive filter scenarios with multiple options.

The test case for multiple options includes filters. Ensure that other common filters (e.g., lowercase, uppercase) are also tested with multiple options.

Would you like to add more test cases to cover additional filters with multiple options?


126-126: Ensure comprehensive filter scenarios for struct data with PrepareForValidation.

The test case for struct data with PrepareForValidation includes a filter. Ensure that other common filters (e.g., lowercase, uppercase) are also tested for struct data with PrepareForValidation.

Would you like to add more test cases to cover additional filters for struct data with PrepareForValidation?


143-143: Ensure comprehensive filter scenarios for struct data with multiple options.

The test case for struct data with multiple options includes filters. Ensure that other common filters (e.g., lowercase, uppercase) are also tested for struct data with multiple options.

Would you like to add more test cases to cover additional filters for struct data with multiple options?


169-172: Ensure consistent validation of expected data.

The test cases validate expected data after applying filters. Ensure that all filters are correctly applied and validated.

Would you like to add assertions to validate the application of all filters?

@devhaozi devhaozi closed this Jul 10, 2024
@devhaozi devhaozi reopened this Jul 10, 2024
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 29c2b31 and 77b6e6f.

Files ignored due to path filters (1)
  • mocks/http/ContextRequest.go is excluded by !mocks/**
Files selected for processing (1)
  • contracts/http/request.go (2 hunks)
Additional comments not posted (2)
contracts/http/request.go (2)

84-84: LGTM! The addition of the filters parameter is consistent with the PR objective.

The Validate method now includes a filters parameter, which aligns with the goal of enhancing data validation processes.


95-96: LGTM! The reordering of the Filters method is minor and logical.

The Filters method is now placed between Rules and Messages, which aligns with organizing the methods in a logical sequence.

coderabbitai[bot]
coderabbitai bot previously approved these changes Jul 10, 2024
contracts/http/request.go Outdated Show resolved Hide resolved
contracts/validation/validation.go Outdated Show resolved Hide resolved
validation/validation.go Show resolved Hide resolved
@devhaozi devhaozi closed this Jul 11, 2024
@devhaozi devhaozi reopened this Jul 11, 2024
Copy link
Contributor

@hwbrzzl hwbrzzl left a comment

Choose a reason for hiding this comment

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

Great 👍

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 77b6e6f and 5ec4b1e.

Files selected for processing (6)
  • contracts/http/request.go (1 hunks)
  • validation/errors_test.go (9 hunks)
  • validation/validation.go (4 hunks)
  • validation/validation_test.go (6 hunks)
  • validation/validator.go (1 hunks)
  • validation/validator_test.go (13 hunks)
Files skipped from review as they are similar to previous changes (5)
  • contracts/http/request.go
  • validation/errors_test.go
  • validation/validation.go
  • validation/validator.go
  • validation/validator_test.go
Additional comments not posted (16)
validation/validation_test.go (16)

31-36: Validate the inclusion of filters in the test case.

The TestMake test case has been updated to include filters. Ensure that the filters are correctly applied and tested.


41-46: Validate the inclusion of filters in the test case.

The TestMake test case has been updated to include filters. Ensure that the filters are correctly applied and tested.


52-57: Validate the inclusion of filters in the test case.

The TestMake test case has been updated to include filters. Ensure that the filters are correctly applied and tested.


63-65: Validate the inclusion of filters in the test case.

The TestMake test case has been updated to include filters. Ensure that the filters are correctly applied and tested.


76-79: Validate the inclusion of filters in the test case.

The TestMake test case has been updated to include filters. Ensure that the filters are correctly applied and tested.


88-91: Validate the inclusion of filters in the test case.

The TestMake test case has been updated to include filters. Ensure that the filters are correctly applied and tested.


105-108: Validate the inclusion of filters in the test case.

The TestMake test case has been updated to include filters. Ensure that the filters are correctly applied and tested.


133-136: Validate the inclusion of filters in the test case.

The TestMake test case has been updated to include filters. Ensure that the filters are correctly applied and tested.


150-153: Validate the inclusion of filters in the test case.

The TestMake test case has been updated to include filters. Ensure that the filters are correctly applied and tested.


Line range hint 378-384:
Validate the correctness of the TestAddFilter test case.

The TestAddFilter test case has been added to test the addition of filters. Ensure the test case correctly verifies the filter functionality.


Line range hint 386-396:
Validate the correctness of the TestAddFilters test case.

The TestAddFilters test case has been added to test the addition of multiple filters. Ensure the test case correctly verifies the filter functionality.


Line range hint 398-432:
Validate the correctness of the TestFilters test case.

The TestFilters test case has been added to test the application of filters. Ensure the test case correctly verifies the filter functionality.


Line range hint 579-624:
Validate the correctness of the TestCustomRule test case.

The TestCustomRule test case has been added to test the addition of custom rules. Ensure the test case correctly verifies the custom rule functionality.


Line range hint 626-646:
Validate the correctness of the Uppercase struct.

The Uppercase struct has been added to define a custom validation rule. Ensure the struct correctly implements the custom rule functionality.


Line range hint 648-668:
Validate the correctness of the Lowercase struct.

The Lowercase struct has been added to define a custom validation rule. Ensure the struct correctly implements the custom rule functionality.


Line range hint 670-690:
Validate the correctness of the Duplicate struct.

The Duplicate struct has been added to define a custom validation rule. Ensure the struct correctly implements the custom rule functionality.

@devhaozi devhaozi enabled auto-merge (squash) July 11, 2024 08:15
@devhaozi devhaozi merged commit 917ad76 into master Jul 11, 2024
10 checks passed
@devhaozi devhaozi deleted the haozi/filter branch July 11, 2024 09:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

✨ [Feature] Support add custom filter for validation
2 participants