add path arg to @@validate and implement gencode
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.
- I add an optional
_ path: String[]?parameter to@@validateattributes - I use the values submitted here to generate code, such as
path: ['startDate',]orpath: ['user', 'name'] - 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.
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?
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
@coderabbitaiin 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
@coderabbitaiin 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 pauseto pause the reviews on a PR.@coderabbitai resumeto resume the paused reviews.@coderabbitai reviewto trigger an incremental review. This is useful when automatic reviews are disabled for the repository.@coderabbitai full reviewto do a full review from scratch and review all the files again.@coderabbitai summaryto regenerate the summary of the PR.@coderabbitai resolveresolve all the CodeRabbit review comments.@coderabbitai configurationto show the current CodeRabbit configuration for the repository.@coderabbitai helpto 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.yamlfile 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.
@ymc9 addressed your feedback - please take another look and thank you :)
hi @ymc9 - following up :)
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 we're not immediately blocked, so no rush - when do you think the next patch release will be?