Skip to content

unknown(false) is not work when configuration "allowUnknown" is true #2824

Open
@LuEason

Description

Support plan

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

Context

  • node version: 14.20.0
  • module version: 17.6.0
  • environment (e.g. node, browser, native): node
  • used with (e.g. hapi application, another framework, standalone, ...): node
  • any other relevant information:

How can we help?

Our project use version 14 before, we want to allow all fields of all object except for some fields we don't expect
For example, we want to allow field "e" of data.d only, and allow other unknown fields for data.b and even data, we use below code:

const joi = require('joi');

const schema = joi.object().keys({
  a: joi.string(),
  b: joi.object().keys({
    c: joi.string()
  }),
  d: joi.object().keys({
    e: joi.string()
  }).unknown(false)
});

const value = {
  a: '123',
  b: {
    c: '123',
    f: 123
  },
  d: {
    e: '12',
    g: 123
  },
  h: 123
};

const result = schema.validate(value, {
  abortEarly: false,
  allowUnknown: true
});

console.log(result?.error?.message);
// child "d" fails because ["g" is not allowed]

After we use version 17, the data pass the validation, which we do not expect.

Do you have any suggestions?

Metadata

Assignees

No one assigned

    Labels

    bugBug or defectsupportQuestions, discussions, and general support

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions