w3cj / hono-open-api-starter

A starter template for building fully documented type-safe JSON APIs with Hono and Open API
https://www.youtube.com/watch?v=sNh9PoM9sUE
MIT License
308 stars 33 forks source link

Only partial typesafety enforced #17

Open bennettcolecohen opened 5 days ago

bennettcolecohen commented 5 days ago

Hey CJ - I'm trying to better understand how to use Zod OpenApi Hono. The example below shows my route.ts schema and handler.ts handler.

If the response schema in handler.ts is missing a required parameter from the schema, a type error correctly shows. Ex: missing validField1. However, a type error is not shown for additional fields. Below, invalidField is allowed to pass and type safety isn't enforced. Why is this? I've also shown my types below.

Are there any glaring issues that I should implement to get this working properly?? Thanks in advance! If this is a bug, is there a workaround? Right now, a hack is to just first create the output object with the proper type and then just return it, but there's nothing beyond my willpower preventing me from messing it up. Thanks!

Code below

// types.ts
export interface AppBindings {
  Variables: {
    logger: PinoLogger;
  };
};

export type AppOpenAPI = OpenAPIHono<AppBindings>;

export type AppRouteHandler<R extends RouteConfig> = RouteHandler<R, AppBindings>;
// routes.ts
export const list = createRoute({
  path: "/items",
  method: "get",
  responses: {
    [HttpStatusCodes.OK]: {
      content: {
        "application/json": {
          schema: z.array(z.object({
            validField1: z.string(),
            validField2: z.string(),
          })),
        },
      },
      description: "The requested items",
    },
  },
});
export type ListRoute = typeof get;
// handler.ts
import type { ListRoute } from "./items.routes";

export const get: AppRouteHandler<ListRoute> = async (c) => {
  return c.json([{
    validField1: "val1",
    validField2: "val2",
    invalidField: "invalid", // THIS SHOULD THROW A TYPE ERROR
  }]);
};
w3cj commented 4 days ago

Hey! I looked into this, and it is a known issue:

https://github.com/honojs/middleware/issues/587

https://github.com/honojs/middleware/issues/181

A manual work around would be to add a satisfies constraint... this is not ideal though as it kind of defeats the purpose of types flowing through

Screenshot 2024-11-22 at 4 33 07 PM

bennettcolecohen commented 4 days ago

Ahh yeah that’s better than my workaround anyway though. Regardless, appreciate this template and tutorial! My team is going to be using it in prod for a new API so should be fun

On Fri, Nov 22, 2024 at 3:33 PM CJ @.***> wrote:

Hey! I looked into this, and it is a known issue:

honojs/middleware#587 https://github.com/honojs/middleware/issues/587

honojs/middleware#181 https://github.com/honojs/middleware/issues/181

A manual work around would be to add a satisfies constraint... this is not ideal though as it kind of defeats the purpose of types flowing through

Screenshot.2024-11-22.at.4.33.07.PM.png (view on web) https://github.com/user-attachments/assets/29828b6d-93e6-4cf4-8c11-886808aa0a59

— Reply to this email directly, view it on GitHub https://github.com/w3cj/hono-open-api-starter/issues/17#issuecomment-2495097553, or unsubscribe https://github.com/notifications/unsubscribe-auth/AVDKAZPHFF2OQHLCA3QIT2T2B65N3AVCNFSM6AAAAABSKBDEY2VHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDIOJVGA4TONJVGM . You are receiving this because you authored the thread.Message ID: @.***>