Skip to content

Behavior of .concat() with tests redefinition #1973

@eliottvincent

Description

@eliottvincent

Context

  • node version: 8.9.4
  • joi version: 15.0.3
  • environment (node, browser): node
  • used with (hapi, standalone, ...): standalone
  • os: Ubuntu 18.04.2 LTS

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

Hi,

I'm used to have several versions of my validation schemas, each new version being based on the previous one. In the bellow example, I have schema a and schema b, b being based on a. I use the .concat() to achieve that.
In my example, I redefine the test .min() by changing the limit value from 1 (schema a) to 0 (schema b).

const a = joi.object({
  customFields: joi.object().min(1),
});

const b = a.concat(
  joi.object({
    customFields: joi.object().min(0),
  }),
);

// tests against schema a
// 1. should fail
joi.validate({ customFields: { } }, a, (err) => {
  if (err) {
    console.info('OK: test 1 should fail and it did.');
    // console.error(err);
  } else {
    console.info('NOT OK: test 1 should fail but it did not.');
  }
});
// 2. should pass
joi.validate({ customFields: { hello: 'hi' } }, a, (err) => {
  if (err) {
    console.info('NOT OK: test 2 should pass but it did not.');
    // console.error(err);
  } else {
    console.info('OK: test 2 should pass and it did.');
  }
});

// tests against schema b
// 3. should pass
joi.validate({ customFields: { } }, b, (err) => {
  if (err) {
    console.info('NOT OK: test 3 should pass but it did not.');
    console.error(err);
  } else {
    console.info('OK: test 3 should pass and it did.');
  }
});

Which result you had ?

OK: test 1 should fail and it did.
OK: test 2 should pass and it did.
NOT OK: test 3 should pass but it did not.
{ ValidationError: child "customFields" fails because ["customFields" must have at least 1 children]
    at Object.exports.process (/home/hidden_path/node_modules/@hapi/joi/lib/errors.js:202:19)
    at internals.Object._validateWithOptions (/home/hidden_path/node_modules/@hapi/joi/lib/types/any/index.js:762:31)
    at module.exports.internals.Any.root.validate (/home/hidden_path/node_modules/@hapi/joi/lib/index.js:145:23)
    at Object.<anonymous> (/home/hidden_path/joitest.js:44:5)
    at Module._compile (module.js:643:30)
    at Object.Module._extensions..js (module.js:654:10)
    at Module.load (module.js:556:32)
    at tryModuleLoad (module.js:499:12)
    at Function.Module._load (module.js:491:3)
    at Function.Module.runMain (module.js:684:10)
  isJoi: true,
  name: 'ValidationError',
  details: 
   [ { message: '"customFields" must have at least 1 children',
       path: [Array],
       type: 'object.min',
       context: [Object] } ],
  _object: { customFields: {} },
  annotate: [Function] }

What did you expect ?

I expected the .min() test to be correctly redefined and test 3 to pass.

Metadata

Metadata

Assignees

Labels

bugBug or defect

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions