hapijs / hapijs/joi

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

Open
#2,824 10 comments 0 reactions 0 assignees View on GitHub
bug support
Dominant language
JavaScript
Stars
21.2k
Forks
1.5k
Avg merge
4h 57m
Merged PRs (30d)
14

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:
```js
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?

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.