Skip to content

Array item could not be an Error #2284

@jurecka

Description

@jurecka

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.

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