Casl: Loading rules from a JSON file does not work when following the documentation

Created on 12 Apr 2019  路  11Comments  路  Source: stalniy/casl

I was changing the setup of the ability rules from DSL to loading them from a JSON file. To setup the initialization from JSON I was following the section Store ability templates in the documentation here.

Loading the rule templates from the JSON file, initializing the template variables and passing them to Ability worked fine. But once I started checking the rules they did not work as expected. I compared the DSL version to the JSON version using console.log(ability.rules) and could not find any difference.

It turns out the there was a difference between the way the DSL and JSON version were being initialized.

In the DSL version I was following a Medium post by Sergii, which initialized the rules in the following way:

return new Ability(rules, { subjectName });

This is the subjectName function:

const TYPE_KEY = Symbol.for('type');

function subjectName(subject) {
  if (!subject || typeof subject === 'string') {
    return subject;
  }
  return subject[TYPE_KEY];
}

In the JSON template version I was following the section Store ability templates from the documentation here, which was initializing the rules without the subjectName:

return new Ability(rules);

After adding subjectName to the initialization the rules are working as expected. I do not really understand what subjectName is doing. Could you please explain?

Should the documentation be updated?

question

All 11 comments

Some example would be helpful

Of the code? All the code is doing is converting JSON rules to Javascript objects and passing them to Ability. There is no difference between the rules created with DSL and JSON. That was the frustrating part.

I think he means this:

Image 2019-04-12 at 10 30 58 AM

That is the code I shared above...

@nidkil You can edit the description of the issue and describe what was the difference, because it's hard to track issues that have no proper description.

What happened to the discription!?!?!?!?

For some strange reason the discription was not saved. Sorry. Should be visible now.

It is

subjectName is briefly described in Instance Checks. Let me know if that description is not enough or how you would rephrase it

Hi Stalniy, I did not see this description before. It is clear. No need to rephrase it.

Ok, closing this

Was this page helpful?
0 / 5 - 0 ratings

Related issues

SebUndefined picture SebUndefined  路  4Comments

victorsilent picture victorsilent  路  4Comments

Foxh0und picture Foxh0und  路  3Comments

m-weeks picture m-weeks  路  5Comments

ivanwitzke picture ivanwitzke  路  3Comments