From 5369c7bdf80e4407c6ac1cdc2dae81fde7c37b0b Mon Sep 17 00:00:00 2001 From: Jinwoo Oh Date: Wed, 16 Feb 2022 23:28:50 -0800 Subject: [PATCH 1/2] fix: isNotEmptyObject `nullable` option works opposite way --- src/decorator/object/IsNotEmptyObject.ts | 2 +- ...alidation-functions-and-decorators.spec.ts | 84 ++++++++++++------- 2 files changed, 53 insertions(+), 33 deletions(-) diff --git a/src/decorator/object/IsNotEmptyObject.ts b/src/decorator/object/IsNotEmptyObject.ts index 7eada8548a..64aada8014 100644 --- a/src/decorator/object/IsNotEmptyObject.ts +++ b/src/decorator/object/IsNotEmptyObject.ts @@ -13,7 +13,7 @@ export function isNotEmptyObject(value: unknown, options?: { nullable?: boolean return false; } - if (options?.nullable === true) { + if (options?.nullable === false) { return !Object.values(value).every(propertyValue => propertyValue === null || propertyValue === undefined); } diff --git a/test/functional/validation-functions-and-decorators.spec.ts b/test/functional/validation-functions-and-decorators.spec.ts index a96f45b363..9dc3b85972 100644 --- a/test/functional/validation-functions-and-decorators.spec.ts +++ b/test/functional/validation-functions-and-decorators.spec.ts @@ -3196,54 +3196,26 @@ describe('IsNotEmptyObject', () => { [], [{ key: 'value' }], ]; - const nullableValidValues = [{ key: 'value' }, { key: 'value' }]; - const nullableInvalidValues = [ - null, - undefined, - '{ key: "value" }', - "{ 'key': 'value' }", - 'string', - 1234, - false, - {}, - { key: undefined }, - { key: null }, - [], - [{ key: 'value' }], - ]; class MyClass { @IsNotEmptyObject() someProperty: object; } - class NullableMyClass { - @IsNotEmptyObject({ nullable: true }) - someProperty: object; - } - - it.each([ - [new MyClass(), validValues], - [new NullableMyClass(), nullableValidValues], - ])('should not fail if validator.validate said that its valid', (validationObject, values) => { - return checkValidValues(validationObject, values); + it('should not fail if validator.validate said that its valid', () => { + return checkValidValues(new MyClass(), validValues); }); - it.each([ - [new MyClass(), invalidValues], - [new NullableMyClass(), nullableInvalidValues], - ])('should fail if validator.validate said that its invalid', (validationObject, values) => { - return checkInvalidValues(validationObject, values); + it('should fail if validator.validate said that its invalid', () => { + return checkInvalidValues(new MyClass(), invalidValues); }); it('should not fail if method in validator said that its valid', () => { validValues.forEach(value => expect(isNotEmptyObject(value)).toBeTruthy()); - nullableValidValues.forEach(value => expect(isNotEmptyObject(value, { nullable: true })).toBeTruthy()); }); it('should fail if method in validator said that its invalid', () => { invalidValues.forEach(value => expect(isNotEmptyObject(value)).toBeFalsy()); - nullableInvalidValues.forEach(value => expect(isNotEmptyObject(value, { nullable: true })).toBeFalsy()); }); it('should return error object with proper data', () => { @@ -3251,6 +3223,54 @@ describe('IsNotEmptyObject', () => { const message = 'someProperty must be a non-empty object'; return checkReturnedError(new MyClass(), invalidValues, validationType, message); }); + + describe('with `nullable` option', () => { + const nullableValidValues = validValues + const nullableInvalidValues = invalidValues + const nonNullableValidValues = [{ key: 'value' }, { key: 'value' }]; + const nonNullableInvalidValues = [ + null, + undefined, + '{ key: "value" }', + "{ 'key': 'value' }", + 'string', + 1234, + false, + {}, + { key: undefined }, + { key: null }, + [], + [{ key: 'value' }], + ]; + class NullableMyClass { + @IsNotEmptyObject({ nullable: true }) + someProperty: object; + } + class NonNullableMyClass { + @IsNotEmptyObject({ nullable: false }) + someProperty: object; + } + + it('should not fail if validator.validate said that its valid', async () => { + await checkValidValues(new NullableMyClass(), nullableValidValues); + await checkValidValues(new NonNullableMyClass(), nonNullableValidValues); + }); + + it('should fail if validator.validate said that its valid', async () => { + await checkInvalidValues(new NullableMyClass(), nullableInvalidValues); + await checkInvalidValues(new NonNullableMyClass(), nonNullableInvalidValues); + }); + + it('should not fail if method in validator said that its valid', () => { + nullableValidValues.forEach(value => expect(isNotEmptyObject(value, { nullable: true })).toBeTruthy()); + nonNullableValidValues.forEach(value => expect(isNotEmptyObject(value, { nullable: false })).toBeTruthy()); + }); + + it('should fail if method in validator said that its invalid', () => { + nullableInvalidValues.forEach(value => expect(isNotEmptyObject(value, { nullable: true })).toBeFalsy()); + nonNullableInvalidValues.forEach(value => expect(isNotEmptyObject(value, { nullable: false })).toBeFalsy()); + }); + }) }); describe('IsLowercase', () => { From 034347b9abce43d2865f91f3a5e0f3e4c557dad0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Attila=20Ol=C3=A1h?= Date: Sat, 3 Dec 2022 14:31:25 +0000 Subject: [PATCH 2/2] refactor: format code with Prettier --- test/functional/validation-functions-and-decorators.spec.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/test/functional/validation-functions-and-decorators.spec.ts b/test/functional/validation-functions-and-decorators.spec.ts index 9dc3b85972..829183256f 100644 --- a/test/functional/validation-functions-and-decorators.spec.ts +++ b/test/functional/validation-functions-and-decorators.spec.ts @@ -3225,8 +3225,8 @@ describe('IsNotEmptyObject', () => { }); describe('with `nullable` option', () => { - const nullableValidValues = validValues - const nullableInvalidValues = invalidValues + const nullableValidValues = validValues; + const nullableInvalidValues = invalidValues; const nonNullableValidValues = [{ key: 'value' }, { key: 'value' }]; const nonNullableInvalidValues = [ null, @@ -3270,7 +3270,7 @@ describe('IsNotEmptyObject', () => { nullableInvalidValues.forEach(value => expect(isNotEmptyObject(value, { nullable: true })).toBeFalsy()); nonNullableInvalidValues.forEach(value => expect(isNotEmptyObject(value, { nullable: false })).toBeFalsy()); }); - }) + }); }); describe('IsLowercase', () => {