Reputation: 78
I have a model with two attributes. One called userType and one called lastName.
Basically what I want to do is: if userType is professor
then lastName should be required.
userType: {
type: 'string',
required: true,
enum: ['professor', 'administrator']
},
lastName: {
type: 'string',
required: function() {
return this.userType === 'professor';
}
},
Why can't I pass required as a function? In the waterline documentation there is even an example with the validation contains
.
If not possible, does it have any other way to do this? I don't want to create custom validations on the controller, I want to leave everything to the model. Maybe even use the beforeValidate callback.
Thanks
Upvotes: 2
Views: 652
Reputation: 1200
According to sails documentation http://sailsjs.org/documentation/concepts/models-and-orm/validations
You could make your own validations, something like :
module.exports = {
types: {
isProfessor: function(lastName){
return (this.userType === 'professor' && !lastName)? false: true;
}
},
attributes:{
userType: {
type: 'string',
required: true,
enum: ['professor', 'administrator']
},
lastName: {
type: 'string',
isProfessor: true
}
}
}
Upvotes: 4