Conversation
1be1762 to
a9903d1
Compare
Switching to zod core's `toDotPath()` in `getMessageFromError()`. I'm not using `z.prettifyError()` because its prettiness to me is questionable due to: - icons/symbols - new lines - offsets In my opinion such formatting should be delegated to UI. For that I'm planning #2663
| .object({ | ||
| status: z.literal("error"), | ||
| error: z.object({ message: z.string() }), | ||
| tree: errorTreeSchema.optional(), |
There was a problem hiding this comment.
perhaps I should rather make a dedicated formResultHandler for that purpose
|
Important Review skippedDraft detected. Please check the settings in the CodeRabbit UI or the You can disable this status message by setting the WalkthroughA recursive error tree type and its schema were introduced and integrated into error response types and OpenAPI schemas for several API endpoints. The result handler now includes a structured Changes
Sequence Diagram(s)sequenceDiagram
participant Client
participant API
participant Validator
participant ResultHandler
Client->>API: Send request with input
API->>Validator: Validate input
Validator-->>API: Return validation error (if any)
API->>ResultHandler: Handle error
alt InputValidationError
ResultHandler->>Validator: z.treeifyError(error.cause)
ResultHandler-->>Client: Respond with error object (includes tree)
else Other error
ResultHandler-->>Client: Respond with error object (tree undefined)
end
Poem
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
SupportNeed help? Create a ticket on our support page for assistance with any issues or questions. 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 using PR comments)
Other keywords and placeholders
CodeRabbit Configuration File (
|
|
@coderabbitai review |
✅ Actions performedReview triggered.
|
There was a problem hiding this comment.
Actionable comments posted: 0
♻️ Duplicate comments (4)
example/example.documentation.yaml (4)
239-240: Document optionaltreein PATCH /v1/user/{id} 400 response
543-544: Document optionaltreein POST /v1/avatar/upload 400 response
612-613: Document optionaltreein POST /v1/avatar/raw 400 response
740-741: Document optionaltreein POST /v1/forms/feedback 400 response
🧹 Nitpick comments (2)
example/example.documentation.yaml (2)
74-75: Document optionaltreein GET /v1/user/retrieve 400 response
You’ve correctly added an optionaltreeproperty referencingSchema2. Since it’s not in therequiredarray, consider updating theexamplesblock to illustrate how thetreestructure appears in practice. This will help API consumers understand and parse the new field.
766-787: Enhance recursive error tree schema (Schema2)
The newSchema2models a recursive error tree nicely. A few optional refinements:
- Replace
id: errorTreeSchemawith$idto align with JSON Schema Draft 2020-12 in OpenAPI 3.1 (e.g.$id: "https://example.com/components/schemas/Schema2").- Add
descriptionfields for the schema and each property (errors,items,properties) to clarify their roles.- Review whether nested nodes need
additionalProperties: falseor more granular control.
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
⛔ Files ignored due to path filters (6)
example/__snapshots__/index.spec.ts.snapis excluded by!**/*.snapexpress-zod-api/tests/__snapshots__/documentation.spec.ts.snapis excluded by!**/*.snapexpress-zod-api/tests/__snapshots__/endpoint.spec.ts.snapis excluded by!**/*.snapexpress-zod-api/tests/__snapshots__/integration.spec.ts.snapis excluded by!**/*.snapexpress-zod-api/tests/__snapshots__/result-handler.spec.ts.snapis excluded by!**/*.snapexpress-zod-api/tests/__snapshots__/system.spec.ts.snapis excluded by!**/*.snap
📒 Files selected for processing (5)
example/example.client.ts(6 hunks)example/example.documentation.yaml(6 hunks)express-zod-api/src/result-handler.ts(3 hunks)express-zod-api/src/result-helpers.ts(1 hunks)express-zod-api/tests/endpoint.spec.ts(2 hunks)
🧰 Additional context used
🧬 Code Graph Analysis (1)
express-zod-api/src/result-handler.ts (3)
express-zod-api/src/result-helpers.ts (1)
errorTreeSchema(104-114)express-zod-api/src/errors.ts (1)
InputValidationError(69-75)express-zod-api/src/index.ts (1)
InputValidationError(26-26)
🔇 Additional comments (8)
express-zod-api/src/result-helpers.ts (1)
104-114: Excellent implementation of the recursive error tree schema!The use of getters for
itemsandpropertiesis a clever solution to avoid circular reference issues during schema definition. The structure perfectly matches Zod'streeifyErroroutput format, enabling proper typing for nested validation errors. The metadata ID ensures proper schema identification in OpenAPI generation.express-zod-api/tests/endpoint.spec.ts (2)
566-566: Appropriate test strategy update for enhanced error responses.Switching to snapshot testing makes sense here since the error responses now include the complex
treeproperty with nested structures. This approach maintains test coverage while being more resilient to the detailed structure changes in error responses.
632-632: Consistent test approach for error response validation.Good consistency in using snapshot testing for input validation error responses across different test scenarios.
express-zod-api/src/result-handler.ts (3)
10-10: Clean imports for error tree functionality.Properly importing the required
InputValidationErroranderrorTreeSchemafor the new feature implementation.Also applies to: 16-16
120-120: Well-designed schema extension.Adding the
treeproperty as optional maintains backward compatibility while enabling the new structured error functionality. The use oferrorTreeSchemaensures type consistency across the application.
136-139: Excellent conditional error tree implementation.The logic correctly:
- Only adds the
treeproperty forInputValidationErrorinstances- Uses
error.cause(which is aZodError) withz.treeifyError()- Maintains clean separation between different error types
- Provides structured validation error details when most beneficial
This implementation aligns perfectly with the PR objective of facilitating easier error display in UI forms.
example/example.client.ts (2)
6-10: Well-structured recursive error type definition.The
Type2type perfectly mirrors the server-sideerrorTreeSchemastructure:
errorsarray for validation messages- Optional
itemsfor array-level errors- Optional
propertiesfor object-level errors- Proper recursive self-references
This ensures type consistency between client and server for the error tree feature.
41-41: Consistent application of error tree types across endpoints.The optional
treeproperty has been appropriately added to all relevant negative response variants (HTTP 400 errors) that could contain input validation errors. This provides consistent error handling capabilities across different API endpoints while maintaining backward compatibility.Also applies to: 102-102, 239-239, 269-269, 330-330
58c0d05 to
5e97386
Compare
|
should not overload the default RH |
https://zod.dev/error-formatting#ztreeifyerror
This should make it easer to display errors in UI form by default.
Summary by CodeRabbit