class-validator
class-validator copied to clipboard
question: strip field with whitelist if ValidateIf return false
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
Hey! @gaetansenn I have the same problem, did you find a solution?
@gaetansenn @chambber I have the same problem too.
BTW this is also good: https://stackoverflow.com/a/70128576/8784518
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 }
#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 undefined
s by simply doing something like this: JSON.parse(JSON.stringify({a:undefined}))
.