Skip to content

Prevent mixing different versions of joi schema types #1913

@kanongil

Description

@kanongil

Context

  • joi version: @hapi/joi@16.0.0-rc2 & joi@14.3.1

What are you trying to achieve or the steps to reproduce ?

Validate old schema:

const Joi = require('@hapi/joi');
const Joi14 = require('joi');

const schema = Joi14.number();

console.log(Joi.validate(1, schema));

Which result you had ?

/Users/gil/test/joi/node_modules/@hapi/joi/lib/cast.js:73
        throw err;
        ^

AssertionError [ERR_ASSERTION]: Cannot add other alternatives without at least one schema(_tests)
    at new AssertionError (internal/assert.js:269:11)
    at Object.exports.assert (/Users/gil/test/joi/node_modules/@hapi/hoek/lib/index.js:592:11)
    at internals.Alternatives.try (/Users/gil/test/joi/node_modules/@hapi/joi/lib/types/alternatives.js:104:14)
    at Object.exports.schema (/Users/gil/test/joi/node_modules/@hapi/joi/lib/cast.js:31:42)
    at internals.Object.keys (/Users/gil/test/joi/node_modules/@hapi/joi/lib/types/object.js:401:35)
    at Object.exports.schema (/Users/gil/test/joi/node_modules/@hapi/joi/lib/cast.js:42:29)
    at Object.internals.appendPath (/Users/gil/test/joi/node_modules/@hapi/joi/lib/cast.js:66:24)
    at Object.exports.schema (/Users/gil/test/joi/node_modules/@hapi/joi/lib/cast.js:15:26)
    at module.exports.internals.Any.root.compile (/Users/gil/test/joi/node_modules/@hapi/joi/lib/index.js:176:21)
    at module.exports.internals.Any.root.validate (/Users/gil/test/joi/node_modules/@hapi/joi/lib/index.js:161:29)

What did you expect ?

Either a successful validation by calling into the old joi logic, or a more descriptive thrown error.

Note that this is the way that Hapi currently validates schemas in routes, causing such an error whenever an old schema is passed.

Metadata

Metadata

Assignees

Labels

breaking changesChange that can breaking existing code

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions