SailsJS:如果它具有未声明的属性,则阻止 Model.create()

SailsJS: Prevent Model.create() if it has undeclared attributes

如果实体包含未在 api/models/YourModel 中声明的属性,是否有方法阻止创建实体?

例如,假设我使用了 MongoDB,并且我有这个 User 型号:

module.exports = {
  attributes: {
    name:{type:'string'},
    age:{type:'number'}
  }
};

我尝试 .create 使用此代码:

User.create({name:'Walter Jr',age:8,missingTest:'something'}).exec(function createCB(err,created){
  console.log('should fail');
});

此外,如果一个 relationship/association 指向另一个模型,如果实体 ID 指向无效实体,创建不应该也会失败吗?

也许我在验证过程中遗漏了一些东西,但到目前为止,这种行为对我来说似乎有点奇怪,而且如果 waterline 有复合主键支持就好了。

你可以试试把schema : true放在config/models.js下

医生说:

A flag to toggle schemaless or schema mode in databases that support schemaless data structures. If turned off, this will allow you to store arbitrary data in a record. If turned on, only attributes defined in the model's attributes object will be stored. For adapters that don't require a schema, such as Mongo or Redis, the default setting is schema:false.

http://sailsjs.org/#!/documentation/concepts/ORM/model-settings.html