猫鼬自定义验证课程的先决条件?

时间:2019-11-04 17:56:38

标签: mongodb mongoose

我有以下CourseSchema,

我需要通过以下条件来验证先决条件字段:

-它必须为空,

-或长度为3,并且与现有课程ID匹配

我该如何处理?我需要在这里连接到MongoDB吗? 预先感谢!

let courseSchema = new mongoose.Schema({
    courseID: {
        type: String, 
        minlength: 3, 
        maxlength: 3, 
        required: [true, "missing courseID"],
        unique: true
    }, 
    courseName: {type: String, required: [true, "missing courseName"]},
    prerequisites: {
        type: String,
        validate:{
            validator: function(v){
                if(v.length === 0){
                    return true;
                }
                else if(v.length === 3){
                    //check if the prerequisite course exist in the DB
                        //if not return false
                        //else return true
                }
            },
            message: "prerequisites must either be empty or (have length= 3 and exist)"
        }
    }
})

0 个答案:

没有答案