在sails.js中过滤用户发布的数据

我想创build/更新控制器中的模型,如下所示:

User.create(postData) 

和postData应该是这样的:

 {name:'Walter Jr', password: '123', gender: 1} 

但是,如果一个恶意用户发布如下内容:

 {name:'Walter Jr', password: '123', gender: 1, anExtraAttribute: 'HAHA'} 

我testing和一个Extratrattribute被保存到数据库。

那么有没有办法来过滤postData,只留下在模型中定义的属性?

是的,你需要在config / models.js下设置schema : true

文件models.js可以像:

 module.exports.models = { /*************************************************************************** * * * Your app's default connection. ie the name of one of your app's * * connections (see `config/connections.js`) * * * ***************************************************************************/ connection : 'localDiskDb', /*************************************************************************** * * * How and whether Sails will attempt to automatically rebuild the * * tables/collections/etc. in your schema. * * * * See http://sailsjs.org/#/documentation/concepts/ORM/model-settings.html * * * ***************************************************************************/ migrate : 'alter', schema : true };