Alon Amir
Alon Amir

Reputation: 5015

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

Is there a way to prevent an entity creation if it contains an attribute that is not declared in api/models/YourModel?

For example, let's say I used MongoDB, and I have this User model:

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

and I try to .create with this code:

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

Also, if a have a relationship/association to another model, shouldn't creation fail as well if the entity id points to an invalid entity?

Maybe I'm missing something in the validation process, but so far the behaviour seems a bit strange to me, also it would be nice if waterline had composite primary key support.

Upvotes: 1

Views: 626

Answers (1)

jaumard
jaumard

Reputation: 8292

You can try to put schema : true under config/models.js

Doc say :

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

Upvotes: 3

Related Questions