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.

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

See original GitHub issue

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?

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:11 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
tmikaeldcommented, Apr 12, 2019

It is

1reaction
nidkilcommented, Apr 12, 2019

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

Read more comments on GitHub >

github_iconTop Results From Across the Web

Python Read JSON File – How to Load ... - freeCodeCamp
JSON files have specific rules that determine which data types are valid for keys and values. ... According to the Python Documentation: Keys...
Read more >
Working with JSON - Learn web development | MDN
In this article, we've given you a simple guide to using JSON in your programs, including how to create and parse JSON, and...
Read more >
How to read an external local JSON file in JavaScript?
The loading of a .json file from harddisk is an asynchronous operation and thus it needs to specify a callback function to execute...
Read more >
Importing JSON files - Product Documentation | ServiceNow
These examples demonstrate how to import various types of JSON data and the necessary path for each row. JSON files that you import...
Read more >
Working With JSON Data in Python
response = requests.get("https://jsonplaceholder.typicode.com/todos") todos = json.loads(response.text). You don't believe this works? Fine, run the file in ...
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