yoitsro / joigoose

Joi validation for your Mongoose models without the hassle of maintaining two schemas
MIT License
177 stars 19 forks source link

Using the example provided throws error: Schema can only contain plain objects #35

Closed bradkyle closed 4 years ago

bradkyle commented 4 years ago
var joiUserSchema = Joi.object({
  name: Joi.object({
    first: Joi.string().required(),
    last: Joi.string().required()
  }),
  email: Joi.string()
    .email()
    .required(),
  bestFriend: Joi.string().meta({
    _mongoose: { type: "ObjectId", ref: "User" }
  }),
  metaInfo: Joi.any(),
  addresses: Joi.array()
    .items({
      line1: Joi.string().required(),
      line2: Joi.string()
    })
    .meta({ _id: false, timestamps: true })
});
var mongooseUserSchema = new Mongoose.Schema(Joigoose.convert(joiUserSchema));

throws:

    Schema can only contain plain objects

      51 |   });
      52 |
    > 53 | var mongooseUserSchema = new Mongoose.Schema(Joigoose.convert(joiUserSchema));
         |                                                       ^
      54 | User = Mongoose.model("User", mongooseUserSchema);
      55 |
      56 | /*

      at new Object.<anonymous>.module.exports (node_modules/@hapi/hoek/lib/error.js:23:19)
      at Object.<anonymous>.module.exports (node_modules/@hapi/hoek/lib/assert.js:20:11)
      at Object.<anonymous>.internals.schema (node_modules/@hapi/joi/lib/compile.js:88:5)
      at Object.<anonymous>.exports.schema (node_modules/@hapi/joi/lib/compile.js:17:26)
      at Object.<anonymous>.internals.Base.$_compile (node_modules/@hapi/joi/lib/base.js:

OS: Arch Linux npm show joigoose:

joigoose@6.2.0 | MIT | deps: 2 | versions: 34
Joi validation for your Mongoose models without the hassle of maintaining two schemas
https://github.com/yoitsro/joigoose

keywords: joi, mongoose, validation, schema, model, hapi

dist
.tarball: https://registry.npmjs.org/joigoose/-/joigoose-6.2.0.tgz
.shasum: 0d6e4a55da72c4a0531153354c830a6ebbf9df39
.integrity: sha512-amnfYhUqW6fOgRVOwv5a3GKTl4gombFmZlEQpfL3hYnAKbNZ0Dg4T6kadr/y8b8hMWjczHHqOBSUO4nMba30bg==
.unpackedSize: 16.3 kB

dependencies:
@hapi/hoek: ^8.5.0 @hapi/joi: ^16.1.7

maintainers:
- yoitsro <ro@mallzee.com>

dist-tags:
latest: 6.2.0

published 3 weeks ago by yoitsro <npm@designbyro.com>

npm show mongoose:


mongoose@5.8.4 | MIT | deps: 11 | versions: 564
Mongoose MongoDB ODM
https://mongoosejs.com

keywords: mongodb, document, model, schema, database, odm, data, datastore, query, nosql, orm, db

dist
.tarball: https://registry.npmjs.org/mongoose/-/mongoose-5.8.4.tgz
.shasum: bd35697b2f800bb919c142b1443630fbab382a35
.integrity: sha512-jQjLckUILEQUqBuG+ihjtA9OLmrqcIG5n+vaeHpR++TG8/ug5yy5ogkDnybTSq8Ql5OORud3+OCOc2Uw96q32w==
.unpackedSize: 1.8 MB

dependencies:
bson: ~1.1.1                     mongoose-legacy-pluralize: 1.0.2 ms: 2.1.2                        sift: 7.0.1
kareem: 2.3.1                    mpath: 0.6.0                     regexp-clone: 1.0.0              sliced: 1.0.1
mongodb: 3.4.1                   mquery: 3.2.2                    safe-buffer: 5.1

npm show joi


joi@14.3.1 | BSD-3-Clause | deps: 3 | versions: 200
Object schema validation
https://github.com/hapijs/joi

DEPRECATED ⚠️  - This module has moved and is now available at @hapi/joi. Please update your dependencies as this version is no longer maintained an may contain bugs and security issues.

keywords: hapi, schema, validation

dist
.tarball: https://registry.npmjs.org/joi/-/joi-14.3.1.tgz
.shasum: 164a262ec0b855466e0c35eea2a885ae8b6c703c
.integrity: sha512-LQDdM+pkOrpAn4Lp+neNIFV3axv1Vna3j38bisbQhETPMANYRbFJFUyOZcOClYvM/hppMhGWuKSFEK9vjrB+bQ==
.unpackedSize: 192.9 kB

dependencies:
hoek: 6.x.x    isemail: 3.x.x topo: 3.x.x

maintainers:
- hueniverse <eran@hammer.io>

dist-tags:
9-beta: 9.0.0-9   latest: 14.3.1    node-0.x: 6.10.1

published a year 
yoitsro commented 4 years ago

Hey @bradkyle. Please can you try upgrading joi to at least v16: https://www.npmjs.com/package/@hapi/joi then trying again?

yoitsro commented 4 years ago

@bradkyle Any news on this?

guilhermesilvame commented 4 years ago

joigoose^6.2.0 is not working with @hapi/joi^17.1.0

Error: Cannot mix different versions of joi schemas at Object. (.\index.js:11:43)

Source:

const Mongoose = require("mongoose")
const Schema = Mongoose.Schema
const Joigoose = require("joigoose")(Mongoose)
const Joi = require("@hapi/joi")
const joiProductSchema = Joi.object({
    model: Joi.string().alphanum().min(2).max(255).trim().required(),
    brand: Joi.string().alphanum().min(2).max(255).trim().required(),
    price: Joi.number().positive(),
    photo: Joi.string().max(255).trim()
})
const ProductSchema = new Schema(Joigoose.convert(joiProductSchema))
yoitsro commented 4 years ago

:tada: This issue has been resolved in version 7.0.0 :tada:

The release is available on:

Your semantic-release bot :package::rocket:

xark-10 commented 3 years ago

var joiSchema= Joi.object({ username: Joi.string().email({ minDomainSegments: 2, tlds: { allow: ['com', 'net'] } }), email: Joi.string().email({ minDomainSegments: 2, tlds: { allow: ['com', 'net'] } }), password: Joi.string().required(), verifyPassword: Joi.string().required(), firstName: Joi.string().required(), lastName: Joi.string().required(),

})

var userSchema = new mongoose.Schema( Joigoose.convert(joiSchema) ); console.log(userSchema) userSchema.pre('save', function (next) { var user = this if (this.isModified('password') || this.isNew) { bcrypt.genSalt(saltValue, function (err, salt) { if (err) { return next(err) } bcrypt.hash(user.password, salt, function (err, hash) { if (err) { return next(err) } user.password = hash next() }) }) } else { return next() //logger.error('error', User Model - Returning User) } })

module.exports = mongoose.model('User', userSchema)

error : Error: Schema can only contain plain objects

yoitsro commented 3 years ago

Hi @xark-10. Thanks for reporting this. Please can you open new issue for this as this issue is quite old now.