class-validator icon indicating copy to clipboard operation
class-validator copied to clipboard

question: strip field with whitelist if ValidateIf return false

Open gaetansenn opened this issue 3 years ago • 2 comments

I was trying to... I would like to handle the whitelist option when validateIf() is provided

@ValidateIf((o) => o.become === EventSlotCareBecome.DECEASED)
@IsString()
@IsNotEmpty()
becomeNote: string;

The problem: If validateIf return false the whitelisting option https://github.com/typestack/class-validator#whitelisting doesn't strip the property. ValidateIf is supposed to ignore the validators on a property when the provided condition function returns false.

Thanks

gaetansenn avatar Jan 11 '22 14:01 gaetansenn

Hey! @gaetansenn I have the same problem, did you find a solution?

chambber avatar Feb 24 '22 11:02 chambber

@gaetansenn @chambber I have the same problem too.

BTW this is also good: https://stackoverflow.com/a/70128576/8784518

kasir-barati avatar May 10 '22 08:05 kasir-barati

Is there an update to this problem? I understand that https://stackoverflow.com/a/70128576/8784518 provides a workaround solution (setting the value of an undesired field to undefined), but the resulting object still contains the undesired key.

e.g. @gaetansenn's example, if the ValidateIf check fails (o.become !== EventSlotCareBecome.DECEASED), the resulting object would still include { ..., becomeNote: undefined }

joshuakarp avatar Nov 09 '22 22:11 joshuakarp

#NSFW

@joshuakarp since I am using #prisma and it ignores the keys that has undefined as their value I do not need to do anything. But for you and those who does not have this feature you have to ignore undefineds by simply doing something like this: JSON.parse(JSON.stringify({a:undefined})).

kasir-barati avatar Nov 10 '22 04:11 kasir-barati