darraghoriordan / eslint-plugin-nestjs-typed

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

Invalid tests ? #68

Closed tangb closed 1 year ago

tangb commented 1 year ago

Hi,

I'm just playing with your project (thank you very much to share it ;-) ) And I'm just thinking there is maybe an issue with a test in all-properties-are-whitelisted rule. If I'm right, this rule checks all properties have at least one validation decorator.

However in this valid test in allPropertiesAreWhitelisted.spec.ts (line ~53)

import { IsInt } from 'sequelize-typescript';

class A {
  @IsInt()
  b: string

  b: string
}

I expect this piece of code to be invalid because a validator is missing in second b class member.

Watching at the code, I think this condition is invalid in some cases:

if (withDecorator.length > 0 && withoutDecorator.length > 0) {

If all properties have no class-validator decorator, withDecorator array will contain nothing and the nested statement won't be called event if withoutDecorator contains some values. So no error will be reported.

If this is correct there are other valid tests that are invalid What do you think ?

Thank you ;-)

darraghoriordan commented 1 year ago

The inconsistency you see with your test is that you're importing from sequelize-typescript. This rule only works for imports from 'class-validator'. If you change your import to use class-validator it will work as expected.

The condition is withDecorator.length > 0 is intentional - we only want to flag DTOs where there is at least one other property has a validator applied.

Sometimes we don't care about validation of a DTO so we can leave all validators off the properties in those cases. Usually though, if you're validating one property, you want to explicitly validate them all. Even if some are just Allow(). This is the pattern that this rule enforces.

tangb commented 1 year ago

I understand your position about leaving all validators off sometimes ;-) Thank you for your reply.

darraghoriordan commented 1 year ago

Thanks for reporting it! 👍