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

Proposal(zod-openapi): reduce build/bundle size by ~60kb by only type importing zod #163

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/zod-openapi/src/lib/zod-extensions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ This code is heavily inspired by https://github.com/asteasolutions/zod-to-openap
import { extendApi } from './zod-openapi';
import {z} from "zod";
import { SchemaObject } from "openapi3-ts/oas31";
import {ZodTypeDef} from "zod/lib/types";
import type {ZodTypeDef} from "zod/lib/types";


declare module 'zod' {
Expand Down
60 changes: 27 additions & 33 deletions packages/zod-openapi/src/lib/zod-openapi.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { SchemaObject, SchemaObjectType } from 'openapi3-ts/oas31';
import merge from 'ts-deepmerge';
import { AnyZodObject, z, ZodTypeAny } from 'zod';
import type { AnyZodObject, z, ZodTypeAny } from 'zod';

type AnatineSchemaObject = SchemaObject & { hideDefinitions?: string[] };

Expand Down Expand Up @@ -62,16 +62,16 @@ function parseTransformation({
['integer', 'number'].includes(`${input.type}`)
? 0
: 'string' === input.type
? ''
: 'boolean' === input.type
? false
: 'object' === input.type
? {}
: 'null' === input.type
? null
: 'array' === input.type
? []
: undefined,
? ''
: 'boolean' === input.type
? false
: 'object' === input.type
? {}
: 'null' === input.type
? null
: 'array' === input.type
? []
: undefined,
{ addIssue: () => undefined, path: [] } // TODO: Discover if context is necessary here
);
} catch (e) {
Expand All @@ -85,8 +85,8 @@ function parseTransformation({
...input,
...(['number', 'string', 'boolean', 'null'].includes(output)
? {
type: output as 'number' | 'string' | 'boolean' | 'null',
}
type: output as 'number' | 'string' | 'boolean' | 'null',
}
: {}),
},
...schemas
Expand Down Expand Up @@ -173,19 +173,17 @@ function parseNumber({
);
}



function getExcludedDefinitionsFromSchema(schemas: AnatineSchemaObject[]): string[] {


function getExcludedDefinitionsFromSchema(
schemas: AnatineSchemaObject[]
): string[] {
const excludedDefinitions = [];
for (const schema of schemas) {
if (Array.isArray(schema.hideDefinitions)) {
excludedDefinitions.push(...schema.hideDefinitions)
excludedDefinitions.push(...schema.hideDefinitions);
}
}

return excludedDefinitions
return excludedDefinitions;
}

function parseObject({
Expand All @@ -199,12 +197,7 @@ function parseObject({
let additionalProperties: SchemaObject['additionalProperties'];

// `catchall` obviates `strict`, `strip`, and `passthrough`
if (
!(
zodRef._def.catchall instanceof z.ZodNever ||
zodRef._def.catchall?._def.typeName === 'ZodNever'
)
)
if (!(zodRef._def.catchall?._def.typeName === 'ZodNever'))
additionalProperties = generateSchema(zodRef._def.catchall, useOutput);
else if (zodRef._def.unknownKeys === 'passthrough')
additionalProperties = true;
Expand All @@ -219,11 +212,10 @@ function parseObject({
).filter((key) => {
const item = (zodRef as z.AnyZodObject).shape[key];
return (
!(item.isOptional() || item._def.typeName === 'ZodDefault') &&
!(
item.isOptional() ||
item instanceof z.ZodDefault ||
item._def.typeName === 'ZodDefault'
) && !(item instanceof z.ZodNever || item._def.typeName === 'ZodDefault')
item._def.typeName === 'ZodNever' || item._def.typeName === 'ZodDefault'
)
);
});

Expand All @@ -241,9 +233,11 @@ function parseObject({
}),
...required,
...additionalProperties,
...hideDefinitions
...hideDefinitions,
},
zodRef.description ? { description: zodRef.description, hideDefinitions } : {},
zodRef.description
? { description: zodRef.description, hideDefinitions }
: {},
...schemas
);
}
Expand All @@ -257,7 +251,7 @@ function parseRecord({
{
type: 'object' as SchemaObjectType,
additionalProperties:
zodRef._def.valueType instanceof z.ZodUnknown
zodRef._def.valueType._def.typeName === 'ZodUnknown'
? {}
: generateSchema(zodRef._def.valueType, useOutput),
},
Expand Down
Loading