koajs / router

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

[feat] Add request validation guard with jsonSchema #170

Closed Pietrohl closed 3 months ago

Pietrohl commented 1 year ago

Describe the feature

I'm trying to add new overload for the router[method] function, to add a validation middleware easily by passing a jsonSchema as the function second parameter. Right now I only did a quick update to the lib for my use case, but if the maintainers think that this could be good to include to the router I can expend some more time working on it.

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!' };
      }
    );

Checklist

3imed-jaberi commented 3 months ago

Close cause: https://github.com/koajs/router/pull/171#issuecomment-2290155024