Skip to content

Commit

Permalink
fix: isNotEmptyObject nullable option works opposite way
Browse files Browse the repository at this point in the history
  • Loading branch information
arkist committed Feb 17, 2022
1 parent b4796a8 commit 33be210
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 33 deletions.
2 changes: 1 addition & 1 deletion src/decorator/object/IsNotEmptyObject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
84 changes: 52 additions & 32 deletions test/functional/validation-functions-and-decorators.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3070,61 +3070,81 @@ 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', () => {
const validationType = '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', () => {
Expand Down

0 comments on commit 33be210

Please sign in to comment.