Skip to content

Commit 58e908e

Browse files
committed
feat: enhance IsNotBlank validation to include numbers and handle null/undefined
1 parent 70e3210 commit 58e908e

File tree

2 files changed

+23
-4
lines changed

2 files changed

+23
-4
lines changed

src/decorator/common/IsNotBlank.ts

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,29 @@ import { buildMessage, ValidateBy } from './ValidateBy';
33

44
export const IS_NOT_BLANK = 'isNotBlank';
55

6+
/**
7+
* @param value The value to be checked
8+
* @returns true if the value is not blank, false otherwise
9+
*/
610
export function isNotBlank(value: unknown): boolean {
7-
return !!value && typeof value === 'string' && value.trim().length > 0;
11+
if (value == null) return false; // Verify if the value is null or undefined
12+
13+
if (typeof value === 'string') return value.trim().length > 0;
14+
15+
if (typeof value === 'number') return !isNaN(value);
16+
17+
return false; // Any other type is considered invalid
818
}
919

20+
/**
21+
* @param validationOptions The validation options
22+
* @returns {PropertyDecorator}
23+
*
24+
* @description
25+
* The decorator checks if the value is not blank
26+
* @description
27+
* The value is considered blank if it is null, undefined, empty string or NaN
28+
*/
1029
export function IsNotBlank(validationOptions?: ValidationOptions): PropertyDecorator {
1130
return ValidateBy(
1231
{

test/functional/validation-functions-and-decorators.spec.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -424,7 +424,7 @@ describe('IsEmpty', () => {
424424
});
425425

426426
describe('IsNotEmpty', () => {
427-
const validValues = ['a', 'abc', ' '];
427+
const validValues = ['a', 'abc', ' ', 0, 1];
428428
const invalidValues = ['', undefined, null];
429429

430430
class MyClass {
@@ -456,8 +456,8 @@ describe('IsNotEmpty', () => {
456456
});
457457

458458
describe('IsNotBlank', () => {
459-
const validValues = ['a', 'abc'];
460-
const invalidValues = ['', undefined, null, ' '];
459+
const validValues = ['a', 'abc', 0, 1];
460+
const invalidValues = ['', undefined, null, ' ', new Object()];
461461

462462
class MyClass {
463463
@IsNotBlank()

0 commit comments

Comments
 (0)