question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

@casl/vue: wrong 'do' prop check in can.ts

See original GitHub issue

Describe the bug Cannot use I prop in Can component

To Reproduce

<Can I="read" a="Post"></Can>

It’s an example from docs

Expected behavior Can component should use I or do prop.

if I is undefined then use do prop

or if do is undefined then use I prop.

Let’s see can.ts Line 66

const $props = props as Record<string, any>;
let actionProp = 'do';
let subjectProp = 'on';

if (!(actionProp in props)) {
  actionProp = 'I';
  subjectProp = detectSubjectProp(props);
}

I checked props parameter in debugger. Even if when I use I property but props has a key of do with undefined value.

I think that condition should be like below

if ($props[actionProp] === undefined) {
  actionProp = 'I';
  subjectProp = detectSubjectProp(props);
}

and also detectSubjectProp too

function detectSubjectProp(props: Record<string, unknown>) {
  if (props['a'] !== undefined) {
    return 'a';
  }

  if (props['this'] !== undefined) {
    return 'this';
  }

  if (props['an'] !== undefined) {
    return 'an';
  }

  return '';
}

It works for me !

CASL Version @casl/ability - v6.3.1 @casl/vue - v2.2.0

Environment: Node: v16.17.0

Issue Analytics

  • State:closed
  • Created a year ago
  • Reactions:2
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
ahascommented, Oct 31, 2022

I don’t understand what it means that you cannot use I prop? there is a test that ensures this is possible -> https://github.com/stalniy/casl/blob/master/packages/casl-vue/spec/can.spec.js#L8

You used in operator to check actionProp is do or I. Vue initializes component property do with undefined even if I used I property without do.

For example

<Can I="create" a="Post">...</Can>

Properties

{
  "I": "create",
  "do": undefined, // 'do' key exists. Vue always initializes with undefined value even if when you don't use 'do'.
  "a": "Post",
  // Skipped
}

so… actionProp is still do and props has a key of do and !(actionProp in props) equals false and $props[actionProp] is undefined then “Neither I nor do prop was passed in <Can>” error will be thrown.

I don’t understand why that test passed. I’m using nuxt3-rc.12 and actually not working.

1reaction
stalniycommented, Dec 17, 2022

looks like from vue@3.1.x or vue@3.2.x the behavior was changed because it worked as expected in vue@3.0.x

Read more comments on GitHub >

github_iconTop Results From Across the Web

Vue error on casl + vue when registering Can component ...
ts (112, 3): The last overload is declared here. My package.json contains: "dependencies": { "@casl/ability": "^4.0 ...
Read more >
<Can> without subject · Issue #107 · stalniy/casl - GitHub
Casl seems to work fine with me leaving it out but the React component gives prop type warnings. ... Tasks: update types for...
Read more >
CASL Vue
Let's consider PROS and CONS of both solutions in order to make the decision. Can Component: PROS: declarative; can cache permissions check results...
Read more >
stalniy-casl/casl - Gitter
I'm evaluating CASL for a FeathersJS/Sequelize/Postgres app. I'm trying to figure out how I would express rules for objects deep in the object...
Read more >
Vue error on casl + vue when registering Can component ...
Coding example for the question Vue error on casl + vue when registering Can component globally-Vue.js.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found