Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove instanceOf checks #90

Merged
merged 1 commit into from
Feb 20, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 21 additions & 7 deletions src/transformer.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { FastifyDynamicSwaggerOptions } from '@fastify/swagger';
import type { FastifySchema } from 'fastify';
import type { OpenAPIV3 } from 'openapi-types';
import { type AnyZodObject, ZodObject, type ZodRawShape, ZodType } from 'zod';
import type { AnyZodObject, ZodObject, ZodRawShape, ZodType } from 'zod';
import { api } from 'zod-openapi';
import type {
ZodOpenApiComponentsObject,
Expand Down Expand Up @@ -36,6 +36,20 @@ export type FastifyZodOpenApiSchema = Omit<
params?: AnyZodObject;
};

export const isZodType = (object: unknown): object is ZodType =>
Boolean(
object &&
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
Object.getPrototypeOf((object as ZodType)?.constructor)?.name ===
'ZodType',
);

export const isZodObject = (
object: unknown,
// eslint-disable-next-line @typescript-eslint/no-explicit-any
): object is ZodObject<any, any, any, any, any> =>
Boolean(object && (object as ZodType)?.constructor?.name === 'ZodObject');

export const createParams = (
// eslint-disable-next-line @typescript-eslint/no-explicit-any
querystring: ZodObject<any, any, any, any, any>,
Expand Down Expand Up @@ -72,7 +86,7 @@ export const createResponseSchema = (
components: api.ComponentsObject,
path: string[],
): unknown => {
if (schema.properties instanceof ZodType) {
if (isZodType(schema.properties)) {
return api.createMediaTypeSchema(schema.properties, components, 'output', [
...path,
'schema',
Expand Down Expand Up @@ -122,7 +136,7 @@ export const createResponse = (

return Object.entries(response).reduce(
(acc, [key, value]: [string, unknown]) => {
if (value instanceof ZodType) {
if (isZodType(value)) {
acc[key] = api.createMediaTypeSchema(value, components, 'output', [
...path,
key,
Expand Down Expand Up @@ -177,7 +191,7 @@ export const fastifyZodOpenApiTransform: Transform = ({
...schema,
};

if (body instanceof ZodType) {
if (isZodType(body)) {
transformedSchema.body = api.createMediaTypeSchema(
body,
components,
Expand All @@ -190,7 +204,7 @@ export const fastifyZodOpenApiTransform: Transform = ({
transformedSchema.response = maybeResponse;
}

if (querystring instanceof ZodObject) {
if (isZodObject(querystring)) {
transformedSchema.querystring = createParams(
querystring,
'query',
Expand All @@ -199,14 +213,14 @@ export const fastifyZodOpenApiTransform: Transform = ({
);
}

if (params instanceof ZodObject) {
if (isZodObject(params)) {
transformedSchema.params = createParams(params, 'path', components, [
url,
'params',
]);
}

if (headers instanceof ZodObject) {
if (isZodObject(headers)) {
transformedSchema.headers = createParams(headers, 'header', components, [
url,
'headers',
Expand Down
Loading