zod-openapi-share
    Preparing search index...

    Class ZodOpenAPISchema<M>

    The ZodOpenAPISchema class is a utility for creating OpenAPI schema definitions

    Yuki Osada r.rstudio.c@gmail.com

    Type Parameters

    • M extends ResponsesConfig

      The user-defined status codes type (M extends ResponsesConfig).

    Implements

    Index

    Constructors

    Methods

    Constructors

    • The constructor to initialize the ZodOpenAPISchema class with a ResponsesConfig type object.

      Type Parameters

      • M extends Partial<Record<StatusCode, ResponseConfig | ReferenceObject>>

      Parameters

      • responses: M

        The user-defined status codes type object (M extends ResponsesConfig).

      Returns ZodOpenAPISchema<M>

      import { z } from '@hono/zod-openapi';
      import { ZodOpenAPISchema } from 'zod-openapi-share';

      const ContentlyStatusCodeArray = [
      100, 102, 103, 200, 201, 202, 203, 206, 207, 208, 226, 300, 301, 302, 303, 305, 306, 307, 308, 400, 401, 402, 403,
      404, 405, 406, 407, 408, 409, 410, 411, 412, 413, 414, 415, 416, 417, 418, 421, 422, 423, 424, 425, 426, 428, 429,
      431, 451, 500, 501, 502, 503, 504, 505, 506, 507, 508, 510, 511, -1,
      ] as const;

      export const errorResponseSchema = z.object({
      status: z.union(ContentlyStatusCodeArray.map((code) => z.literal(code))).meta({
      example: 400,
      description: 'HTTP Status Code',
      }),
      message: z.string().min(1).meta({
      example: 'Bad Request',
      description: 'Error Message',
      }),
      });

      const route = new ZodOpenAPISchema({
      400: {
      description: 'Bad Request',
      content: { 'application/json': { schema: errorResponseSchema } },
      },
      500: {
      description: 'Internal Server Error',
      content: { 'application/json': { schema: errorResponseSchema } },
      },
      } as const);

    Methods

    • statusCodes argument omitted

      Type Parameters

      • R extends RouteConfig

        The route config type (R extends RouteConfig). This type is provided by @hono/zod-openapi.

      Parameters

      • route: R

        Route config (R extends RouteConfig). This type is provided by @hono/zod-openapi.

      Returns R

      • The route config type (R extends RouteConfig) with the specified status codes added to the responses. This type is provided by @hono/zod-openapi.
      import { z } from '@hono/zod-openapi';
      import { ZodOpenAPISchema } from 'zod-openapi-share';

      const ContentlyStatusCodeArray = [
      100, 102, 103, 200, 201, 202, 203, 206, 207, 208, 226, 300, 301, 302, 303, 305, 306, 307, 308, 400, 401, 402, 403,
      404, 405, 406, 407, 408, 409, 410, 411, 412, 413, 414, 415, 416, 417, 418, 421, 422, 423, 424, 425, 426, 428, 429,
      431, 451, 500, 501, 502, 503, 504, 505, 506, 507, 508, 510, 511, -1,
      ] as const;

      const errorResponseSchema = z.object({
      status: z.union(ContentlyStatusCodeArray.map((code) => z.literal(code))).meta({
      example: 400,
      description: 'HTTP Status Code',
      }),
      message: z.string().min(1).meta({
      example: 'Bad Request',
      description: 'Error Message',
      }),
      });


      const route = new ZodOpenAPISchema({
      400: {
      description: 'Bad Request',
      content: { 'application/json': { schema: errorResponseSchema } },
      },
      500: {
      description: 'Internal Server Error',
      content: { 'application/json': { schema: errorResponseSchema } },
      },
      } as const);

      const responseBodySchema = z.object({
      result: z.string().meta({
      example: 'Hello World!',
      description: 'Sample Endpoint Response',
      }),
      });

      const rootRoute = route.createSchema(
      {
      path: '/',
      method: 'get',
      description: 'Sample Endpoint',
      responses: {
      200: {
      description: 'OK',
      content: {
      'application/json': {
      schema: responseBodySchema,
      },
      },
      },
      },
      }
      // You can omit the `statusCodes` argument here
      );
    • statusCodes argument included

      Type Parameters

      • R extends RouteConfig

        The route config type (R extends RouteConfig). This type is provided by @hono/zod-openapi.

      • T extends readonly UserDefinedStatusCode<M>[]

        The readonly array of unique user-defined status codes type (M extends ResponsesConfig).

      Parameters

      • route: R

        Route config (R extends RouteConfig). This type is provided by @hono/zod-openapi.

      • statusCodes: NeverWrapper<UserDefinedStatusCode<M>, T>

        Optional array of unique status codes (only user-defined ones) to be added to the route.

      Returns R

      • The route config type (R extends RouteConfig) with the specified status codes added to the responses. This type is provided by @hono/zod-openapi.
      import { z } from '@hono/zod-openapi';
      import { ZodOpenAPISchema } from 'zod-openapi-share';

      const ContentlyStatusCodeArray = [
      100, 102, 103, 200, 201, 202, 203, 206, 207, 208, 226, 300, 301, 302, 303, 305, 306, 307, 308, 400, 401, 402, 403,
      404, 405, 406, 407, 408, 409, 410, 411, 412, 413, 414, 415, 416, 417, 418, 421, 422, 423, 424, 425, 426, 428, 429,
      431, 451, 500, 501, 502, 503, 504, 505, 506, 507, 508, 510, 511, -1,
      ] as const;

      const errorResponseSchema = z.object({
      status: z.union(ContentlyStatusCodeArray.map((code) => z.literal(code))).meta({
      example: 400,
      description: 'HTTP Status Code',
      }),
      message: z.string().min(1).meta({
      example: 'Bad Request',
      description: 'Error Message',
      }),
      });


      const route = new ZodOpenAPISchema({
      400: {
      description: 'Bad Request',
      content: { 'application/json': { schema: errorResponseSchema } },
      },
      500: {
      description: 'Internal Server Error',
      content: { 'application/json': { schema: errorResponseSchema } },
      },
      } as const);

      const responseBodySchema = z.object({
      result: z.string().meta({
      example: 'Hello World!',
      description: 'Sample Endpoint Response',
      }),
      });

      const rootRoute = route.createSchema(
      {
      path: '/',
      method: 'get',
      description: 'Sample Endpoint',
      responses: {
      200: {
      description: 'OK',
      content: {
      'application/json': {
      schema: responseBodySchema,
      },
      },
      },
      },
      },
      [400, 500] // This argument is the `statusCodes`
      );