jedireza / mongo-models

:package: Map JavaScript classes to MongoDB collections
MIT License
67 stars 28 forks source link

Compatibility with @hapi/joi 16 #41

Closed nickrobillard closed 4 years ago

nickrobillard commented 4 years ago

When using @hapi/joi 16.x.x, the following error occurs when instantiating a model. This can be easily reproduced using the Customer example. Simply use joi 16 instead of 15.

Example:

    const Customer = require('../models/customer');
    let customers;

    try {
      customers = await Customer.create(name, email, phone);
    } catch (err) {
      console.error(err);
    }

Throws error:

Error: Invalid schema content: ($_root.alternatives)
    at new module.exports (/home/me/projects/test/node_modules/mongo-models/node_modules/@hapi/joi/node_modules/@hapi/hoek/lib/error.js:23:19)
    at Object.module.exports [as assert] (/home/me/projects/test/node_modules/mongo-models/node_modules/@hapi/joi/node_modules/@hapi/hoek/lib/assert.js:20:11)
    at Object.exports.schema (/home/me/projects/test/node_modules/mongo-models/node_modules/@hapi/joi/lib/cast.js:50:10)
    at internals.Object.keys (/home/me/projects/test/node_modules/mongo-models/node_modules/@hapi/joi/lib/types/object/index.js:363:35)
    at Object.exports.schema (/home/me/projects/test/node_modules/mongo-models/node_modules/@hapi/joi/lib/cast.js:31:29)
    at internals.Object.keys (/home/me/projects/test/node_modules/mongo-models/node_modules/@hapi/joi/lib/types/object/index.js:363:35)
    at Object.exports.schema (/home/me/projects/test/node_modules/mongo-models/node_modules/@hapi/joi/lib/cast.js:31:29)
    at module.exports.internals.Any.root.compile (/home/me/projects/test/node_modules/mongo-models/node_modules/@hapi/joi/lib/index.js:157:25)
    at module.exports.internals.Any.root.validate (/home/me/projects/test/node_modules/mongo-models/node_modules/@hapi/joi/lib/index.js:143:29)
    at Function.validate (/home/me/projects/test/node_modules/mongo-models/index.js:463:20) {
  path: '$_root.alternatives'
}

Joi validate() method has changed in v16 and the following small changes resolve the issue. Based on some quick tests, this change is also backwards compatible with v15.

jedireza commented 4 years ago

Thanks! The changes look good. The build failed due to a simple linting error. Looks like we need to drop the require statement for joi in that file now since it's not being used.

Lint:
    /home/travis/build/jedireza/mongo-models/index.js:
        Line 4: no-unused-vars - 'Joi' is assigned a value but never used. Allowed unused vars must match /^internals$/.

Let's get that updated and then we can land this PR.

nickrobillard commented 4 years ago

Done!

jedireza commented 4 years ago

Thanks!

jedireza commented 4 years ago

published as mongo-models@3.0.2