-
Notifications
You must be signed in to change notification settings - Fork 187
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
feat: Conform Validator Middleware #666
Conversation
🦋 Changeset detectedLatest commit: 8775f57 The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
Before I write my thoughts, let me first say thank you. If I were to create this middleware, I would create it with the same interface. I'm glad we're on the same page. import { conformValidator } from '@hono/conform-validator'
conformValidator((formData) => parseWithZod(formData, { schema })),
// parseWithZod or parseWithYup or parseWithValibot This allows middleware users to choose which Validator to use to validate form data, and makes it possible to validate form data with minimal code. The process of returning errors is defined by the user each time, but how about a method to customize it by allowing an optional hook function to be passed as the second argument of the conformValidator function, just like other validators? If there is no hook function, a process to return the default error content defined by the middleware is necessary. middleware/packages/valibot-validator/src/index.ts Lines 48 to 60 in e488b9f
Also, although it may be limited to React, I think it would be a good idea to return the Conform const submission = c.req.valid('form')
if (submission.status !== 'success') {
const res = c.json({ success: false, result: submission.reply() }, 400)
throw HTTPException(400, { res })
} |
Thank you for reviews !
The optional Hook function is a great idea !
As for the behavior when the hook function is omitted, I think it would be best to perform only validation, as with the current process, to prevent unintentional transmission of confidential information to the outside. Also, I think error handling should be done carefully and should be implemented by the implementer himself, rather than leaving it to another library. |
Thank you for the great implementation. I think that when there is a validation error in https://conform.guide/#the-gist
I was thinking about what kind of assumptions you made in your opinion. I thought that this might be the case depending on the validation schema implementation of middleware users, but if that's the case, I think the same thing could happen with existing validation middleware. I would like to respect your opinion, so I would like you to tell me in what kind of situations this could occur. I think it will also be useful for yusukebe's decision-making. |
That's true. On second thought, I realized that since the other validator middleware already returns error responses by default, it would be more natural for this middleware to match the behavior. I would like to modify the default behavior so that if a validation error occurs, the default behavior is to return an error response. Thanks. |
…alidation error occurs
Fixed it. |
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.
Thank you for thinking about the API design together with me.
I think there's no problem with this implementation of the API for this middleware. Since it handles FormData
, I wonder if we can make good use of hono/validator
's FormData
processing.
@yusukebe
I think there's no problem with this as a middleware design, so would you mind reviewing it in detail?
@uttk Great! I'll do it. |
Thank you for reviews! |
Hi @uttk Thank you for some fixes. I've left two comments. There will be last. Check them! |
Thank you for your kind review. |
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.
LGTM!
Added a validator that allows validation using conform.
Although the processing content is highly abstract and does not do much, it can increase the affinity with Hono RPC.