Loading rules from a JSON file does not work when following the documentation
See original GitHub issueI 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?
Issue Analytics
- State:
- Created 4 years ago
- Comments:11 (3 by maintainers)
Top GitHub Comments
It is
For some strange reason the discription was not saved. Sorry. Should be visible now.