我想重用一些验证规则(登录/注册)。但是,如果我将验证分配给用户模式,jugglingdb 也会将这些验证添加到 BaseUser(这不是我想要的,我不知道这是怎么发生的)
// Provide user properties to re-use for login validation
var common_properties = {
email : String,
password : String
};
var common_validations = [
['email', {validation: 'presence'}],
['email', {validation: 'format', with: /@.*?\./}],
['password', {validation: 'presence'}],
['password', {validation: 'length', min: 6}]
];
var BaseUser = schema.define('BaseUser', common_properties);
BaseUser._validations = common_validations;
var User = schema.define('User', _.extend(common_properties, {
name : String,
salt : String,
created : { type: Date, default: Date.now }
}));
User._validations = common_validations;
User.validatesPresenceOf('name');
User.validatesUniquenessOf('email');
console.log(BaseUser._validations)
// [ [ 'email', { validation: 'presence' } ],
// [ 'email', { validation: 'format', with: /@.*?\./ } ],
// [ 'password', { validation: 'presence' } ],
// [ 'password', { validation: 'length', min: 6 } ],
// [ 'name', { validation: 'presence' }, undefined ], Should not be here??
// [ 'email', { validation: 'uniqueness' }, { async: true } ] ] Should not be here??
有谁知道 javascript / jugglingdb 是如何做到这一点的?我只想将 common_validations 分配给 BaseUser,仅此而已。