有没有好的方法来验证file
字段,只允许以".gci"结尾的字符串?我不确定是否可以直接在模式中这样做,或者是否应该在其他地方处理它.
schema.js:
const schema = new mongoose.Schema({
file: {
type: String,
required: [true, "Please provide a .gci file"],
},
});
有没有好的方法来验证file
字段,只允许以".gci"结尾的字符串?我不确定是否可以直接在模式中这样做,或者是否应该在其他地方处理它.
schema.js:
const schema = new mongoose.Schema({
file: {
type: String,
required: [true, "Please provide a .gci file"],
},
});
你可以像这样使用custom validator:
const schema = new mongoose.Schema({
file: {
type: String,
required: [true, "Please provide a .gci file"],
validate: {
validator: function(v) {
if (!value.endsWith(".gci")) {
return false;
}
},
message: props => `${props.value} doesn't end with .gci extension!`
}
});