Skip to content

If all elements of array is required, it does not validate unknown items #2620

@shotasenga

Description

@shotasenga

Support plan

  • is this issue currently blocking your project? (yes/no): no
  • is this issue affecting a production system? (yes/no): yes

Context

  • node version: v14.16.1
  • module version with issue: 17.4.0, 17.3.0
  • last module version without issue: n/a
  • environment (e.g. node, browser, native): Node.js
  • used with (e.g. hapi application, another framework, standalone, ...): standalone
  • any other relevant information: n/a

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

I am writing a validation for an array of strings to accept only defined items.

const schema = Joi.array().items(
  Joi.string().valid('name').required(),
  Joi.string().valid('age').required(),
);

schema.validate(['name', 'age', 'something unknown'])

What was the result you got?

The validation passes.

{ value: [ 'name', 'age', 'something unknown' ] }

What result did you expect?

I am expecting validation fails if there are any unknown items. If any of the items are optional, it works as I expect.

const schema = Joi.array().items(
  Joi.string().valid('name').required(),
  Joi.string().valid('age'),
);

schema.validate(['name', 'age', 'something unknown'])
{
  value: [ 'name', 'age', 'something unknown' ],
  error: [Error [ValidationError]: "[2]" does not match any of the allowed types] {
    _original: [ 'name', 'age', 'something unknown' ],
    details: [ [Object] ]
  }
}

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