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

Improve ZodErrorFilter to only return first union error #1256

Merged
merged 1 commit into from
Mar 6, 2024

Conversation

iamacook
Copy link
Member

@iamacook iamacook commented Mar 6, 2024

Summary

This extends the ZodErrorFilter to reduce the number of issues returned in the error payload when a invalid_union error is thrown by zod.

Instead of the error having multiple elements, e.g.:

  statusCode: 422,
  code: 'invalid_union',
  unionErrors: [
    { issues: [Array], name: 'ZodError' },
    { issues: [Array], name: 'ZodError' }
  ],
  path: [],
  message: 'Invalid input'
}

it returns the first issue of the unionErrors:

{
  statusCode: 422,
  code: 'invalid_type',
  expected: 'string',
  received: 'boolean',
  path: ['value'],
  message: 'Expected string, received boolean',
}

Motivation

We only return the first issue of a "standard" ZodError, removing any identifier that it is a ZodError, to reduce the error payload. This mirrors that with invalid_union errors.

Changes

  • Create ZodErrorFiler['mapZodErrorResponse'] to extract issues according to type
  • Add test coverage

@iamacook iamacook self-assigned this Mar 6, 2024
@iamacook iamacook marked this pull request as ready for review March 6, 2024 10:34
@iamacook iamacook requested a review from a team as a code owner March 6, 2024 10:34
@coveralls
Copy link

Pull Request Test Coverage Report for Build 8170650706

Details

  • 3 of 3 (100.0%) changed or added relevant lines in 1 file are covered.
  • 3 unchanged lines in 2 files lost coverage.
  • Overall coverage decreased (-0.08%) to 93.117%

Files with Coverage Reduction New Missed Lines %
src/routes/transactions/mappers/common/transaction-info.mapper.ts 1 78.17%
src/routes/transactions/entities/tests/human-description.builder.ts 2 80.0%
Totals Coverage Status
Change from base Build 8169973314: -0.08%
Covered Lines: 6406
Relevant Lines: 6635

💛 - Coveralls

@hectorgomezv hectorgomezv added the in review Someone is reviewing this Pull Request label Mar 6, 2024
@hectorgomezv hectorgomezv removed the in review Someone is reviewing this Pull Request label Mar 6, 2024
@iamacook iamacook merged commit a3680e6 into main Mar 6, 2024
16 checks passed
@iamacook iamacook deleted the zod-union-errors branch March 6, 2024 15:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants