Skip to content

Provide forbiddenBecause message in Can child render function #972

Description

@paulleonartcalvo

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

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions