Using required:

const courseSchema = new mongoose.Schema({
  name: {type: String, required: true},
  author: {type: String, required: true},
  tags: [String],
  date: { type: Date, default: Date.now },
  isPublished: Boolean,
  price: Number,
});

If we try to save without passing name we will get → UnhandledPromiseRejectionWarning

→ promise can be in 3 state:

we have to handle each state

when saving the result we need to handle the state .

try{
	const result = await course.save();
	console.log(result);
} catch(ex){
	console.log(ex.message);
}

We can manually trigger the validation :

const result = await course.validate();
// it returns the promise 

course.validate((err) => {
if(err){}
});

The validation → required is only meaningful or valid in Mongoose

Mongo db doesn’t care about validation

Built-in validators :

  1. required → it returns boolean