hapijs / hapijs/joi

coercions during alternative match leads to unexpected results

Open
#2,579 1 comment 0 reactions 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): yes
* *is this issue affecting a production system?* (yes/no): yes

#### Context

* *node version*: 14
* *module version with issue*: 17.x
* *last module version without issue*: n/a
* *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?

Coercions occurring during an alternative match do not take precedence over the uncoerced values during the subschema response value merging, resulting in uncoerced properties overwriting coerced ones.

```js
// coercion works as expected when not inside `alternatives`
joi.object({ org_id: joi.number().required() }).unknown().validate({ org_id: '5', foo: 'bar' }, { convert: true })
// === { value: { org_id: 5, foo: 'bar' } }

// coercion fails when inside `alternatives`
joi.alternatives().try(
joi.object({ org_id: joi.number().required() }).unknown(),
joi.object({ foo: joi.string().required() }).unknown()
).match('all').validate({ org_id: '5', foo: 'bar' }, { convert: true })
// === { value: { org_id: '5', foo: 'bar' } }
```

#### What was the result you got?

org_id: '5'

#### What result did you expect?

org_ig: 5

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.