简体   繁体   English

在节点js中使用joigoose和mongoose进行验证的模式

[英]Schema Using joigoose and mongoose for validation in node js

I am using @hapi/joi for validation in mongoose schema.我正在使用@hapi/joi在 mongoose 模式中进行验证。 I am converting joi object in mongoose schema using joigoose as follows :我正在使用joigoose转换joigoose模式中的joigoose对象,如下所示:

import mongoose from 'mongoose';
import joi from '@hapi/joi';
const joigoose = require('joigoose')(mongoose);
let objectId = mongoose.Schema.Types.ObjectId;

let userInfo = joi.object().keys({
    userId: joi.string().required().alphanum().meta({ type: 'objectId' }),
    firstName: joi.string().required().min(4).max(20),
    lastName: joi.string(),
    birthDate: joi.date(),
    email: joi.string().email(),
    mobile: joi.number().max(10),
    isActive: joi.boolean()
})

let userInfoSchema = new mongoose.Schema(joigoose.convert(userInfo));
module.exports = mongoose.model('userInfoModel', userInfoSchema, 'userInfo');

This is throwing error as follows:这是抛出错误如下:

在此处输入图片说明

I am not able to figure out what is the problem.我无法弄清楚是什么问题。

UPDATE : Joigoose has fixed this error in 7.0.0 version: https://github.com/yoitsro/joigoose/issues/36更新:Joigoose 在 7.0.0 版本中修复了这个错误: https : //github.com/yoitsro/joigoose/issues/36

Run the command below to list all dependencies of packages in your project's folder运行下面的命令以列出项目文件夹中包的所有依赖项

npm list

Find in the tree the joigoose and its dependencies:在树中找到 joigoose 及其依赖项:

+-- joigoose@6.2.0
| +-- @hapi/hoek@8.5.1 deduped
| `-- @hapi/joi@16.1.8 deduped

Install the same version of @hapi/joi, by doing:通过执行以下操作安装相同版本的@hapi/joi:

npm install @hapi/joi@16.1.8

Unfortunately, until now, the latest version of joigoose is not compatible with @hapi/joi 17.x version.不幸的是,到目前为止,最新版本的 joigoose 与@hapi/joi 17.x 版本不兼容。

Link: https://github.com/yoitsro/joigoose/issues链接: https : //github.com/yoitsro/joigoose/issues

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM