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: [#452] slice in request cannot be bind #539

Merged
merged 3 commits into from
Jul 4, 2024
Merged

fix: [#452] slice in request cannot be bind #539

merged 3 commits into from
Jul 4, 2024

Conversation

hwbrzzl
Copy link
Contributor

@hwbrzzl hwbrzzl commented Jul 3, 2024

📑 Description

Closes goravel/goravel#452

Summary by CodeRabbit

  • New Features

    • Enhanced data validation by introducing support for additional data types in the form and JSON validation.
  • Bug Fixes

    • Improved data binding logic to handle multiple types of data sources accurately.
  • Miscellaneous

    • Updated version number from v1.14.2 to v1.14.3.

✅ Checks

  • Added test cases for my code

Copy link
Contributor

coderabbitai bot commented Jul 3, 2024

Walkthrough

This update enhances the validation logic by adding an additional dataFace argument to the Make function, introducing a new data binding process, and updating data structures and tests to manage request slices effectively. This modification addresses an issue where slices in a request could not be properly bound and validated.

Changes

Files Change Summary
support/constant.go Updated the Version constant from "v1.14.2" to "v1.14.3".
validation/validation.go, validator.go Added arguments and parameters to functions for improved data binding and validation logic. Updated the Bind method to handle multiple data sources.
validation/validator_test.go Modified struct fields and test cases to reflect new validation logic. Added Ages and Names fields to Data struct for comprehensive data validation.

Sequence Diagram(s)

sequenceDiagram
    participant Client
    participant Server
    participant Validator
    participant Binder

    Client->>Server: Send request with data
    Server->>Binder: Bind data through `Make`
    Binder->>Validator: Invoke `NewValidator` with data
    Validator-->>Binder: Return validated data
    Binder-->>Server: Return response
    Server-->>Client: Send validated response
Loading

Assessment against linked issues

Objective Addressed Explanation
🐛 [Bug] slice in request cannot be bind (#452)

Poem

In fields of code where data flows,
A rabbit toils, validation grows.
Slices now bind, so smooth, so free,
In v1.14.3, the fix we see.
Bugs hop away, the code’s delight,
In every test, it shines so bright. 🌟🐇


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.

@hwbrzzl hwbrzzl changed the base branch from master to v1.14.x July 3, 2024 14:40
if formData, ok := v.data.(*validate.FormData); ok {
values := make(map[string]any)
for key, value := range v.data.Src().(url.Values) {
values[key] = value[0]
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Only support one value in the form case, this is the default behavior of gookit/validate for now.

Comment on lines -91 to -126
{
name: "empty when data is struct and key is lowercase",
data: func() validate.DataFace {
data, err := validate.FromStruct(struct {
a string
}{
a: "aa",
})
assert.Nil(t, err)

return data
}(),
rules: map[string]string{"a": "required"},
assert: func(data Data) {
assert.Equal(t, "", data.A)
},
},
{
name: "empty when data is struct and key is struct",
data: func() validate.DataFace {
data, err := validate.FromStruct(struct {
D *Data
}{
D: &Data{
A: "aa",
},
})
assert.Nil(t, err)

return data
}(),
rules: map[string]string{"d.a": "required"},
assert: func(data Data) {
assert.Equal(t, "", data.A)
},
},
Copy link
Contributor Author

@hwbrzzl hwbrzzl Jul 3, 2024

Choose a reason for hiding this comment

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

Should test these cases in the Make test method, so remove them.

Comment on lines +143 to +144
assert.Equal(t, []int{1, 2}, data.Ages)
assert.Equal(t, []string{"a", "b"}, data.Names)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

The test cases for the issue.

Copy link

codecov bot commented Jul 4, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Please upload report for BASE (v1.14.x@9e18217). Learn more about missing BASE report.

Additional details and impacted files
@@            Coverage Diff             @@
##             v1.14.x     #539   +/-   ##
==========================================
  Coverage           ?   70.10%           
==========================================
  Files              ?      176           
  Lines              ?    10884           
  Branches           ?        0           
==========================================
  Hits               ?     7630           
  Misses             ?     2686           
  Partials           ?      568           

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

@hwbrzzl
Copy link
Contributor Author

hwbrzzl commented Jul 4, 2024

@coderabbitai review

Copy link
Contributor

coderabbitai bot commented Jul 4, 2024

Actions performed

Review triggered.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

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 9e18217 and 686a761.

Files selected for processing (4)
  • support/constant.go (1 hunks)
  • validation/validation.go (1 hunks)
  • validation/validator.go (3 hunks)
  • validation/validator_test.go (6 hunks)
Files skipped from review due to trivial changes (1)
  • support/constant.go
Additional comments not posted (7)
validation/validator.go (4)

4-4: Import update: Ensure consistency with data handling changes.

The import of "mime/multipart" was replaced with "net/url". Ensure that this aligns with the updated data handling logic in the Bind method.


24-24: Struct update: Verify correct usage of the data field.

The Validator struct now includes a data field of type validate.DataFace. Ensure that this field is utilized correctly in the methods of the Validator struct.


27-30: Method update: Verify correct initialization of data parameter.

The NewValidator function now includes an additional parameter, data validate.DataFace, to initialize the data field in the Validator struct. Ensure that this parameter is passed correctly and utilized in the Bind method.


34-53: Method update: Ensure correct handling of various data sources.

The Bind method has been modified to handle various data sources and bind data accordingly. Ensure that the method correctly handles different data sources and binds them to the provided pointer.

validation/validation.go (1)

63-63: Method update: Verify correct passing of dataFace parameter.

The Make function in the Validation struct now includes an additional argument, dataFace, which is passed to the NewValidator function. Ensure that this parameter is passed correctly and utilized in the NewValidator function.

validation/validator_test.go (2)

22-28: Struct update: Verify correct usage of new fields in test cases.

The Data struct now includes new fields Ages and Names. Ensure that these fields are utilized correctly in the test cases.


Line range hint 62-145:
Test case update: Ensure comprehensive coverage for binding slice data.

The TestBind method has been updated to include test cases for the new fields Ages and Names. Ensure that the test cases cover various scenarios for binding slice data.

@hwbrzzl hwbrzzl merged commit ac8d1e5 into v1.14.x Jul 4, 2024
10 checks passed
@hwbrzzl hwbrzzl deleted the bowen/#452 branch July 4, 2024 14:18
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.

🐛 [Bug] slice in request cannot be bind
1 participant