Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: read nullable option in @IsNotEmptyObject decorator correctly #1555

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -3196,61 +3196,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