Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular 2 Route params validation

How can I validate Route params in Angular2? For example, I have a route:

{path: 'artist/:id', component: ArtistInfoComponent}

All I want is to check if parameter id is a valid number and if so, just show the component. Otherwise show a page with error message.

I found Route Guards in Angular 2 but I think this isn't the best way to do validation because I can have many different params for different pages and writing individual Route Guard for all these pages isn't what I want.

Is there a possibility to specify a regex for each route parameter?

UPD: Maybe there is a way to do a validation on query params? I mean if I have such url: /search?q=blablabla&page=2 how can I check if the parameter page is valid? Is there a way to do it without Guards?

like image 439
Taras Avatar asked Dec 25 '16 21:12

Taras


1 Answers

Guards are the way to go, you could put validation configuration into data section of the route and create reusable guard to validate parameters based on this configuration.

like image 150
kemsky Avatar answered Sep 24 '22 06:09

kemsky