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

add path arg to @@validate and implement gencode #1641

Merged
merged 1 commit into from
Aug 12, 2024
Merged

add path arg to @@validate and implement gencode #1641

merged 1 commit into from
Aug 12, 2024

Conversation

j0rdanba1n
Copy link
Contributor

@j0rdanba1n j0rdanba1n commented Aug 8, 2024

Problem

Form-level validation (done with @@validate, which zenstack will convert to schema.refine() in zod) would not be assigned to any specific field, making fetching the error message tricky, hacky, and less type safe. I had to find these errors using formState.errors[""], since they had no key.

Solution

Zod already has a solution for this, allowing you to pass a path option into refine to set the key this error should use / field that this validation error should be associated with.

In this pull request, I make zenstack compatible with this.

  1. I add an optional _ path: String[]? parameter to @@validate attributes
  2. I use the values submitted here to generate code, such as path: ['startDate',] or path: ['user', 'name']
  3. I merge the message and path options and pass it into the refine call in the generated code

Testing

I tested this after running pnpm zenstack generate in my own project and it worked!
Also tested with pnpm jest zod.test.ts.

Copy link
Contributor

coderabbitai bot commented Aug 8, 2024

Walkthrough

Walkthrough

The changes enhance the validation framework by modifying the makeValidationRefinements function to incorporate a new options structure for validation messages, which now includes both message and path attributes. This improves the flexibility of validation messages. Additionally, the @@validate attribute has been updated to accept a field reference path, allowing for more contextual and relevant validations in complex scenarios.

Changes

Files Change Summary
.../utils/schema-gen.ts, .../stdlib.zmodel The makeValidationRefinements function was updated to include a path argument and a new options object for validation messages. The @@validate attribute now accepts an additional optional _path parameter, enhancing context-awareness in validation logic.
.../tests/plugins/zod.test.ts A new test case titled "refinement with path" was added, focusing on schema refinements with various validation conditions for an array field, ensuring correct error message generation.

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant ValidationSystem
    participant Schema

    User->>Schema: Trigger validation
    Schema->>ValidationSystem: Call makeValidationRefinements
    ValidationSystem->>ValidationSystem: Construct options with message and path
    ValidationSystem->>Schema: Return options for validation
    Schema->>User: Return validation result
Loading
sequenceDiagram
    participant User
    participant ValidationSystem
    participant Field

    User->>Field: Validate field
    Field->>ValidationSystem: Call @@validate(value, message, path)
    ValidationSystem->>Field: Process validation with context
    Field->>User: Return validation outcome
Loading

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:

‼️ IMPORTANT
Auto-reply has been disabled for this repository in the CodeRabbit settings. The CodeRabbit bot will not respond to your replies unless it is explicitly tagged.

  • 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 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.

@j0rdanba1n j0rdanba1n changed the title add path arg to @@validate and gencode add path arg to @@validate and implement gencode Aug 8, 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 1c52fcc and 3e95db4.

Files ignored due to path filters (2)
  • packages/plugins/trpc/tests/projects/t3-trpc-v10/package.json is excluded by !**/*.json
  • pnpm-lock.yaml is excluded by !**/pnpm-lock.yaml, !**/*.yaml
Files selected for processing (2)
  • packages/schema/src/plugins/zod/utils/schema-gen.ts (3 hunks)
  • packages/schema/src/res/stdlib.zmodel (1 hunks)
Additional comments not posted (4)
packages/schema/src/plugins/zod/utils/schema-gen.ts (3)

225-226: LGTM!

The construction of the message variable for the options object is clear and correctly formatted.


227-228: LGTM!

The introduction of the path variable for the options object is clear and correctly formatted.


Line range hint 230-244: LGTM!

The construction of the options variable and its integration into the refine call is well-implemented, enhancing validation flexibility.

packages/schema/src/res/stdlib.zmodel (1)

636-636: LGTM!

The addition of the _ path: FieldReference? parameter to the @@validate attribute enhances its functionality by allowing field-specific error associations.

Copy link
Member

@ymc9 ymc9 left a comment

Choose a reason for hiding this comment

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

Hey @j0rdanba1n , many thanks for making this PR! I agree it's a very useful one.

I've added some comments. Also, if you would like to, the tests related to @@validate reside in "tests/integration/tests/enhancements/with-policy/field-validation.test.ts". I think a simple case covering the new parameter should be sufficient.

Thanks!

packages/schema/src/res/stdlib.zmodel Outdated Show resolved Hide resolved
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 3e95db4 and 34500ee.

Files ignored due to path filters (1)
  • pnpm-lock.yaml is excluded by !**/pnpm-lock.yaml, !**/*.yaml
Files selected for processing (3)
  • packages/schema/src/plugins/zod/utils/schema-gen.ts (4 hunks)
  • packages/schema/src/res/stdlib.zmodel (1 hunks)
  • tests/integration/tests/plugins/zod.test.ts (1 hunks)
Files skipped from review as they are similar to previous changes (2)
  • packages/schema/src/plugins/zod/utils/schema-gen.ts
  • packages/schema/src/res/stdlib.zmodel
Additional comments not posted (1)
tests/integration/tests/plugins/zod.test.ts (1)

461-500: The test case for "refinement with path" is well-structured.

The test case effectively verifies the new functionality of associating validation errors with specific paths. It ensures that the paths in the error messages align with the expected validation logic.

@j0rdanba1n
Copy link
Contributor Author

@ymc9 addressed your feedback - please take another look and thank you :)

@j0rdanba1n
Copy link
Contributor Author

hi @ymc9 - following up :)

@ymc9
Copy link
Member

ymc9 commented Aug 12, 2024

hi @ymc9 - following up :)

Sorry for the late response @j0rdanba1n
The change looks great to me. I'm merging it. Is it blocking you now? If so, we can make a new patch release.

@ymc9 ymc9 merged commit 9017a13 into zenstackhq:dev Aug 12, 2024
13 checks passed
@j0rdanba1n
Copy link
Contributor Author

j0rdanba1n commented Aug 13, 2024

@ymc9 we're not immediately blocked, so no rush - when do you think the next patch release will be?

@j0rdanba1n j0rdanba1n deleted the validation-path branch August 13, 2024 06:57
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.

2 participants