arb / celebrate

A joi validation middleware for Express.
MIT License
1.33k stars 66 forks source link

Middleware letting objects through, supposed to only allow arrays #175

Closed limelier closed 4 years ago

limelier commented 4 years ago

My joi schema looks like this:

Joi.array()

Here is an example value that is not working as expected:

{}

The issue I am having with celebrate is:

Running Joi.array().validate({}); produces the expected validation error, but sending {} as a request body to the following route still prints {}, even though it should not get through.

router.get(
    '/',
    celebrate({
        body: Joi.array(),
    }),
    async (req, res) => {
        console.log(req.body);
    },
);

What's going on?

arb commented 4 years ago

celebrate doesn't validate req.body for GET requests. While it's technically allowed to have a body in GET requests per the HTTP spec, most people don't use GET that way. I also copied how hapi deals with this and hapi does not validate body on GET requests. The code around this is here.

Can you change your HTTP verb to POST instead?

limelier commented 4 years ago

I see, I wasn't familiar with that. Using the POST verb yields a validation error, as expected.

arb commented 4 years ago

I'll add something to the docs clarifying this.

arb commented 4 years ago

Note added in ea22b24ee98d6bc56b229f1613943dbb4d4104ca