Skip to content

Commit 33be210

Browse files
committed
fix: isNotEmptyObject nullable option works opposite way
1 parent b4796a8 commit 33be210

File tree

2 files changed

+53
-33
lines changed

2 files changed

+53
-33
lines changed

src/decorator/object/IsNotEmptyObject.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ export function isNotEmptyObject(value: unknown, options?: { nullable?: boolean
1313
return false;
1414
}
1515

16-
if (options?.nullable === true) {
16+
if (options?.nullable === false) {
1717
return !Object.values(value).every(propertyValue => propertyValue === null || propertyValue === undefined);
1818
}
1919

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

Lines changed: 52 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -3070,61 +3070,81 @@ describe('IsNotEmptyObject', () => {
30703070
[],
30713071
[{ key: 'value' }],
30723072
];
3073-
const nullableValidValues = [{ key: 'value' }, { key: 'value' }];
3074-
const nullableInvalidValues = [
3075-
null,
3076-
undefined,
3077-
'{ key: "value" }',
3078-
"{ 'key': 'value' }",
3079-
'string',
3080-
1234,
3081-
false,
3082-
{},
3083-
{ key: undefined },
3084-
{ key: null },
3085-
[],
3086-
[{ key: 'value' }],
3087-
];
30883073

30893074
class MyClass {
30903075
@IsNotEmptyObject()
30913076
someProperty: object;
30923077
}
30933078

3094-
class NullableMyClass {
3095-
@IsNotEmptyObject({ nullable: true })
3096-
someProperty: object;
3097-
}
3098-
3099-
it.each([
3100-
[new MyClass(), validValues],
3101-
[new NullableMyClass(), nullableValidValues],
3102-
])('should not fail if validator.validate said that its valid', (validationObject, values) => {
3103-
return checkValidValues(validationObject, values);
3079+
it('should not fail if validator.validate said that its valid', () => {
3080+
return checkValidValues(new MyClass(), validValues);
31043081
});
31053082

3106-
it.each([
3107-
[new MyClass(), invalidValues],
3108-
[new NullableMyClass(), nullableInvalidValues],
3109-
])('should fail if validator.validate said that its invalid', (validationObject, values) => {
3110-
return checkInvalidValues(validationObject, values);
3083+
it('should fail if validator.validate said that its invalid', () => {
3084+
return checkInvalidValues(new MyClass(), invalidValues);
31113085
});
31123086

31133087
it('should not fail if method in validator said that its valid', () => {
31143088
validValues.forEach(value => expect(isNotEmptyObject(value)).toBeTruthy());
3115-
nullableValidValues.forEach(value => expect(isNotEmptyObject(value, { nullable: true })).toBeTruthy());
31163089
});
31173090

31183091
it('should fail if method in validator said that its invalid', () => {
31193092
invalidValues.forEach(value => expect(isNotEmptyObject(value)).toBeFalsy());
3120-
nullableInvalidValues.forEach(value => expect(isNotEmptyObject(value, { nullable: true })).toBeFalsy());
31213093
});
31223094

31233095
it('should return error object with proper data', () => {
31243096
const validationType = 'isNotEmptyObject';
31253097
const message = 'someProperty must be a non-empty object';
31263098
return checkReturnedError(new MyClass(), invalidValues, validationType, message);
31273099
});
3100+
3101+
describe('with `nullable` option', () => {
3102+
const nullableValidValues = validValues
3103+
const nullableInvalidValues = invalidValues
3104+
const nonNullableValidValues = [{ key: 'value' }, { key: 'value' }];
3105+
const nonNullableInvalidValues = [
3106+
null,
3107+
undefined,
3108+
'{ key: "value" }',
3109+
"{ 'key': 'value' }",
3110+
'string',
3111+
1234,
3112+
false,
3113+
{},
3114+
{ key: undefined },
3115+
{ key: null },
3116+
[],
3117+
[{ key: 'value' }],
3118+
];
3119+
class NullableMyClass {
3120+
@IsNotEmptyObject({ nullable: true })
3121+
someProperty: object;
3122+
}
3123+
class NonNullableMyClass {
3124+
@IsNotEmptyObject({ nullable: false })
3125+
someProperty: object;
3126+
}
3127+
3128+
it('should not fail if validator.validate said that its valid', async () => {
3129+
await checkValidValues(new NullableMyClass(), nullableValidValues);
3130+
await checkValidValues(new NonNullableMyClass(), nonNullableValidValues);
3131+
});
3132+
3133+
it('should fail if validator.validate said that its valid', async () => {
3134+
await checkInvalidValues(new NullableMyClass(), nullableInvalidValues);
3135+
await checkInvalidValues(new NonNullableMyClass(), nonNullableInvalidValues);
3136+
});
3137+
3138+
it('should not fail if method in validator said that its valid', () => {
3139+
nullableValidValues.forEach(value => expect(isNotEmptyObject(value, { nullable: true })).toBeTruthy());
3140+
nonNullableValidValues.forEach(value => expect(isNotEmptyObject(value, { nullable: false })).toBeTruthy());
3141+
});
3142+
3143+
it('should fail if method in validator said that its invalid', () => {
3144+
nullableInvalidValues.forEach(value => expect(isNotEmptyObject(value, { nullable: true })).toBeFalsy());
3145+
nonNullableInvalidValues.forEach(value => expect(isNotEmptyObject(value, { nullable: false })).toBeFalsy());
3146+
});
3147+
})
31283148
});
31293149

31303150
describe('IsLowercase', () => {

0 commit comments

Comments
 (0)