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

AbilityBuilder fails when the ability factory is an arrow function #981

Closed
forivall opened this issue Oct 18, 2024 · 0 comments · Fixed by #982
Closed

AbilityBuilder fails when the ability factory is an arrow function #981

forivall opened this issue Oct 18, 2024 · 0 comments · Fixed by #982
Labels

Comments

@forivall
Copy link
Contributor

Describe the bug

AbilityBuilder fails when the ability factory is an arrow function with the error

TypeError: Cannot read properties of undefined (reading 'possibleRulesFor')
  at isAbilityClass (node_modules/@casl/ability/src/AbilityBuilder.ts:87:8)
  at AbilityBuilder.build (node_modules/@casl/ability/src/AbilityBuilder.ts:124:5)

This is because arrow functions don't have a .prototype property, and so isAbilityClass fails.

function isAbilityClass(factory: AbilityFactory<any>): factory is AnyClass {
  return typeof factory.prototype.possibleRulesFor === 'function';
  //                    ^ does not exist on arrow functions!
}

To Reproduce
Steps to reproduce the behavior:

const wrappedCreateMongoAbility = (rules, options) => createMongoAbility(rules, { myOption: true, ...options });
const builder = new AbilityBuilder(wrappedCreateMongoAbility);
builder.can('read', 'foo');
builder.build();

Expected behavior

AbilityBuilder should work with arrow functions as the factory.

Interactive example (optional, but highly desirable)
N/A

CASL Version

@casl/ability - v6.7.1
@casl/mongoose - v8.0.2

Environment:

NodeJS v20.13.1

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

Successfully merging a pull request may close this issue.

1 participant