hapijs / hapijs/joi

Validate and strip alternatives with links only.

Open
#2,817 0 comments 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

#### Context

* *node version*: v16.15.0
* *module version with issue*: 17.6.0
* *last module version without issue*: ?
* *environment* (e.g. node, browser, native): node
* *used with* (e.g. hapi application, another framework, standalone, ...): hapi, standalone
* *any other relevant information*:

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

Have a set of alternative schemas (all) stripped correctly when using linked subschemas only.

`validateSchema1` shows the expected behavior (link-less).
`validateSchema3` shows the faulty behavior (links only).
`validateSchema2` shows a workaround behavior (fake alternative).

```ts
import * as Joi from 'joi';

export const validateA = Joi.object({
a: Joi.any().required(),
}).id('validateA');

export const validateB = Joi.alternatives()
.try(Joi.object({ b: Joi.string().required() }))
.match('all')
.id('validateB');

export const validateSchema1 = Joi.alternatives(
validateA,
validateB
)
.match('all')
.id('validateSchema1');

export const validateSchema2 = Joi.alternatives(
Joi.object({}),
Joi.link('#validateA'),
Joi.link('#validateB')
)
.match('all')
.id('validateSchema2')
.shared(validateA)
.shared(validateB);

export const validateSchema3 = Joi.alternatives(
Joi.link('#validateA'),
Joi.link('#validateB')
)
.match('all')
.id('validateSchema3')
.shared(validateA)
.shared(validateB);

const v = {
a: 2,
b: 'b',
c: 'stripMe',
};

console.log(validateSchema1.validate(v, { stripUnknown: true }));
console.log(validateSchema2.validate(v, { stripUnknown: true }));
console.log(validateSchema3.validate(v, { stripUnknown: true }));
```

#### What was the result you got?

```
{ value: { a: 2, b: 'b' } }
{ value: { a: 2, b: 'b' } }
{ value: { b: 'b' } }
```

#### What result did you expect?

```
{ value: { a: 2, b: 'b' } }
{ value: { a: 2, b: 'b' } }
{ value: { a: 2, b: 'b' } }
```

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.