Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
As required by #145 I've added the ability to pass a "context" object/any to the validation function, so it could return true or false based on the context where is used.
I've choose to put the context object as the third parameter because of retro-compatibility reasons:
The resulting usage is this (in a real project, where the validation depends on the state of a report entity):
Directive:
Permission declaration:
Service hasPermission():
Routing
Here comes an issue, we need the report to do the permission validation, the right way would be to "resove" and have it available in the controller, too.
The problem is that the resolvers are evaluated after the guards (https://stackoverflow.com/questions/39190427/angular2-resolve-before-canactivate).
The only solution I can figure out is to create a custom validator that fetch the report, whom is fetched a second time in the controller (or resolver).
This is an example (not fully tested):
Note:
I've added all the tests for the service, but omitted the ones for the directive and the router, I've tried to add some locally and it seems to work, the issue is that really I don't know what and how to test, and I feel I don't really know what I'm doing. I'd like to test what happen if the context changes, but I'm not confident with my testing skills.
The docs has not been edited as well, my english is not really good ;)