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

feat(zod-validator): add zErrValidator with default error throwing hook #897

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

ktKongTong
Copy link

close: #890

Copy link

changeset-bot bot commented Dec 20, 2024

⚠️ No Changeset found

Latest commit: 3091c60

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@yusukebe
Copy link
Member

Hi @ktKongTong

zErrValidator is interesting, but we should not add it without thinking about it carefully. This is just a wrapper, so I think just adding the description to the README will be okay.

@ktKongTong
Copy link
Author

ktKongTong commented Dec 21, 2024

Hi @yusukebe

we should not add it without thinking about it carefully.

Totally agree.

I believe this is a common use case. More specifically, it addresses the default behavior when a validation error occurs.

The current default behavior is excellent and is definitely the best choice for most lightweight use cases.

However, as the API grows in size (e.g., with 50 or more endpoints), the number of biz-errors also increases significantly. In such scenarios, throwing a validation error and handling it in a centralized location, rather than returning a default response, helps maintain a consistent error response format. This allows the client to parse errors in a uniform manner.

Therefore, I created this pull request.

Alternatively, we also can change the third parameters of zValidator , add a string: throw-err
to control the default behavior in a more convenience way.

export const zValidator = <
// ...
>(
  target: Target,
  schema: T,
  resultHandleStrategy?: Hook<z.infer<T>, E, P, Target> | 'throw-err'
): MiddlewareHandler<E, P, V> =>
  // @ts-expect-error not typed well
  validator(target, async (value, c) => {
    // ...
    const result = await schema.safeParseAsync(validatorValue)
    if(typeof resultHandleStrategy === 'string' && resultHandleStrategy === 'throw-err') {
      if(!result.success) {
        throw result.error
      }
    } else if (typeof resultHandleStrategy === 'function' && resultHandleStrategy) {
      const hook = resultHandleStrategy
      const hookResult = await hook({ data: validatorValue, ...result, target }, c)
      // hook result handle ...
    }
      // ...
  })

// usage

import { zValidator } from './validator-wrapper'
app.post(
  '/post',
  zValidator('json', schema, 'throw-err') // An error will be thrown if validation fails.
  //...
)

This is just a wrapper, so I think just adding the description to the README will be okay.

As your suggestion, another pr(#900) was created.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants