javascriptexpresslocomotivejs

model validation on server side in locomotivejs


I want to know if there is a function to check if my model is valid on server side ,I am using locomotivejs on express.

here is my model schema

var BillSchema = new Schema({
   BrandId:Schema.Types.ObjectId,
    ModelNo:{ type: String, required: true },
    BillNo:{ type: String, required: true },
    years:Number,
    months:Number,
    })

In my controller , I am receiving values from view.

var bill=new Bill();
  bill.CategoryId=self.param('CategoryId');
  bill.BrandId=self.param("BrandId");
  bill.ModelNo=self.param("Model");
  bill.BillNo=self.param("BillNo");
if(bill.categoryId!="" && bill.BrandId!="" && bill.ModelNo!="" && bill.years!="")
{
// code to save the values to db
}

Is there is a way to avoid this validation

if(bill.categoryId!="" && bill.BrandId!="" && bill.ModelNo!="" && bill.years!="")

with something like ModelState.Isvalid in .Net MVC

Thanks in advance...


Solution

  • guys I got my problem resolved. mongoose provides validate function for this.

    bill.validate(function (err) {
      if(err) {// handle error}
    else
     {// save to db.}
    

    Thanks...