Javascript 使用Sails.js根据param生成所需的属性
我有一个具有两个属性的模型。一个叫userType,一个叫lastName 基本上我想做的是:如果userType是Javascript 使用Sails.js根据param生成所需的属性,javascript,node.js,sails.js,waterline,Javascript,Node.js,Sails.js,Waterline,我有一个具有两个属性的模型。一个叫userType,一个叫lastName 基本上我想做的是:如果userType是professor,那么lastName应该是必需的 userType:{ 键入:“字符串”, 要求:正确, 枚举:[“教授”、“管理员”] }, 姓氏:{ 键入:“字符串”, 必需:函数(){ 返回this.userType==='professor'; } }, 为什么我不能将required作为函数传递?在中,甚至有一个验证包含的示例 如果不可能,是否有其他方法可以做到这一
professor
,那么lastName应该是必需的
userType:{
键入:“字符串”,
要求:正确,
枚举:[“教授”、“管理员”]
},
姓氏:{
键入:“字符串”,
必需:函数(){
返回this.userType==='professor';
}
},
为什么我不能将required作为函数传递?在中,甚至有一个验证包含的示例
如果不可能,是否有其他方法可以做到这一点?我不想在控制器上创建自定义验证,我想把一切都留给模型。甚至可以使用beforeValidate回调
谢谢根据sails文档
您可以自行进行验证,例如:
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
}
}
}
根据sails文件
您可以自行进行验证,例如:
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
}
}
}
仅供参考,此功能已在Sails 1.0中删除。仅供参考,此功能已在Sails 1.0中删除。