Skip to content

Commit 5369c7b

Browse files
arkistNoNameProvided
authored andcommitted
fix: isNotEmptyObject nullable option works opposite way
1 parent 1f4a89c commit 5369c7b

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
@@ -3196,61 +3196,81 @@ describe('IsNotEmptyObject', () => {
31963196
[],
31973197
[{ key: 'value' }],
31983198
];
3199-
const nullableValidValues = [{ key: 'value' }, { key: 'value' }];
3200-
const nullableInvalidValues = [
3201-
null,
3202-
undefined,
3203-
'{ key: "value" }',
3204-
"{ 'key': 'value' }",
3205-
'string',
3206-
1234,
3207-
false,
3208-
{},
3209-
{ key: undefined },
3210-
{ key: null },
3211-
[],
3212-
[{ key: 'value' }],
3213-
];
32143199

32153200
class MyClass {
32163201
@IsNotEmptyObject()
32173202
someProperty: object;
32183203
}
32193204

3220-
class NullableMyClass {
3221-
@IsNotEmptyObject({ nullable: true })
3222-
someProperty: object;
3223-
}
3224-
3225-
it.each([
3226-
[new MyClass(), validValues],
3227-
[new NullableMyClass(), nullableValidValues],
3228-
])('should not fail if validator.validate said that its valid', (validationObject, values) => {
3229-
return checkValidValues(validationObject, values);
3205+
it('should not fail if validator.validate said that its valid', () => {
3206+
return checkValidValues(new MyClass(), validValues);
32303207
});
32313208

3232-
it.each([
3233-
[new MyClass(), invalidValues],
3234-
[new NullableMyClass(), nullableInvalidValues],
3235-
])('should fail if validator.validate said that its invalid', (validationObject, values) => {
3236-
return checkInvalidValues(validationObject, values);
3209+
it('should fail if validator.validate said that its invalid', () => {
3210+
return checkInvalidValues(new MyClass(), invalidValues);
32373211
});
32383212

32393213
it('should not fail if method in validator said that its valid', () => {
32403214
validValues.forEach(value => expect(isNotEmptyObject(value)).toBeTruthy());
3241-
nullableValidValues.forEach(value => expect(isNotEmptyObject(value, { nullable: true })).toBeTruthy());
32423215
});
32433216

32443217
it('should fail if method in validator said that its invalid', () => {
32453218
invalidValues.forEach(value => expect(isNotEmptyObject(value)).toBeFalsy());
3246-
nullableInvalidValues.forEach(value => expect(isNotEmptyObject(value, { nullable: true })).toBeFalsy());
32473219
});
32483220

32493221
it('should return error object with proper data', () => {
32503222
const validationType = 'isNotEmptyObject';
32513223
const message = 'someProperty must be a non-empty object';
32523224
return checkReturnedError(new MyClass(), invalidValues, validationType, message);
32533225
});
3226+
3227+
describe('with `nullable` option', () => {
3228+
const nullableValidValues = validValues
3229+
const nullableInvalidValues = invalidValues
3230+
const nonNullableValidValues = [{ key: 'value' }, { key: 'value' }];
3231+
const nonNullableInvalidValues = [
3232+
null,
3233+
undefined,
3234+
'{ key: "value" }',
3235+
"{ 'key': 'value' }",
3236+
'string',
3237+
1234,
3238+
false,
3239+
{},
3240+
{ key: undefined },
3241+
{ key: null },
3242+
[],
3243+
[{ key: 'value' }],
3244+
];
3245+
class NullableMyClass {
3246+
@IsNotEmptyObject({ nullable: true })
3247+
someProperty: object;
3248+
}
3249+
class NonNullableMyClass {
3250+
@IsNotEmptyObject({ nullable: false })
3251+
someProperty: object;
3252+
}
3253+
3254+
it('should not fail if validator.validate said that its valid', async () => {
3255+
await checkValidValues(new NullableMyClass(), nullableValidValues);
3256+
await checkValidValues(new NonNullableMyClass(), nonNullableValidValues);
3257+
});
3258+
3259+
it('should fail if validator.validate said that its valid', async () => {
3260+
await checkInvalidValues(new NullableMyClass(), nullableInvalidValues);
3261+
await checkInvalidValues(new NonNullableMyClass(), nonNullableInvalidValues);
3262+
});
3263+
3264+
it('should not fail if method in validator said that its valid', () => {
3265+
nullableValidValues.forEach(value => expect(isNotEmptyObject(value, { nullable: true })).toBeTruthy());
3266+
nonNullableValidValues.forEach(value => expect(isNotEmptyObject(value, { nullable: false })).toBeTruthy());
3267+
});
3268+
3269+
it('should fail if method in validator said that its invalid', () => {
3270+
nullableInvalidValues.forEach(value => expect(isNotEmptyObject(value, { nullable: true })).toBeFalsy());
3271+
nonNullableInvalidValues.forEach(value => expect(isNotEmptyObject(value, { nullable: false })).toBeFalsy());
3272+
});
3273+
})
32543274
});
32553275

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

0 commit comments

Comments
 (0)