hapijs / hapijs/joi

In a nested object -> pattern -> object schema, defaults are not populated in the result if data is invalid

Open
#2,449 0 comments 1 reaction 0 assignees View on GitHub
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): No.
* *is this issue affecting a production system?* (yes/no): No.

#### Context

* *node version*: 12.14.0
* *module version with issue*: `joi@17.2.0`
* *last module version without issue*: Unknown
* *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 run the following script:

```js
const joi = require('joi');

// schemas
const guestSchema = joi.object().keys({ name: joi.string(), fun: joi.boolean().default(true) });
const guestsSchema = joi.object().pattern(joi.string(), guestSchema);
const partySchema = joi.object().keys({ guests: guestsSchema });

// data
const guests = {
alice: { name: 'alice' },
bob: { name: true },
};
const party = { guests };

// tests
console.log('Validating the guests object populates the default field, even for bob');
let result = guestsSchema.validate(guests, { abortEarly: false });
console.log(JSON.stringify(result, undefined, 2));

console.log('----');

console.log('Validating the party object does not populate the default field for any guest');
result = partySchema.validate(party, { abortEarly: false });
console.log(JSON.stringify(result, undefined, 2));
```

#### What was the result you got?

- ✅ In the call `guestsSchema.validate` the values of `result.value.alice.fun` and of `result.value.bob.fun` were `true`.
- 🟥 In the call to `partySchema.validate`, the values of `result.value.guests.alice.fun` and of `result.value.guests.bob.fun` were `undefined`.

#### What result did you expect?

In the call to `partySchema.validate`, the values of `result.value.guests.alice.fun` and of `result.value.guests.bob.fun` should be `true`.

#### Additional Information

The same or a similar bug reproduces with a simpler guest schema:
```
// schemas
const guestSchema = joi.string().default('anon');
const guestsSchema = joi.object().pattern(joi.string(), guestSchema);
const partySchema = joi.object().keys({ guests: guestsSchema });

// data
const guests = {
diamond: true,
edward: undefined,
};
const party = { guests };

// tests
console.log('Validating the guests object populates edward as anon');
let result = guestsSchema.validate(guests, { abortEarly: false });
console.log(JSON.stringify(result, undefined, 2));

console.log('----');

console.log('Validating the party object does not populate edward as anon');
result = partySchema.validate(party, { abortEarly: false });
console.log(JSON.stringify(result, undefined, 2));
```

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.