koajs / router

Router middleware for Koa. Maintained by @forwardemail and @ladjs.
MIT License
849 stars 174 forks source link

Route schema validation #171

Closed Pietrohl closed 3 weeks ago

Pietrohl commented 12 months ago

Checklist

Validation Schema to routes verb methods

This PR is to resolve the issue 170 for a possible new feature, to add a function overload to route[verb] function so it can accept a schema validation as second parameter.

Example usage:

    const app = new Koa();
    const router = new Router();

    const schema = {
      type: 'object',
      properties: {
        x: { type: 'integer' }
      },
      required: ['x'],
      additionalProperties: false
    };

    router.post('/user', { body: schema }, function (ctx, next) {
      ctx.status = 201;
      ctx.body = { message: 'User added!' };
      }
    );
socket-security[bot] commented 12 months ago

New dependencies detected. Learn more about Socket for GitHub ↗︎

Packages Version New capabilities Transitives Size Publisher
@koa/bodyparser 5.0.0 None +7 473 kB fengmk2
3imed-jaberi commented 3 weeks ago

@Pietrohl Thanks for your proposal, I think currently we are on the rethinking step about the router, and for sure this feature will be one of the points on our list. Maybe add your implementation as module to be a recipe under the recipes section on README.md can be a great option, like multi-verbs module here.

All what you need after publishing the small validation module, open issue and notify me.

In the meanwhile, If you really want the validation feature, I recommend you to use @koa-better-modules/joi-router which is the up to date version of koa-joi-router.