Skip to content
This repository has been archived by the owner on May 6, 2024. It is now read-only.

Cannot read properties of undefined - isLazyTypeFunc #28

Closed
PsclDev opened this issue Mar 24, 2023 · 2 comments
Closed

Cannot read properties of undefined - isLazyTypeFunc #28

PsclDev opened this issue Mar 24, 2023 · 2 comments

Comments

@PsclDev
Copy link

PsclDev commented Mar 24, 2023

After looking through the issues I saw that this was already reported two times, which should be fixed with the MR #18, but seems still an Issue here.

My only dto:

import { PartialType } from '@nestjs/swagger';
import { createZodDto } from 'nestjs-zod';
import { z } from 'nestjs-zod/z';

const CreateLinkSchema = z.object({
  url: z.string().url(),
  slug: z.string().min(1).max(15),
});

export class CreateLinkDto extends createZodDto(CreateLinkSchema) {}
export class UpdateLinkDto extends PartialType(CreateLinkDto) {}

main.ts

import { Logger } from '@nestjs/common';
import { NestFactory } from '@nestjs/core';
import { DocumentBuilder, SwaggerModule } from '@nestjs/swagger';
import { patchNestJsSwagger } from 'nestjs-zod';
import { AppModule } from './app.module';
import env from './env';

async function bootstrap() {
  const logger = new Logger('Main');
  const app = await NestFactory.create(AppModule);

  if (env.devMode) {
    patchNestJsSwagger();
    const documentBuilder = new DocumentBuilder()
      .setTitle('URL Shortener backend')
      .setVersion('1.0')
      .build();
    const document = SwaggerModule.createDocument(app, documentBuilder);
    SwaggerModule.setup('docs', app, document);
  }

  logger.log(`App listening on port ${env.port}`);
  await app.listen(env.port);
}
bootstrap();

Throws this exception:

path/backend/node_modules/.pnpm/@nestjs+swagger@6.2.1_bgmtsjzu2jyijhogwtog4e3a6e/node_modules/@nestjs/swagger/dist/services/schema-object-factory.js:89
        if (this.isLazyTypeFunc(type)) {
                 ^
TypeError: Cannot read properties of undefined (reading 'isLazyTypeFunc')
    at exploreModelSchema (path/backend/node_modules/.pnpm/@nestjs+swagger@6.2.1_bgmtsjzu2jyijhogwtog4e3a6e/node_modules/@nestjs/swagger/dist/services/schema-object-factory.js:89:18)
    at SchemaObjectFactory.e.exploreModelSchema (path/backend/node_modules/.pnpm/nestjs-zod@1.2.3_kwb2a4f7bk4iyyo5b2u32jtvxm/node_modules/nestjs-zod/dist/index.js:1:13958)
    at path/backend/node_modules/.pnpm/@nestjs+swagger@6.2.1_bgmtsjzu2jyijhogwtog4e3a6e/node_modules/@nestjs/swagger/dist/services/schema-object-factory.js:33:36
    at Array.map (<anonymous>)
    at SchemaObjectFactory.createFromModel (path/backend/node_modules/.pnpm/@nestjs+swagger@6.2.1_bgmtsjzu2jyijhogwtog4e3a6e/node_modules/@nestjs/swagger/dist/services/schema-object-factory.js:20:45)
    at exploreApiParametersMetadata (path/backend/node_modules/.pnpm/@nestjs+swagger@6.2.1_bgmtsjzu2jyijhogwtog4e3a6e/node_modules/@nestjs/swagger/dist/explorers/api-parameters.explorer.js:35:55)
    at path/backend/node_modules/.pnpm/@nestjs+swagger@6.2.1_bgmtsjzu2jyijhogwtog4e3a6e/node_modules/@nestjs/swagger/dist/swagger-explorer.js:72:45
    at Array.reduce (<anonymous>)
    at path/backend/node_modules/.pnpm/@nestjs+swagger@6.2.1_bgmtsjzu2jyijhogwtog4e3a6e/node_modules/@nestjs/swagger/dist/swagger-explorer.js:71:104
    at path/backend/node_modules/.pnpm/lodash@4.17.21/node_modules/lodash/lodash.js:13469:38

package.json - reduced to minimum

  "dependencies": {
    "@nestjs/common": "^9.0.0",
    "@nestjs/core": "^9.0.0",
    "@nestjs/platform-express": "^9.0.0",
    "nestjs-zod": "^1.2.3",
    "zod": "^3.21.4"
  },
  "devDependencies": {
    "@nestjs/cli": "^9.0.0",
    "@nestjs/schematics": "^9.0.0",
    "@nestjs/swagger": "^6.2.1",
    "@nestjs/testing": "^9.0.0",
    "tsconfig-paths": "4.1.0",
    "typescript": "^4.7.4"
  }
@risen228
Copy link
Owner

risen228 commented Apr 2, 2023

@PsclDev Hello! The problem is that PartialType accepts the regular class and searches for its fields using reflect-metadata. But ZodDto is another type of thing:

export interface ZodDto<
  TOutput = any,
  TDef extends ZodTypeDef = ZodTypeDef,
  TInput = TOutput
> {
  new (): TOutput
  isZodDto: true
  schema: ZodSchema<TOutput, TDef, TInput>
  create(input: unknown): TOutput
}

So, I think PartialType cannot be used here. Instead, I suggest you to create partial DTO in this way:

const UpdateLinkSchema = CreateLinkSchema.partial()
export class UpdateLinkDto extends createZodDto(UpdateLinkSchema) {}

To fix this on library level, we need a completely new approach for defining ZodDto type. Now I have no thoughts how to solve it.

@PsclDev
Copy link
Author

PsclDev commented Apr 2, 2023

Thanks for the response, quickly tested it and works like a charm.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants