Casl: Nested array field condition

Created on 23 Jun 2020  路  3Comments  路  Source: stalniy/casl

How can a condition be created such that a user cannot join an event where he is an organizer:

  event = { ..., organizers: [ { email: 'xxx', ... }, { email: 'yyy', ... } ] }

  // Something like:
  abilities.cannot('join', 'event', { 'orgnizers': { $exist: { 'id': user.id } } } )
question

Most helpful comment

Works completely the same way. You can also check $elemMatch operator.

To answer your question: yes, it works the same way for array item

All 3 comments

Please read the docs:

https://casl.js.org/v4/en/guide/conditions-in-depth#match-one-of-few-items-in-an-array-property

I've seen that, but the issue not the same. In my case it's a property of the array items that needs to match up, not the array item itself.

Or would writing "organizers.email": { $in: [user.email] } work, in spite of the fact that email is not a property of the organizers, but a property of the items in the organizers array?

Works completely the same way. You can also check $elemMatch operator.

To answer your question: yes, it works the same way for array item

Was this page helpful?
0 / 5 - 0 ratings