MongoDB系列之mongoose验证


在创建集合规则时,可以设置当前字段的验证规则,验证失败则输入插入失败。

1、必填

required: true必传字段

const postSchema = new mongoose.Schema({
    
    
	title: {
    
    
		type: String,
		// 必选字段
		required: true
	}
});
Post.create({
    
    })
	.then(result => console.log(result))
	.catch(error => {
    
    
		console.log(error);
	});

在这里插入图片描述
如果要实现自定义的错误信息,需要将true放入一个数组中,数组的第二个数为错误的信息。

const postSchema = new mongoose.Schema({
    
    
	title: {
    
    
		type: String,
		// 必选字段
		required: [true, "请传入文章标题"]
	}
});
Post.create({
    
    })
	.then(result => console.log(result))
	.catch(error => {
    
    
		console.log(error);
	});

在这里插入图片描述

2、最小长度

minlength: 3 字符串最小长度

const postSchema = new mongoose.Schema({
    
    
	title: {
    
    
		type: String,
		// 必选字段
		required: [true, "请传入文章标题"],
		// 字符串的最小长度
		minlength: [2, "文章长度不能小于2"]
	}
});
Post.create({
    
     title: "w" })
	.then(result => console.log(result))
	.catch(error => {
    
    
		console.log(error);
	});

在这里插入图片描述

3、最大长度

maxlength: 20 字符串最大长度

const postSchema = new mongoose.Schema({
    
    
	title: {
    
    
		type: String,
		// 必选字段
		required: [true, "请传入文章标题"],
		// 字符串的最小长度
		minlength: [2, "文章长度不能小于2"],
		// 字符串的最大长度
		maxlength: [5, "文章长度最大不能超过5"]
	}
});
Post.create({
    
     title: "wenzhang" })
	.then(result => console.log(result))
	.catch(error => {
    
    
		console.log(error);
	});

在这里插入图片描述

4、去前后的空格

trim: true 去除字符串两边的空格

扫描二维码关注公众号,回复: 14533131 查看本文章
const postSchema = new mongoose.Schema({
    
    
	title: {
    
    
		type: String,
		// 必选字段
		required: [true, "请传入文章标题"],
		// 字符串的最小长度
		minlength: [2, "文章长度不能小于2"],
		// 字符串的最大长度
		maxlength: [5, "文章长度最大不能超过5"],
		// 去除字符串两边的空格
		trim: true
	}
});
Post.create({
    
     title: "   zhang      " })
	.then(result => console.log(result))
	.catch(error => {
    
    
		console.log(error);
	});

在这里插入图片描述

5、数值最小值

min: 2 数值最小为2

const postSchema = new mongoose.Schema({
    
    
	title: {
    
    
		type: String,
		// 必选字段
		required: [true, "请传入文章标题"],
		// 字符串的最小长度
		minlength: [2, "文章长度不能小于2"],
		// 字符串的最大长度
		maxlength: [5, "文章长度最大不能超过5"],
		// 去除字符串两边的空格
		trim: true
	},
	age: {
    
    
		type: Number,
		// 数字的最小范围
		min: 18
	}
});
Post.create({
    
     title: "   zhang      ", age: 10 })
	.then(result => console.log(result))
	.catch(error => {
    
    
		console.log(error);
	});

在这里插入图片描述

6、数值最大值

max: 100 数值最大为100

const postSchema = new mongoose.Schema({
    
    
	title: {
    
    
		type: String,
		// 必选字段
		required: [true, "请传入文章标题"],
		// 字符串的最小长度
		minlength: [2, "文章长度不能小于2"],
		// 字符串的最大长度
		maxlength: [5, "文章长度最大不能超过5"],
		// 去除字符串两边的空格
		trim: true
	},
	age: {
    
    
		type: Number,
		// 数字的最小范围
		min: 18,
		// 数字的最大范围
		max: 100
	}
});
Post.create({
    
     title: "   zhang      ", age: 102 })
	.then(result => console.log(result))
	.catch(error => {
    
    
		console.log(error);
	});

在这里插入图片描述

7、默认值

default: 默认值

const postSchema = new mongoose.Schema({
    
    
	title: {
    
    
		type: String,
		// 必选字段
		required: [true, "请传入文章标题"],
		// 字符串的最小长度
		minlength: [2, "文章长度不能小于2"],
		// 字符串的最大长度
		maxlength: [5, "文章长度最大不能超过5"],
		// 去除字符串两边的空格
		trim: true
	},
	age: {
    
    
		type: Number,
		// 数字的最小范围
		min: 18,
		// 数字的最大范围
		max: 100
	},
	publishDate: {
    
    
		type: Date,
		// 默认值
		default: Date.now
	}
});
Post.create({
    
     title: "   zhang      ", age: 100 })
	.then(result => console.log(result))
	.catch(error => {
    
    
		console.log(error);
	});

在这里插入图片描述

8、枚举

enum: ["html", "css", "javascript", "node.js"]

const postSchema = new mongoose.Schema({
    
    
	title: {
    
    
		type: String,
		// 必选字段
		required: [true, "请传入文章标题"],
		// 字符串的最小长度
		minlength: [2, "文章长度不能小于2"],
		// 字符串的最大长度
		maxlength: [5, "文章长度最大不能超过5"],
		// 去除字符串两边的空格
		trim: true
	},
	age: {
    
    
		type: Number,
		// 数字的最小范围
		min: 18,
		// 数字的最大范围
		max: 100
	},
	publishDate: {
    
    
		type: Date,
		// 默认值
		default: Date.now
	},
	category: {
    
    
		type: String,
		// 枚举 列举出当前字段可以拥有的值
		enum: ["html", "css", "javascript", "node.js"]
	}
});
Post.create({
    
     title: "   zhang      ", age: 100, category: "java" })
	.then(result => console.log(result))
	.catch(error => {
    
    
		console.log(error);
	});

在这里插入图片描述
如果需要自定义错误信息 ,只需要按如下代码书写即可

const postSchema = new mongoose.Schema({
    
    
	title: {
    
    
		type: String,
		// 必选字段
		required: [true, "请传入文章标题"],
		// 字符串的最小长度
		minlength: [2, "文章长度不能小于2"],
		// 字符串的最大长度
		maxlength: [5, "文章长度最大不能超过5"],
		// 去除字符串两边的空格
		trim: true
	},
	age: {
    
    
		type: Number,
		// 数字的最小范围
		min: 18,
		// 数字的最大范围
		max: 100
	},
	publishDate: {
    
    
		type: Date,
		// 默认值
		default: Date.now
	},
	category: {
    
    
		type: String,
		// 枚举 列举出当前字段可以拥有的值
		enum: {
    
    
			values: ["html", "css", "javascript", "node.js"],
			message: "分类名称要在一定的范围内才可以"
		}
	}
});
Post.create({
    
     title: "   zhang      ", age: 100, category: "java" })
	.then(result => console.log(result))
	.catch(error => {
    
    
		console.log(error);
	});

在这里插入图片描述

9、自定义验证规则

const postSchema = new mongoose.Schema({
    
    
	title: {
    
    
		type: String,
		// 必选字段
		required: [true, "请传入文章标题"],
		// 字符串的最小长度
		minlength: [2, "文章长度不能小于2"],
		// 字符串的最大长度
		maxlength: [5, "文章长度最大不能超过5"],
		// 去除字符串两边的空格
		trim: true
	},
	age: {
    
    
		type: Number,
		// 数字的最小范围
		min: 18,
		// 数字的最大范围
		max: 100
	},
	publishDate: {
    
    
		type: Date,
		// 默认值
		default: Date.now
	},
	category: {
    
    
		type: String,
		// 枚举 列举出当前字段可以拥有的值
		enum: {
    
    
			values: ["html", "css", "javascript", "node.js"],
			message: "分类名称要在一定的范围内才可以"
		}
	},
	author: {
    
    
		type: String,
		validate: {
    
    
			validator: v => {
    
    
				// 返回布尔值
				// true 验证成功
				// false 验证失败
				// v 要验证的值
				return v && v.length > 4;
			},
			// 自定义错误信息
			message: "传入的值不符合验证规则"
		}
	}
});
Post.create({
    
     title: "   zhang      ", age: 100, category: "css", author: "bd" })
	.then(result => console.log(result))
	.catch(error => {
    
    
		console.log(error);
	});

在这里插入图片描述

10、获取错误信息

通过error.errors['字段名称'].message

Post.create({
    
     title: "aa", age: 60, category: "java", author: "bd" })
	.then(result => console.log(result))
	.catch(error => {
    
    
		// 获取错误信息对象
		const err = error.errors;
		// 循环错误信息对象
		for (var attr in err) {
    
    
			// 将错误信息打印到控制台中
			console.log(err[attr]["message"]);
		}
	});

在这里插入图片描述

写在最后

如果你感觉文章不咋地//(ㄒoㄒ)//,就在评论处留言,作者继续改进;o_O???
如果你觉得该文章有一点点用处,可以给作者点个赞;\\*^o^*//
如果你想要和作者一起进步,可以微信扫描二维码,关注前端老L~~~///(^v^)\\\~~~
谢谢各位读者们啦(^_^)∠※!!!

猜你喜欢

转载自blog.csdn.net/weixin_62277266/article/details/127217862