class-validator
class-validator copied to clipboard
question: What is difference between @IsDefined x @IsNotEmpty()?
It's not clear to me the main difference between @IsDefined and @IsNotEmpty.
According to documentation, both do the same: It checks if the given value is not empty, !== null, and undefined.
Besides that, doing my research, I noticed that @IsNotEmpty is most used than @IsDefined.
Thus, may I use both or there is some difference that I don´t get it? Thanks for help
/**
* Checks if value is defined (!== undefined, !== null).
*/
export function isDefined(value: any): boolean {
return value !== undefined && value !== null;
}
/**
* Checks if given value is not empty (!== '', !== null, !== undefined).
*/
export function isNotEmpty(value: unknown): boolean {
return value !== '' && value !== null && value !== undefined;
}
The difference between @IsNotEmpty and @IsDefined is that @IsNotEmpty determines whether it is an empty string.
@IsDefined does not judge strings!
Closing this as answered.
If the issue still persists, you may open a new Q&A in the discussions tab and someone from the community may be able to help.
This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.