Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Behavior of .concat() with tests redefinition #1973

Closed
eliottvincent opened this issue Jul 19, 2019 · 2 comments
Closed

Behavior of .concat() with tests redefinition #1973

eliottvincent opened this issue Jul 19, 2019 · 2 comments
Assignees
Labels
bug Bug or defect
Milestone

Comments

@eliottvincent
Copy link
Contributor

eliottvincent commented Jul 19, 2019

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.

@eliottvincent
Copy link
Contributor Author

eliottvincent commented Jul 19, 2019

I gave a look at the _tests value of customFields for both schemas.
Schema a:

[ { func: [Function], name: 'min', arg: 1, options: undefined } ]

Schema b:

[ { func: [Function], name: 'min', arg: 1, options: undefined },
  { func: [Function], name: 'min', arg: 0, options: undefined } ]

So .concat() actually did merge correctly the schemas, but is it its behavior to duplicate tests instead of merging them too?

What I would consider as a correct result would be:

[ { func: [Function], name: 'min', arg: 0, options: undefined } ]

@hueniverse
Copy link
Contributor

This will be fixed in v16 (master). v15 and older has some bugs around not considering some min/max operations unique.

@hueniverse hueniverse self-assigned this Jul 19, 2019
@hueniverse hueniverse added the bug Bug or defect label Jul 19, 2019
@hueniverse hueniverse added this to the 16.0.0 milestone Jul 19, 2019
@hueniverse hueniverse added the v16 label Aug 11, 2019
@lock lock bot locked as resolved and limited conversation to collaborators Feb 8, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Bug or defect
Projects
None yet
Development

No branches or pull requests

2 participants