【发布时间】:2020-05-26 12:07:21
【问题描述】:
我想在用户注册时使用 joi-password-complexity 包来强制密码复杂性。
https://github.com/kamronbatman/joi-password-complexity
我试过了,但出现以下错误:
(node:14872) UnhandledPromiseRejectionWarning: AssertionError [ERR_ASSERTION]:无效的架构内容: (密码.$_root.alternatives)
这是我正在使用的代码:
const mongoose = require("mongoose");
const Joi = require("joi");
const passwordComplexity = require("joi-password-complexity");
const complexityOptions = {
min: 5,
max: 250,
lowerCase: 1,
upperCase: 1,
numeric: 1,
symbol: 1,
requirementCount: 2,
};
const userSchema = new mongoose.Schema({
name: {
type: String,
minlenght: 1,
maxlength: 55,
required: true
},
email: {
type: String,
minlength: 5,
maxlength: 255,
unique: true,
required: true
},
password: {
type: String,
minlength: 5,
maxlength: 1024,
required: true
}
})
const User = mongoose.model("User", userSchema);
function validateUser(user) {
const schema = {
name: Joi.string().min(1).max(55).required(),
email: Joi.string().min(5).max(255).required().email(),
password: passwordComplexity(complexityOptions) // This is not working
}
return Joi.validate(user, schema);
}
exports.User = User;
exports.validate = validateUser;
我也尝试过这个例子:https://forum.codewithmosh.com/d/215-joi-password-complexity-problem,但它似乎已经过时了,因为“new”关键字会引发另一个错误(不是构造函数)。
感谢任何帮助!
【问题讨论】:
标签: node.js mongodb mongoose passwords joi