Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Verifying schema on Meteor Method using autoform

I'm using autoform, collection2. I want to use method call type for insert/update, as I want to add additional fields before saving to database in server. SimpleSchema would check the data in client, but how can I make the data is checked against the schema on server-side as well? My method for adding new data is as follows:

Meteor.methods({
  companyAdd: function (companyAttr) {

    // add additional fields to document

    var currentDate = new Date(); 

    var company = _.extend(companyAttr, {
        createdBy: user._id,
        createdAt: currentDate
    });

    var newCompanyId = Companies.insert(company);
    return {_id: newCompanyId};
  }
}
like image 868
Ahmet Cetin Avatar asked Mar 10 '15 13:03

Ahmet Cetin


1 Answers

I found in documentation of simpleschema, if anyone else would need solution later on: you can just check against schema:

Meteor.methods({
   companyAdd: function (companyAttr) {

   //here we check the data sent to method against the defined schema
   check(companyAttr, Companies.simpleSchema());

   var currentDate = new Date(); 

   var company = _.extend(companyAttr, {
      createdBy: user._id,
      createdAt: currentDate
   });

   var newCompanyId = Companies.insert(company);
   return {_id: newCompanyId};
  }
}
like image 58
Ahmet Cetin Avatar answered Nov 18 '22 14:11

Ahmet Cetin