我正在尝试验证猫鼬模式中accountActivatioToken的maxlength字段,这取决于loginType字段,该字段也是同一模式的一部分。
我正在遇到一个错误,因为loginType和accountActivatioToken都正在同时验证并保存在数据库中。
我将如何进行关注?
SCHEMA CONFIG:-
`const userSchema = new mongoose.Schema(
{
loginType: {
...trimReqdMaxltSchema,
maxlength: 6,
default: "google",
validate(value) {
if (!value.match(/(google|email)/g)) {
throw new Error("INVALID USER LOGIN TYPE PROVIDED. ");
}
},
},
accountActivationToken: {
...uniqueReqdMaxltSchema,
// FOLLOWING FIELD NEEDS TO BE VALIDATED BASED ON loginType
maxlength: () => this.loginType === "google" ? 1300 : 300,
required: false,
},
{ timestamps: true }
);
this.loginType无效,因为maxlength始终选择300。
控制器代码片段:-
const userDetails = {
loginType: "google",
accountActivationToken: token,
...
};
const userCredDoc = new UserCredModel(userDetails);
const savedUser = await userCredDoc.save();