vania-dart / framework

Fast, simple, and powerful backend framework for Dart built with ❤️
https://vdart.dev
MIT License
155 stars 12 forks source link

[Feature Request] - Parameter validation conditions for the router #79

Closed javad-zobeidi closed 1 month ago

javad-zobeidi commented 1 month ago

Is your feature request related to a problem? Please describe. I find it challenging to manually validate route parameters in the Router class. This process requires defining parameter types individually for each route, which is repetitive and increases the risk of errors.

Describe the solution you'd like I would like the Router class to have built-in methods for parameter validation, such as whereInt, whereString, and where, which can be applied directly when defining routes. This would streamline the routing process and ensure that parameters are validated correctly.

Describe alternatives you've considered I've considered manually adding validation logic within each route's action, but this approach is not scalable and clutters the codebase. Another alternative is using middleware for validation, but this still requires additional setup for each route.

Additional context Here is an example of how the improved Router class could be used:

Router.get('my-route/{id}', (int id) {
  return Response.json(id);
}).whereInt('id');

Router.get('my-route/show', () {
  return Response.json({'message': 'Show route'});
});

Router.get('my-route/{slug}', (String slug) {
      return Response.json(slug);
    }).where('slug', r'[a-z0-9-]+');