Skip to content

Commit e735126

Browse files
committed
feat: add IsNotBlank decorator and corresponding tests
1 parent 0ea279b commit e735126

File tree

3 files changed

+64
-1
lines changed

3 files changed

+64
-1
lines changed

src/decorator/common/IsNotBlank.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import { ValidationOptions } from '../ValidationOptions';
2+
import { buildMessage, ValidateBy } from './ValidateBy';
3+
4+
export const IS_NOT_BLANK = "isNotBlank";
5+
6+
export function isNotBlank(value: unknown): boolean {
7+
return (
8+
!!value && typeof value === "string" && value.trim().length > 0
9+
);
10+
}
11+
12+
export function IsNotBlank(
13+
validationOptions?: ValidationOptions,
14+
): PropertyDecorator {
15+
return ValidateBy(
16+
{
17+
name: IS_NOT_BLANK,
18+
validator: {
19+
validate: (value): boolean => isNotBlank(value),
20+
defaultMessage: buildMessage(
21+
(eachPrefix) => eachPrefix + "$property should not be blank",
22+
validationOptions,
23+
),
24+
},
25+
},
26+
validationOptions,
27+
);
28+
}

src/decorator/decorators.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ export * from './common/Equals';
2121
export * from './common/NotEquals';
2222
export * from './common/IsEmpty';
2323
export * from './common/IsNotEmpty';
24+
export * from './common/IsNotBlank';
2425
export * from './common/IsIn';
2526
export * from './common/IsNotIn';
2627

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

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,8 @@ import {
124124
notEquals,
125125
isEmpty,
126126
isNotEmpty,
127+
IsNotBlank,
128+
isNotBlank,
127129
isIn,
128130
isNotIn,
129131
isDateString,
@@ -422,7 +424,7 @@ describe('IsEmpty', () => {
422424
});
423425

424426
describe('IsNotEmpty', () => {
425-
const validValues = ['a', 'abc'];
427+
const validValues = ['a', 'abc', ' '];
426428
const invalidValues = ['', undefined, null];
427429

428430
class MyClass {
@@ -453,6 +455,38 @@ describe('IsNotEmpty', () => {
453455
});
454456
});
455457

458+
describe('IsNotBlank', () => {
459+
const validValues = ['a', 'abc'];
460+
const invalidValues = ['', undefined, null, ' '];
461+
462+
class MyClass {
463+
@IsNotBlank()
464+
someProperty: string;
465+
}
466+
467+
it('should not fail if validator.validate said that its valid', () => {
468+
return checkValidValues(new MyClass(), validValues);
469+
});
470+
471+
it('should fail if validator.validate said that its invalid', () => {
472+
return checkInvalidValues(new MyClass(), invalidValues);
473+
});
474+
475+
it('should not fail if method in validator said that its valid', () => {
476+
validValues.forEach(value => expect(isNotBlank(value)).toBeTruthy());
477+
});
478+
479+
it('should fail if method in validator said that its invalid', () => {
480+
invalidValues.forEach(value => expect(isNotBlank(value)).toBeFalsy());
481+
});
482+
483+
it('should return error object with proper data', () => {
484+
const validationType = 'isNotBlank';
485+
const message = 'someProperty should not be blank';
486+
return checkReturnedError(new MyClass(), invalidValues, validationType, message);
487+
});
488+
})
489+
456490
describe('IsIn', () => {
457491
const constraint = ['foo', 'bar'] as const;
458492
const validValues = ['foo', 'bar'];

0 commit comments

Comments
 (0)