猫鼬验证模式中数组的长度
Mongoose validate length of array in schema
我想创建一个模式,其中包含活动参与者姓名的数组,我通过这样做来制作参与者列表:
quizPart:[{
type:String,
}]
我如何验证此数组的长度是零(没有参与者参加此活动)或 2,而不是 1(每个团队活动有两人)。我想要 return 一条我可以用 ValidationError
处理的错误消息
我正在向这个架构中添加数据,如下所示:
var school = new School();
school.quizPart=req.body.quiz;
其中 req.body.quiz = ["name1","name2"]
或 ['','']
然后,如果只有 1 个字段具有字符串值,我想像这样将错误解析到响应正文中:
function handleValidationError(err, body) {
for (field in err.errors) {
switch (err.errors[field].path) {
case "quizPart":
body["quizPartError"] = err.errors[field].message;
break;
}}}
这是我要说的一个工作示例。
写一个 pre('update')
mongoose 钩子并检查 $set
对象 quizParts
字段的长度是否为 0 或 2。
index.js
const mongoose = require('mongoose');
const test = require('./test');
mongoose.connect('mongodb://localhost:27017/test2', {useNewUrlParser: true});
mongoose.set('debug',true);
const db = mongoose.connection;
db.on('error', console.error.bind(console, 'connection error:'));
db.once('open', function() {
// we're connected!
});
(async() => {
try {
const testUpdate = test();
const updateQuery = {
$set: {
quizPart: [
{
type: 'Type 1'
},
{
type: 'Type 2'
}
]
}
};
const updateResult = await testUpdate.update({}, updateQuery).exec();
} catch(err) {
console.error(err);
}
})();
test.js
const mongoose = require('mongoose');
const { Schema } = mongoose;
module.exports = function() {
const testSchema = new Schema({
quizPart: [
{
type: String,
}
]
},
{
collection: 'test',
timestamps: true
});
testSchema.pre('update', function(next) {
const update = this._update.$set;
if (update.length === 0 || update.length === 2) {
return next();
}
else {
return next(new Error('Cannot have length 1!'));
}
});
return mongoose.model('test', testSchema);
};
创建字段:
quizPart:[{
type:String,
}],
然后通过以下方式验证字段:
schoolSchema.path('quizPart').validate((list)=>{
alNumRegex= /^[a-z0-9]+$/i
return list[0]!=="" && list[1]!=="" || alNumRegex.test(list[0]) && alNumRegex.test(list[1]);
},'Please Register atleast two participants for quiz.');
我想创建一个模式,其中包含活动参与者姓名的数组,我通过这样做来制作参与者列表:
quizPart:[{
type:String,
}]
我如何验证此数组的长度是零(没有参与者参加此活动)或 2,而不是 1(每个团队活动有两人)。我想要 return 一条我可以用 ValidationError
我正在向这个架构中添加数据,如下所示:
var school = new School();
school.quizPart=req.body.quiz;
其中 req.body.quiz = ["name1","name2"]
或 ['','']
然后,如果只有 1 个字段具有字符串值,我想像这样将错误解析到响应正文中:
function handleValidationError(err, body) {
for (field in err.errors) {
switch (err.errors[field].path) {
case "quizPart":
body["quizPartError"] = err.errors[field].message;
break;
}}}
这是我要说的一个工作示例。
写一个 pre('update')
mongoose 钩子并检查 $set
对象 quizParts
字段的长度是否为 0 或 2。
index.js
const mongoose = require('mongoose');
const test = require('./test');
mongoose.connect('mongodb://localhost:27017/test2', {useNewUrlParser: true});
mongoose.set('debug',true);
const db = mongoose.connection;
db.on('error', console.error.bind(console, 'connection error:'));
db.once('open', function() {
// we're connected!
});
(async() => {
try {
const testUpdate = test();
const updateQuery = {
$set: {
quizPart: [
{
type: 'Type 1'
},
{
type: 'Type 2'
}
]
}
};
const updateResult = await testUpdate.update({}, updateQuery).exec();
} catch(err) {
console.error(err);
}
})();
test.js
const mongoose = require('mongoose');
const { Schema } = mongoose;
module.exports = function() {
const testSchema = new Schema({
quizPart: [
{
type: String,
}
]
},
{
collection: 'test',
timestamps: true
});
testSchema.pre('update', function(next) {
const update = this._update.$set;
if (update.length === 0 || update.length === 2) {
return next();
}
else {
return next(new Error('Cannot have length 1!'));
}
});
return mongoose.model('test', testSchema);
};
创建字段:
quizPart:[{
type:String,
}],
然后通过以下方式验证字段:
schoolSchema.path('quizPart').validate((list)=>{
alNumRegex= /^[a-z0-9]+$/i
return list[0]!=="" && list[1]!=="" || alNumRegex.test(list[0]) && alNumRegex.test(list[1]);
},'Please Register atleast two participants for quiz.');