dev*_*mat 3 passwords mongoose mongodb node.js joi
我想在用户注册时使用 joi-password-complexity 包来强制密码复杂性。
https://github.com/kamronbatman/joi-password-complexity
我尝试过,但出现以下错误:
(节点:14872)UnhandledPromiseRejectionWarning:断言错误[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;
Run Code Online (Sandbox Code Playgroud)
我也尝试遵循这个示例: https: //forum.codewithmosh.com/d/215-joi-password-complexity-problem,但它似乎已经过时,因为“new”关键字会抛出另一个错误(不是构造函数)。
任何帮助表示赞赏!
无法重现您的确切错误,但我的工作方式如下:
@hapi/joi: ^17.1.0
(撰写本文时为最新版本,也适用于 16.1.8)joi-password-complexity: ^4.0.0
(也是最新的)代码:
function validateUser(user) {
// no change here
const schema = Joi.object({
name: Joi.string().min(1).max(55).required(),
email: Joi.string().min(5).max(255).required().email(),
password: passwordComplexity(complexityOptions)
});
// note that we call schema.validate instead of Joi.validate
// (which doesn't seem to exist anymore)
return schema.validate(user);
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
9699 次 |
最近记录: |