stalniy/casl

Provide `forbiddenBecause` message in `Can` child render function

Opened this issue · 0 comments

Is your feature request related to a problem? Please describe.
When using the Can component, it is often desirable to inform the user, visually, why they cannot perform a certain action. However, it is verbose and cumbersome to surface the .because("...") messages that one may define on their abilities. When you use Can you have to:

  1. Write the condition into the props
  2. Write the condition again when either throwing a Forbidden error or just creating one to get the message

Describe the solution you'd like

  • Can's child render function will accept a third argument (forbiddenBecause), which will be the same string as outputted by ForbiddenError.from(ability).unlessCan(...condition)?.message.
  • It will be of type string | undefined. string when allowed is false, returning either the default error message or a defined rule message. It will be undefined when allowed is true.

Describe alternatives you've considered (optional)

  • Repeating the condition to get a Forbidden Error
  • Using getRelevantRulesFor

Additional context (optional)
None