Skip to content

Bad behavior of Joi.object().concat() #534

@briansorahan

Description

@briansorahan

I looked through the issues (both open and closed) and could not find one describing this behavior. Here is the script to reproduce:

var Joi = require('joi');

var a = Joi.object().keys({
  foo: Joi.string()
});

var b = Joi.object().keys({
  foo: Joi.number()
});

var ab = a.concat(b);

console.log(Joi.validate({ foo: 'bar' }, ab));
console.log(Joi.validate({ foo: 1 }, ab));

I get:

{ error: 
   { [ValidationError: foo must be a number]
     name: 'ValidationError',
     details: [ [Object] ],
     _object: { foo: 'bar' },
     annotate: [Function] },
  value: { foo: 'bar' } }
{ error: 
   { [ValidationError: foo must be a string]
     name: 'ValidationError',
     details: [ [Object] ],
     _object: { foo: 1 },
     annotate: [Function] },
  value: { foo: 1 } }

I know this use case seems a bit stupid, but it might be good to be more defensive.

This issue is present in joi 5.1.0 installed with npm.

Metadata

Metadata

Assignees

Labels

breaking changesChange that can breaking existing codebugBug or defect

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions