Search code examples
javascriptmeteormeteor-autoformmeteor-collection2

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};
  }
}

Solution

  • 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};
      }
    }