Search code examples
casl

Is it possible to define rules with nested object in CASL


Right now I have 'user' object with nested object named 'author' with id field in it. How can I define rules for that object?

This code doesn't work:

can('manage', 'Post', { author: { id: id } }); 

Neither this one (well, it works, but I save rules in localStorage, and saving functions inside of it is not a good idea)

can('manage', 'Post', { 'author': function(author) { ... } });

Solution

  • You can use dot notation:

    can('manage', 'Post', { 'author.id': id });
    

    You can find more information about query language and common cases to define rules in CASL docs