darraghoriordan / eslint-plugin-nestjs-typed

Some eslint rules for working with NestJs projects
http://www.darraghoriordan.com
171 stars 34 forks source link

Using constants as options for `@ApiProperty` breaks `api-property-returning-array-should-set-array` #66

Closed nidomiro closed 1 year ago

nidomiro commented 1 year ago

I currently get the following error when linting:

Cannot read properties of undefined (reading 'name')

Occurred while linting [...]/add-job.dto.ts:36
          Rule: "@darraghor/nestjs-typed/api-property-returning-array-should-set-array"
          Pass --verbose to see the stacktrace.

It seems to be a problem to use any constant inside of @ApiProperty, as both {...swaggerImportDefinitionTypeOptions} and swaggerImportDefinitionTypeOptions will result in the same error.

here is the content of add-job.dto.ts:

import { ApiProperty, ApiPropertyOptional, getSchemaPath } from '@nestjs/swagger';
import { Type } from 'class-transformer';
import { IsOptional, ValidateNested } from 'class-validator';

import { CronStringWithSeconds, IsCronStringWithSeconds } from '[...]';

import {
    classTransformerImportDefinitionTypeOptions,
    FromDate,
    FromLastSuccessfulImport,
    FullImport,
    ImportDefinition,
    ImportDefinitionBase,
    swaggerImportDefinitionTypeOptions,
} from './import-definition.dto';

export class JobSchedule {
    @ApiProperty({
        description:
            'The cron pattern to use for the automatic import interval. Optionally you can add seconds to the pattern.',
        example: {
            everyMinute: '0 * * ? * * *',
        },
        type: 'string',
    })
    @IsCronStringWithSeconds()
    @Type(() => String)
    pattern!: CronStringWithSeconds;
}

export class AddJobDto {
    @ApiProperty({ // It fails here
       ...swaggerImportDefinitionTypeOptions,
    })
    @ValidateNested()
    @Type(() => ImportDefinitionBase, classTransformerImportDefinitionTypeOptions)
    importDefinition!: ImportDefinition;

    @ApiPropertyOptional({
        description: 'If you define this property, the job will not be executed immediately, but on the defined schedule.',
        type: JobSchedule,
    })
    @ValidateNested()
    @Type(() => JobSchedule)
    @IsOptional()
    jobSchedule?: JobSchedule;
}

swaggerImportDefinitionTypeOptions is defined as follows:

export const swaggerImportDefinitionTypeOptions = {
    oneOf: [
        {
            $ref: getSchemaPath(FullImport),
        },
        {
            $ref: getSchemaPath(FromDate),
        },
        {
            $ref: getSchemaPath(FromLastSuccessfulImport),
        },
    ],
    discriminator: {
        propertyName: 'kind' satisfies keyof Pick<ImportDefinitionBase, 'kind'>,
    },
} satisfies ApiPropertyOptions;

I'm using:

"@darraghor/eslint-plugin-nestjs-typed": "^3.22.4",
darraghoriordan commented 1 year ago

thanks for reporting

fixed in 3.22.5

the rule will ignore anything that isn't an object expression now

nidomiro commented 1 year ago

Thanks for the fix👍

nidomiro commented 1 year ago

@darraghoriordan I can confirm that it works with @ApiProperty(swaggerImportDefinitionTypeOptions), but @ApiProperty({ ...swaggerImportDefinitionTypeOptions}) still fails with the same error

darraghoriordan commented 1 year ago

ah right, i had fixed @ApiProperty( ...swaggerImportDefinitionTypeOptions) but missed the object expression wrapper.

this (@ApiProperty({ ...swaggerImportDefinitionTypeOptions}) is fixed in 3.22.6 👍