anatine / zod-plugins

Plugins and utilities for Zod
640 stars 89 forks source link

schemaObject.required?.includes is not a function #223

Open nicofishman opened 1 week ago

nicofishman commented 1 week ago

Hi! πŸ‘‹

Firstly, thanks for your work on this project! πŸ™‚

Having this swagger.yaml and running npx ts-node -r tsconfig-paths/register src/main.swagger.ts && npx openapi-typescript ./swagger.yaml -o ./types/schema.ts threw TypeError: schemaObject.required?.includes is not a function. I use zod-nestjs to generate classes with zod. I tracked the problem down to the package and could solve it with a simple check

Here is the diff that solved my problem:

diff --git a/node_modules/@anatine/zod-nestjs/src/lib/create-zod-dto.js b/node_modules/@anatine/zod-nestjs/src/lib/create-zod-dto.js
index deb2e7d..d62395e 100644
--- a/node_modules/@anatine/zod-nestjs/src/lib/create-zod-dto.js
+++ b/node_modules/@anatine/zod-nestjs/src/lib/create-zod-dto.js
@@ -49,7 +49,7 @@ const createZodDto = (zodSchema) => {
              * array to a boolean.
              */
             const convertedSchemaObject = schemaObject;
-            if (required !== undefined) {
+            if (required !== undefined && Array.isArray(required)) {
                 convertedSchemaObject.required = required;
             }
             // @nestjs/swagger expects OpenAPI 3.0-style schema objects