Skip to content

feat(standard-validator): Add flattenErrors utility to @hono/standard-validator - #2050

Open
KTrain5169 wants to merge 12 commits into
honojs:mainfrom
KTrain5169:sortErrors-utility-sValidator
Open

feat(standard-validator): Add flattenErrors utility to @hono/standard-validator#2050
KTrain5169 wants to merge 12 commits into
honojs:mainfrom
KTrain5169:sortErrors-utility-sValidator

Conversation

@KTrain5169

@KTrain5169 KTrain5169 commented Jul 27, 2026

Copy link
Copy Markdown

The author should do the following, if applicable

  • Add tests
  • Run tests
  • pnpm changeset at the top of this repo and push the changeset
  • Follow the contribution guide

Description

See #1987

This PR adds a new flattenErrors utility that sorts errors by form and field errors.
For field errors, they are also keyed by error path.
Should be directly usable by passing in the result.data array from a use of the middleware.

import { sValidator, flattenErrors } from '@hono/standard-validator'

sValidator('json', schema, (result) => {
    if (!result.success) {
        const sortedErrors = sortErrors(result.error)
        // use results
    }
})

thanks to @stebeus for testing initial implementation and offering an improved version of the utility

@changeset-bot

changeset-bot Bot commented Jul 27, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 97911fa

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@hono/standard-validator Minor

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

@KTrain5169

Copy link
Copy Markdown
Author

actually on second thought would it make more sense to name the function flattenErrors instead?

@stebeus

stebeus commented Jul 28, 2026

Copy link
Copy Markdown

actually on second thought would it make more sense to name the function flattenErrors instead?

Yes, because it returns a flat object of an array of issues. But before doing that, the Valibot and Arktype tests need to be fixe, as these validators don't put object-level issues to formErrors. I'm finishing a fix for your fork right now.

✓ sorts Zod validation errors by path 1ms
× sorts Valibot validation errors by path 5ms
× sorts ArkType validation errors by path 2ms

FAIL src/index.test.ts > sortErrors > sorts Valibot validation errors by path
AssertionError: expected { formErrors: [], …(1) } to strictly equal { …(2) }

- Expected
+ Received

  {
    "fieldErrors": {
      "password": [
        "Password must be at least 4 characters long",
      ],
+     "role": [
+       "Invalid key: Expected never but received \"role\"",
+     ],
      "username": [
        "Username cannot be longer than 10 characters",
        "Username must contain only alphanumeric characters",
      ],
    },
-   "formErrors": [
-     "Unrecognized key: \"role\"",
-   ],
+   "formErrors": [],
  }

 ❯ src/index.test.ts:530:26
    528|
    529|     // Assert
    530|     expect(sortedErrors).toStrictEqual(testResult)
       |                          ^
    531|   })
    532|

⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯[1/2]⎯

FAIL src/index.test.ts > sortErrors > sorts ArkType validation errors by path
AssertionError: expected { formErrors: [], …(1) } to strictly equal { …(2) }

- Expected
+ Received

  {
    "fieldErrors": {
      "password": [
-       "Password must be at least 4 characters long",
+       "password must be at least length 4 (was 3)",
      ],
      "username": [
-       "Username cannot be longer than 10 characters",
-       "Username must contain only alphanumeric characters",
+       "username (\"Super John Doe\") must be...
+   ◦ matched by ^[\\p{L}\\p{N}_]+$
+   ◦ at most length 10",
      ],
    },
-   "formErrors": [
-     "Unrecognized key: \"role\"",
-   ],
+   "formErrors": [],
  }

stebeus and others added 5 commits July 28, 2026 10:56
It's verbose to do a one-to-one translation of a Zod/Valibot schema to
ArkType, on which hints that ArkType is used differently from other
validators.
Fix the Valibot and ArkType tests for `sortErrors`
@KTrain5169 KTrain5169 changed the title feat(standard-validator): Add sortErrors utility to @hono/standard-validator feat(standard-validator): Add flattenErrors utility to @hono/standard-validator Jul 29, 2026
@KTrain5169

Copy link
Copy Markdown
Author

just changed the function to flattenErrors, rationale is that later on we may want to add more sorting options that isn't just flattening the array like this.

I'll be sure to add this to the README later

@codecov

codecov Bot commented Jul 31, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 93.33333% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 92.15%. Comparing base (40f956e) to head (97911fa).
⚠️ Report is 9 commits behind head on main.

Files with missing lines Patch % Lines
packages/standard-validator/__schemas__/arktype.ts 50.00% 1 Missing ⚠️
Additional details and impacted files
@@           Coverage Diff           @@
##             main    #2050   +/-   ##
=======================================
  Coverage   92.14%   92.15%           
=======================================
  Files         115      115           
  Lines        4113     4128   +15     
  Branches     1072     1076    +4     
=======================================
+ Hits         3790     3804   +14     
- Misses        287      288    +1     
  Partials       36       36           
Flag Coverage Δ
oauth-providers 92.55% <ø> (ø)
standard-validator 95.65% <93.33%> (-0.46%) ⬇️
ua-blocker 90.00% <ø> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Comment thread packages/standard-validator/src/index.ts Outdated
Comment thread packages/standard-validator/src/index.ts Outdated
Comment thread .changeset/tricky-socks-rule.md Outdated

@yusukebe yusukebe left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add how to use this flattenErrors to the README?

@yusukebe

yusukebe commented Aug 4, 2026

Copy link
Copy Markdown
Member

Hey @muningis!

If you have time, can you review this?

@stebeus

stebeus commented Aug 4, 2026

Copy link
Copy Markdown

Can you add how to use this flattenErrors to the README?

Yes, we can. @KTrain5169 I'll document this for you.

@KTrain5169

Copy link
Copy Markdown
Author

Can you add how to use this flattenErrors to the README?

Yes, we can. @KTrain5169 I'll document this for you.

thank you, was going to do it earlier but got swamped with other things to do :/

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.

4 participants