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

question: What is difference between @IsDefined x @IsNotEmpty()?

Open mknparreira opened this issue 3 years ago • 1 comments

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

mknparreira avatar Apr 30 '22 15:04 mknparreira

/**
 * 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!

martin-yin avatar May 04 '22 15:05 martin-yin

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.

NoNameProvided avatar Nov 16 '22 03:11 NoNameProvided

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.

github-actions[bot] avatar Dec 17 '22 00:12 github-actions[bot]