darraghoriordan / eslint-plugin-nestjs-typed

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

validated-non-primitive-property-needs-type-decorator Customization #32

Closed falahati closed 2 years ago

falahati commented 2 years ago

It is helpful if we are able to exclude some data types or list decorators that are identical in functionality to the @Type decorator.

For example, we are currently using custom transformers for Map<> and Date, and altho these decorators take care of all these, this rule still nags about not having the Type decorator which is not required at all; but for this reason, we had to disable it which is unfortunate since it is a very good rule to have active.

Here, TransformDate takes care of transformation and no Type decorator is required:

    @ApiPropertyOptional(
        {
            description: "Filter by date",
            required: false,
            writeOnly: true,
        }
    )
    @Expose()
    @IsOptional()
    @TransformDate()
    @IsDate()
    public fromDate?: Date;

Having options to disable this rule for one or more data types, or disable it when another decorator is used will be very useful for more complex projects.

And thanks for this plugin, very useful for staying away from possible pitfalls.

darraghoriordan commented 2 years ago

hey, thanks for reporting this

Yup, feels like a nice way to improve this would be to allow you to pass a custom list of ignores to the rule.

Not sure if decorator names or data types is best. will think about it :D

darraghoriordan commented 2 years ago

This should be fixed for you in version 3.12.0

you can add any custom type decorators to the options now

"@darraghor/nestjs-typed/validated-non-primitive-property-needs-type-decorator": [
        "error",
        {additionalTypeDecorators: ["TransformDate"]},
    ],

I added a test for your scenario but let me know if there are any issues with it.

falahati commented 2 years ago

thanks @darraghoriordan