diegohaz/bodymen

Is body-parser really needed?

Martinsos opened this issue · 4 comments

You mention in readme that body-parser is needed, but you dont use it in your rest-generator project, how is that? Thanks!

I am using the rest generator and i can tell you that it is included.

I have also found this place by using rest-generator. I am really curious, what kind of problems bodymen solves?

@kbzowski It parses and validates the request body based on a schema.

FYI, I'm working on a new project that will replace querymen and bodymen with a pretty similar API: https://github.com/diegohaz/schm

This is the initial work regarding express middlewares: https://github.com/diegohaz/schm/tree/master/packages/schm-express

Still lacks features such as pagination

@kbzowski as Diego told you.

Bodymen is a bunch of middlewares that helps you to map and validate the payload ( body) that you are receiving ...

Instead of doing this in your controller :

export const save = ({ bodymen: { body }, params, user }, res, next) => {

    if (!body.userName) {
        res.status(400).json({
            isError: true,
            param: 'userName',
            message: 'userName was empty or null'
        });
 });

you could use bodymen and doing something like this  : 

router.post('/save',
  token({ required: true }),
  bodymen.middleware({
  	userName:{
  		required: true,
  		type: String
  	}
  }),
  save);

and whola.... the error code should be sent !

Regards