How to Progamatically Validate a Mongoose Model Instance

When using built-in validations in a mongoose model, it's possible to trigger a model validation.

For example if you received user inputs, and created a new instance of your model, it's possible to make sure the models fits your needs.

It's pretty convenient if you want to make sure that what you are going to insert fits your requirements without actually querying the database server.

You would do it like this:

let user = new User({username:"jack", age:38});
await user.validate();

Sources from the mongoose package documentation: