How do I specify a field that can be validated but not stored in Waterline?
Question
I'm looking at the example in the Waterline docs here.
var User = Waterline.Collection.extend({
types: {
// snip
password: function(password) {
return password === this.passwordConfirmation;
});
},
attributes: {
// snip
password: {
type: 'string',
password: true
},
passwordConfirmation: {
type: 'string'
}
}
});
Is there a way to tell Waterline that passwordConfirmation
is not part of the schema so that it is not created if migrate is set to alter
or drop
, or if using a schemaless DB engine?
The actual use case I want is for a clear text password
field to validated from the request, but use beforeCreate
to populate a hash
field that is actually stored (but not allowing the password
property to be stored in the process).
Thanks.
Solution
Waterline doesn't support transient fields that are validated but not persisted. You can add the schema: true
property to your model which will have it filter out any attributes that aren't explicitly declared, but still make them available in lifecycle callbacks. You'd have to do the validation for those attributes manually (in beforeCreate()
or beforeValidate()
for example), and you'd lose the ability to add arbitrary fields to schemaless dbs, but it's not necessarily a bad solution.
For your case though, I don't see why it's exactly necessary. Why not just hash the password in beforeCreate
and save it back to password
?
beforeCreate: function (values, cb) {
values.password = hash(values.password);
return cb();
}