我有一个验证函数,用于检查模型中的内容长度。
new HtmlWebpackPlugin({
// ... your other options ...
// Ensure asynchronous chunks are injected into <head>
inject: 'head',
// Ensure chunks are evaluated in correct order
chunksSortMode: 'dependency'
})
所以,我把它作为验证器字段放在模型架构上,就像这样......
let contentLengthchecker = (content) => {
if(!content){
return false;
}else{
if(content.length < 5 || content.length > 100){
return false;
}else{
return true;
}
}
};
并将该数组放在模型的验证器字段中。
const titleValidators = [
{validator: contentLengthchecker, message: 'Title length must be between 6 to 30 characters'}
];
所以,现在我想让这个功能有点可重复使用。所以,我正在写这个
const adSchema = new Schema({
title: {type: String, required: true, validate: titleValidators},
description: {type: String, required: true, validate: descriptionValidators}
});
但是,现在我正努力将minLength和maxLenght参数传递给它。我不知道将它们放在模型架构中的哪个位置。
那么,在哪里放置其他参数?
答案 0 :(得分:0)
const titleValidators = [{
validator: contentLengthchecker,
message: 'Title length must be between 6 to 30 characters',
minLength: 6,
maxLength: 30
}];
let contentLengthchecker = (content) => {
if(!content){
return false;
}else{
if(!this.minLength || !this.maxLength){
throw 'minLength or maxLength is not specified';
}
if(content.length < this.minLength || content.length > this.maxLength){
return false;
}else{
return true;
}
}
我认为这应该有用。