3

Do I need to use @hapi/joi with mongoose?

As I understand @hapi/joi is used for validating HTTP request (headers, parameters, body etc.). @hapi/joi provides it's own schema validation. The mongoose also provides it's own schema validation but on another level. If hapi/joi tries to validate HTTP request, than mongoose schema validation ensures that data is valid to be inserted into the database.

As you can see both libraries provide it's own schemas validation (on different levels). It requires additional resource to keep both schema equal to each other (which is a place for bugs).

The question is: Do I need to keep this both libraries and support two schema? Or I can use mongoose and their validation and @hapi/joi is redundancy?

Thanks in advance!

Update:

I found related question, but it is not answering my question anyway :(

Roman Mahotskyi
  • 4,576
  • 5
  • 35
  • 68

1 Answers1

6

You don't have to use a schema validation package like Joi.

But it would be good to use both of them. They compliment each other.

Joi is used for APIs to make sure that the data the client sends is valid. And mongoose schema is used to ensure that our data is in right shape.

A scenario where API validation with Joi makes sense:

We generally hash the user password, so in our user schema the maxlength option of the password can much bigger than the actual password length. So with Joi we can validate the password field so that it can't be greater than for example 10 characters in a login route.

A scenario where mongoose schema validation makes sense:

Let's say the client sent a valid data, it is possible that we forgot to set a property when we create a document. If we hadn't a required: true option in the mongoose schema for that field, the document would be created without that field.

Also validating the client data as soon as possible is good for security and performance before hitting the database.

The only downside of using both is some validation duplication. But it seems they created a package called joigoose to create a mongoose schema from a Joi schema.

SuleymanSah
  • 17,153
  • 5
  • 33
  • 54