WARNING: THIS SITE IS A MIRROR OF GITHUB.COM / IT CANNOT LOGIN OR REGISTER ACCOUNTS / THE CONTENTS ARE PROVIDED AS-IS / THIS SITE ASSUMES NO RESPONSIBILITY FOR ANY DISPLAYED CONTENT OR LINKS / IF YOU FOUND SOMETHING MAY NOT GOOD FOR EVERYONE, CONTACT ADMIN AT ilovescratch@foxmail.com
Skip to content

Conversation

@hyoban
Copy link

@hyoban hyoban commented Nov 27, 2025

The author should do the following, if applicable

  • Add tests
  • Run tests
  • bun run format:fix && bun run lint:fix to format the code
  • Add TSDoc/JSDoc to document the code

We may not have returned a response yet. In RSSHub, we have a wrapped handler, https://github.com/DIYgod/RSSHub/blob/680abcc00820876176d34cd47c3c13a37ad564dd/lib/registry.ts#L193-L210, and I tried to add a validator. DIYgod/RSSHub#20584 This will result in unexpected errors. Please tell me if I did anything wrong.

BTW, can we have the standard validator accept a nullable schema? In that case, validation can be skipped. I can pass an empty middleware.

And is it possible to restrict the schema type to the parameter defined in the path (Is there a type utils for this in Hono?) This is how I wrote it now.

type ParamObjectFromPath<P extends string> = P extends `${string}:${infer Param}/${infer Rest}`
    ? Param extends `${infer Name}?`
        ? { [K in Name]?: string } & ParamObjectFromPath<`/${Rest}`>
        : { [K in Param]: string } & ParamObjectFromPath<`/${Rest}`>
    : P extends `${string}:${infer Param}`
      ? Param extends `${infer Name}?`
          ? { [K in Name]?: string }
          : { [K in Param]: string }
      : Record<string, never>;

@codecov
Copy link

codecov bot commented Nov 27, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 91.49%. Comparing base (b06005a) to head (fc37c1e).

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #4553   +/-   ##
=======================================
  Coverage   91.49%   91.49%           
=======================================
  Files         172      172           
  Lines       11230    11230           
  Branches     3257     3256    -1     
=======================================
  Hits        10275    10275           
  Misses        954      954           
  Partials        1        1           

☔ View full report in Codecov by Sentry.
📢 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.

@yusukebe
Copy link
Member

Hey @hyoban

Can you provide a minimal code or project to explain your problem?

@hyoban
Copy link
Author

hyoban commented Nov 27, 2025

Can you provide a minimal code or project to explain your problem?

Like this, I want to handle the return only in the middleware.

import { Hono } from "hono";
import { validator } from "hono/validator";
import { serve } from '@hono/node-server'

const app = new Hono();

app.use(async (c, next) => {
  await next();
  return c.json({ message: c.get('message') });
});

app.get(
  "/",
  validator("query", () => {
    return 'whatever';
  }),
  (c) => {
    c.set('message', 'Hello, Hono!');
  }
);

serve(app)

@yusukebe
Copy link
Member

Hi @hyoban

Thank you for the response.

I understand your problem. Actually, I/we designed the handler after the validator returns a response. So, if you don't return anything, it doesn't behave correctly, and a TypeScript type mismatch occurs.

This means I've not imagined the use case using the no response handler after validation. But we may have to accept it. I'll work on it. Thanks!

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.

2 participants