arb / celebrate

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

Difference between 'body' and '[Segments.BODY]' (Question) #225

Closed hyoretsu closed 3 years ago

hyoretsu commented 3 years ago

I first learned to use it with Segments.BODY, the docs use it and a lot of project also use it. But I recently found out using IntelliSense that celebrate() has a body property that checks req.body, and it seems to work just the same as my previous implementaion.

Is there any actual difference between the two?

arb commented 3 years ago

I don't think I understand the question. Segments.BODY is to help people write schemas and not have to use the string "body" etc. I'm not sure what you are asking about a body property on celebrate? Even if IntelliSense says it's there, it not part of the public API regardless.

hyoretsu commented 3 years ago

Asking if there's any difference between using this

 celebrate({
  [Segments.BODY]: {
   id: Joi.string().required(),
  },
 }),

or this, as IntelliSense pointed out. (When body is set, joi will validate req.body with the supplied schema.)

 celebrate({
  body: {
   id: Joi.string().required(),
  },
 }),

When I changed the former to the latter, everything looked like it's working just fine. Even though most people and the docs use [Segments.BODY]

arb commented 3 years ago

No, there would be no functional difference in those two styles.

hyoretsu commented 3 years ago

Oh, great. Tyvm