Mongoose嵌套模式验证模型

时间:2018-03-02 07:15:45

标签: node.js mongodb validation mongoose

我有以下Mongoose模型:

var schema = mongoose.Schema({
 webServiceType: {type: String, default: ''}
    , siteMinder: {
            site_url: {type: String, default: ''}
            ,sso_url: {type: String, default: ''}
            ,sso_key: {type: String, default: ''}
            ,ad_group: {type: String, default: ''}  
    }
    , adfs: {
            authorize_url: {type: String, default: ''}
            ,client_id: {type: String, default: ''}
            ,token_url: {type: String, default: ''}
            ,callback_url: {type: String, default: ''}
            ,redirect_url: {type: String, default: ''}
    }
});

我需要检查 webServiceType adfs 还是siteminder。如果值为adfs,我需要验证子对象,例如 authorize_url client_id 等。

我尝试了以下步骤,但收到错误。

schema.path('webServiceType').validate(function(value, next){
    if(value == 'adfs'){
        schema.path('adfs.authorize_url').validate(function(value, respond){
            var validation = value.length != 0;
            respond(validation);
        }, 'Invalid Authorize URL');

        schema.path('adfs.client_id').validate(function(value, respond){
            var validation = value.length != 0;
            respond(validation);
        }, 'Invalid Client ID');

        schema.path('adfs.token_url').validate(function(value, respond){
            var validation = value.length != 0;
            respond(validation);
        }, 'Invalid Token URL ');

        schema.path('adfs.callback_url').validate(function(value, respond){
            var validation = value.length != 0;
            respond(validation);
        }, 'Invalid Callback URL');

        schema.path('adfs.redirect_url').validate(function(value, respond){
            var validation = value.length != 0;
            respond(validation);
        }, 'Invalid ADFS Login Redirect URL ');
    }
    next();
});

提前致谢!

1 个答案:

答案 0 :(得分:0)

如果你需要根据webServiceType得到不同的验证/不同的模式,那么使用鉴别器可能是更好的选择:

const webServiceSchema = new mongoose.Schema({ /* some common part */}, {discriminatorKey: 'webServiceType'});
const WebService = mongoose.model('Event', eventSchema);

const SiteMinder = WebService.discriminator('siteMinder', new Schema({
        site_url: {type: String, default: '', validate: (value) => value.length != 0},
        sso_url: {type: String, default: '', validate: (value) => value.length != 0},
        sso_key: {type: String, default: '', validate: (value) => value.length != 0},
        ad_group: {type: String, default: '', validate: (value) => value.length != 0}  
}));

const Adfs = WebService.discriminator('adfs', new Schema({
    authorize_url: {type: String, default: '', validate: (value) => value.length != 0},
    client_id: {type: String, default: '', validate: (value) => value.length != 0},
    token_url: {type: String, default: '', validate: (value) => value.length != 0},
    callback_url: {type: String, default: '', validate: (value) => value.length != 0},
    redirect_url: {type: String, default: '', validate: (value) => value.length != 0},
}));

您可以在docshere中了解有关歧视者的更多信息。

另请注意,我已将此长度的所有验证移至validate属性。它似乎更好,你可以阅读它here

相关问题