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

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

Closed
nidkil opened this issue Apr 12, 2019 · 11 comments
Closed
Labels

Comments

@nidkil
Copy link

nidkil commented Apr 12, 2019

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?

@stalniy
Copy link
Owner

stalniy commented Apr 12, 2019

Some example would be helpful

@nidkil
Copy link
Author

nidkil commented Apr 12, 2019

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.

@tmikaeld
Copy link

I think he means this:

Image 2019-04-12 at 10 30 58 AM

@nidkil
Copy link
Author

nidkil commented Apr 12, 2019

That is the code I shared above...

@tmikaeld
Copy link

@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.

@nidkil
Copy link
Author

nidkil commented Apr 12, 2019

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

@nidkil
Copy link
Author

nidkil commented Apr 12, 2019

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

@tmikaeld
Copy link

It is

@stalniy
Copy link
Owner

stalniy commented Apr 12, 2019

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

@nidkil
Copy link
Author

nidkil commented Apr 15, 2019

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

@stalniy
Copy link
Owner

stalniy commented Apr 15, 2019

Ok, closing this

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

No branches or pull requests

3 participants