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

[FEAT] Add EnumFallback decorator #836

Closed
wants to merge 3 commits into from

Conversation

Bendakh
Copy link
Contributor

@Bendakh Bendakh commented Dec 19, 2023

Description

This PR aims to create a new decorator EnumFallback which allows to provide a fallback function with the enum type.

This is a WIP

Motivation and Context

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)

Comment on lines 9 to 34
export function EnumFallback(enumType: any, fallback: (value: any) => any, validationOptions?: ValidationOptions) {
return function (object: Record<string, any>, propertyName: string) {
registerDecorator({
name: 'enumFallback',
target: object.constructor,
propertyName,
constraints: [enumType, fallback],
options: validationOptions,
validator: {
validate(value: any, args: ValidationArguments) {
const [enumTypeValue, fallbackFn] = args.constraints;
if (value === undefined || !Object.values(enumTypeValue).includes(value)) {
// eslint-disable-next-line
const updatedObject = Object.assign({}, args.object, {
[propertyName]: fallbackFn(value),
});
// eslint-disable-next-line
Object.assign(args.object, updatedObject);
}

return true;
},
},
});
};
}
Copy link
Contributor

Choose a reason for hiding this comment

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

You implemented the solution described in #833 which has been rejected in favour of #834. Sorry, my investigation may not be clear enough.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants