文章来源于网络收集而来,版权归原创者所有,如有侵权请及时联系!
ajv-bsontype
添加 mongodb 的 bsonType 格式验证器
安装
npm install ajv-bsontype --save
设置
var Ajv = require('ajv');
var ajv = new Ajv;
require('ajv-bsontype')(ajv);
使用
const schema = {
required: [ "name", "year", "major", "gpa" ],
properties: {
name: {
bsonType: "string",
description: "must be a string and is required"
},
gender: {
bsonType: "string",
description: "must be a string and is not required"
},
year: {
bsonType: "int",
description: "must be an integer in [ 2017, 3017 ] and is required"
},
major: {
enum: [ "Math", "English", "Computer Science", "History", null ],
description: "can only be one of the enum values and is required"
},
gpa: {
bsonType: [ "double" ],
description: "must be a double and is required"
}
}
}
const data = {
name: "Alice",
year: 2019,
major: "History",
gpa: 3
}
ajv.validate(schema, data)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论