在 sails.js 中过滤用户 post 数据

Filter user post data in sails.js

我想 create/update 控制器中的模型为:

User.create(postData)

和post数据应该是这样的:

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

但是如果恶意用户 post 是这样的:

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

我测试了一个ExtraAttribute被保存到数据库中。

那么有没有办法过滤post数据并只保留模型中定义的属性?

是的,您需要在 config/models.js

下设置 schema : true

文件 models.js 可以是:

module.exports.models = {

  /***************************************************************************
  *                                                                          *
  * Your app's default connection. i.e. 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

};