The user-defined status codes type (M extends ResponsesConfig).
The constructor to initialize the ZodOpenAPISchema class with a ResponsesConfig type object.
The user-defined status codes type object (M extends ResponsesConfig).
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);
statusCodes
argument omitted
The route config type (R extends RouteConfig). This type is provided by @hono/zod-openapi.
Route config (R extends RouteConfig). 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
The route config type (R extends RouteConfig). This type is provided by @hono/zod-openapi.
The readonly array of unique user-defined status codes type (M extends ResponsesConfig).
Route config (R extends RouteConfig). This type is provided by @hono/zod-openapi.
Optional array of unique status codes (only user-defined ones) to be added to the route.
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`
);
The ZodOpenAPISchema class is a utility for creating OpenAPI schema definitions
Author
Yuki Osada r.rstudio.c@gmail.com