-
-
Notifications
You must be signed in to change notification settings - Fork 90
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
Conversation
WalkthroughWalkthroughThe changes enhance the validation framework by modifying the Changes
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? TipsChatThere are 3 ways to chat with CodeRabbit:
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)
Additionally, you can add CodeRabbit Configuration File (
|
There was a problem hiding this 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
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 therefine
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.
There was a problem hiding this 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!
There was a problem hiding this 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
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.
@ymc9 addressed your feedback - please take another look and thank you :) |
hi @ymc9 - following up :) |
Sorry for the late response @j0rdanba1n |
@ymc9 we're not immediately blocked, so no rush - when do you think the next patch release will be? |
Problem
Form-level validation (done with
@@validate
, which zenstack will convert toschema.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 usingformState.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.
_ path: String[]?
parameter to@@validate
attributespath: ['startDate',]
orpath: ['user', 'name']
Testing
I tested this after running
pnpm zenstack generate
in my own project and it worked!Also tested with
pnpm jest zod.test.ts
.