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

Array item could not be an Error #2284

Closed
jurecka opened this issue Jan 27, 2020 · 2 comments
Closed

Array item could not be an Error #2284

jurecka opened this issue Jan 27, 2020 · 2 comments
Assignees
Labels
breaking changes Change that can breaking existing code bug Bug or defect
Milestone

Comments

@jurecka
Copy link

jurecka commented Jan 27, 2020

Support plan

  • which support plan is this issue covered by? (e.g. Community, Core, Plus, or Enterprise): Community
  • is this issue currently blocking your project? (yes/no): no
  • is this issue affecting a production system? (yes/no): yes

Context

  • node version: 12.14.1
  • module version with issue: 16.1.1
  • last module version without issue: 16.0.1
  • environment (e.g. node, browser, native): node
  • used with (e.g. hapi application, another framework, standalone, ...): standalone
  • any other relevant information:

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

I am trying validate that an array contains an Error.

/** run the next tests by ./node_modules/jest/bin/jest.js joi.test.js */
const Joi = require('@hapi/joi');

const ErrorSchema = Joi.object().instance(Error)
const CustomErrorSchema = Joi
  .any()
  .custom((error) => {
    if (error instanceof Error) {
      return true;
    }
    throw new Error('Error validation fail');
  })
  .raw(true);
const createArraySchema = (...items) => Joi.array().items(...items);

it('1# should pass validation of Error schema', () => {
  expect(ErrorSchema.validate(new Error('test error')))
    .toEqual({
      value: new Error('test error')
    });
});

it('2# should pass validation of Error schema in array', () => {
  expect(createArraySchema(ErrorSchema).validate([new Error('test error')]))
    .toEqual({
      value: [new Error('test error')]
    });
});

it('3# should pass validation of Error schema in array (workaround)', () => {
  expect(createArraySchema(CustomErrorSchema.raw(false))
    .raw(true)
    .validate([new Error('test error')])
  )
    .toEqual({
      value: [new Error('test error')]
    });
});

What was the result you got?

Test 2# fails due to validation always fails even though the Error is valid item of the array.

What result did you expect?

Array item could be an Error.

I see the #2118 where this issue was introduced. Due to missing description there I created this issue.

@jurecka jurecka added the support Questions, discussions, and general support label Jan 27, 2020
@tmartin1
Copy link

Ran into the same problem. I believe the ret[0] instanceof Error is the culprit here: https://github.com/hapijs/joi/blob/v16/lib/validator.js#L419

The way it is now, if there is an Error at index zero, even if it is supposed to be there, the validation fails.

@hueniverse hueniverse added bug Bug or defect and removed support Questions, discussions, and general support labels Jul 5, 2020
@hueniverse hueniverse self-assigned this Jul 5, 2020
@hueniverse hueniverse added this to the 17.1.2 milestone Jul 5, 2020
@hueniverse hueniverse added the breaking changes Change that can breaking existing code label Jul 6, 2020
@hueniverse
Copy link
Contributor

I'm marking this as a breaking change (released as a non-breaking version) because it is possible someone has written an extension that uses this bug to send multiple errors from a custom rule using an array. If that's the case, this will break that because it now requires a special array generated by helpers.errorsArray(). Since this wasn't documented and was a bug, I am releasing this as a non-breaking release.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
breaking changes Change that can breaking existing code bug Bug or defect
Projects
None yet
Development

No branches or pull requests

3 participants