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

[CLASS_TRANSFORMERS] Extends class-transform package with EnumFallback decorator #837

Merged
merged 6 commits into from
Dec 26, 2023

Conversation

amarlankri
Copy link
Contributor

@amarlankri amarlankri commented Dec 21, 2023

Description

Create class-transformers package with new decorator EnumFallback. This decorator can be used as follows:

import { EnumFallback } from '@algoan/nestjs-class-transformers';

export enum UserRole {
  ADMIN = 'ADMIN',
  READER = 'READER',
}

class User {
  @EnumFallback({
    type: UserRole,
    fallback: (value: UserRole) => UserRole.READER // if the role is not "ADMIN" or "READER", then the role will be "READER".
  })
  public role?: UserRole;
}

Finishes implementation started in #836

Motivation and Context

Fix #834

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)

@amarlankri amarlankri added the enhancement New feature or request label Dec 21, 2023
@amarlankri amarlankri marked this pull request as ready for review December 21, 2023 13:30
}

if (!Object.values(type).includes(value)) {
return fallback(value);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If there is error in the fallback function, you think we should throw error or return a specific value?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think so 🤔 Unlike the logging feature, this decorator may have a real impact on the application behaviour. In my opinion, it is more like the function you pass to Array.map for instance. It means that if the given function fails, it is not the responsibility to the library to handle it.

What do you think?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, so let the client handle it himself.

expect(user.role).toEqual('WRITER');
});

it('should return the fallback value for each invalid element if the property is an array', async () => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you please add a test when the fallback function is more than just returning a value? e.g. verifying a logging as we discussed

@meriamBenSassi meriamBenSassi merged commit 9cfc61d into algoan:master Dec 26, 2023
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[CLASS_TRANSFORMER] Allow to give fallback value to enum
5 participants