Skip to content

Commit

Permalink
feat: update IsNotBlank decorator to handle non-string values and imp…
Browse files Browse the repository at this point in the history
…rove validation logic
  • Loading branch information
gabriel-logan committed Jan 24, 2025
1 parent 33f245e commit 1bccd18
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 7 deletions.
14 changes: 9 additions & 5 deletions src/decorator/common/IsNotBlank.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,17 @@ export const IS_NOT_BLANK = 'isNotBlank';
/**
* @param value The value to be checked
* @returns true if the value is not blank, false otherwise
* @description
* The value is considered blank if it is null, undefined or empty string
* @description
* Non-string values is considered not blank
*/
export function isNotBlank(value: unknown): boolean {
if (value == null) return false; // Verify if the value is null or undefined
if (value == null) return false;

if (typeof value === 'string') return value.trim().length > 0;

if (typeof value === 'number') return !isNaN(value);

return false; // Any other type is considered invalid
return true;
}

/**
Expand All @@ -24,7 +26,9 @@ export function isNotBlank(value: unknown): boolean {
* @description
* The decorator checks if the value is not blank
* @description
* The value is considered blank if it is null, undefined, empty string or NaN
* The value is considered blank if it is null, undefined or empty string
* @description
* Non-string values is considered not blank
*/
export function IsNotBlank(validationOptions?: ValidationOptions): PropertyDecorator {
return ValidateBy(
Expand Down
4 changes: 2 additions & 2 deletions test/functional/validation-functions-and-decorators.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -456,8 +456,8 @@ describe('IsNotEmpty', () => {
});

describe('IsNotBlank', () => {
const validValues = ['a', 'abc', 0, 1];
const invalidValues = ['', undefined, null, ' ', new Object()];
const validValues = ['a', 'abc', 0, 1, new Object(), [], new Date(), true, false];
const invalidValues = ['', undefined, null, ' '];

class MyClass {
@IsNotBlank()
Expand Down

0 comments on commit 1bccd18

Please sign in to comment.