zenstack icon indicating copy to clipboard operation
zenstack copied to clipboard

add path arg to @@validate and implement gencode

Open j0rdanba1n opened this issue 1 year ago • 2 comments

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.

j0rdanba1n avatar Aug 08 '24 12:08 j0rdanba1n

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

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:

:bangbang: 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.

coderabbitai[bot] avatar Aug 08 '24 12:08 coderabbitai[bot]

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

j0rdanba1n avatar Aug 08 '24 20:08 j0rdanba1n

hi @ymc9 - following up :)

j0rdanba1n avatar Aug 12 '24 19:08 j0rdanba1n

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 avatar Aug 12 '24 20:08 ymc9

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

j0rdanba1n avatar Aug 13 '24 06:08 j0rdanba1n