Skip to content
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

Validator should throw error rather than return c.json({ success, message }) #1441

Closed
kevbook opened this issue Sep 11, 2023 · 5 comments
Closed
Labels
enhancement New feature or request.

Comments

@kevbook
Copy link

kevbook commented Sep 11, 2023

What is the feature you are proposing?

Validator should throw error rather than return c.json({ success, message })
So we can use onError handler to format and send the right response to the client.

@kevbook kevbook added the enhancement New feature or request. label Sep 11, 2023
@yusukebe
Copy link
Member

@kevbook

The onError() is not designed for the validation error. Write like the following:

app.post(
  '/',
  validator('json', (data, c) => {
    if (!data.title) {
      return c.json({
        success: false
      }, 400)
    }
    return {
      title: data.title,
    }
  }),
  (c) => {
    return c.json({
      success: true
    }, 201)
  }
)

@yusukebe
Copy link
Member

I see, thanks. Reopened the issue.

@yusukebe yusukebe reopened this Sep 12, 2023
@yusukebe yusukebe added the v4 label Sep 16, 2023
@ahmetbicer
Copy link

+1 for this. It would be nice to catch validator errors from onError handler.

@yusukebe
Copy link
Member

Okay. We have to consider making it. But, it may be a breaking change. So will be included in the next major version "v4". Thanks!

@jvhellemond
Copy link

jvhellemond commented Nov 15, 2023

+1
I like to keep all my error responses uniform, with (translated) messages form malformed JSON or FormData generated by onError.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request.
Projects
None yet
Development

No branches or pull requests

4 participants