Skip to content

Cannot require a minimal number of matching object properties with pattern #2205

@jandppw

Description

@jandppw

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): no

Context

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

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

const observations = Joi.object()
  .keys({
    '#': Joi.number()
      .integer()
      .positive()
      .required()
  })
  .pattern(/^[^#]+$/, Joi.array(), { matches: Joi.array().min(3) })
  .unknown(false);

expect(observations.validate({'#': 5, a: []}).error).toBeDefined() // as expected: not enough properties
expect(observations.validate({'#': 5, a: [], b: [], c: []}).error).toBeUndefined() // as expected: enough properties

expect(observations.validate({'#': 5}).error).toBeDefined() // does not produce an error (!)

does not work

The matches is not executed if there are no matching keys at all.

This means that it is not possible to require a minimal number of matching properties. In any case, I can't find another way in the pattern documentation.

What was the result you got?

observations.validate({'#': 5} did not fail

What result did you expect?

observations.validate({'#': 5} should fail

Related

Related to the older issue #1457

Suggestions

One way to resolve this might be to pass the empty array to matches, if it exists, and it holds an array schema, if there are no matching properties.

Another way might be to allow an extra option min: 3, or something of the sort, to cover this situation.

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