hapijs / hapijs/joi

[Feature request] Prevent parent's tailor option from affecting children tailor options

Open
#2,462 2 comments 0 reactions 1 assignee Claimed by @Marsup View on GitHub
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): no
* *is this issue affecting a production system?* (yes/no): no

#### Context

* *node version*: v12.16.3
* *module version*: v17.2.1
* *environment*: node
* *used with*: standalone
* *any other relevant information*: n/a

#### What problem are you trying to solve?

When using the tailor option both on a child object schema and on the parent schema, the child object will use the tailor option of the parent not it's own.

See below an example using the Joi sandbox:

*Code:*
```javascript
Joi.object({
requiredKey: Joi.string().alter({
create: schema => schema.required(),
update: schema => schema.forbidden()
}),
nested: Joi.object({
forbiddenKey: Joi.string().alter({
create: schema => schema.required(),
update: schema => schema.forbidden()
})
}).tailor("update")
}).tailor("create")
```

```json
{
"requiredKey": "This should pass",
"nested": {
"forbiddenKey": "This should fail"
}
}
```

*Result screenshot:*

![image (9)](https://user-images.githubusercontent.com/28379549/92583934-9de91a00-f2c5-11ea-89e4-a03361dcc382.png)

#### Do you have a new or modified API suggestion to solve the problem?

The ability to specify options for tailor and create a `freeze` option that will prevent tailor from being changed once it is set.

```javascript
Joi.object({
requiredKey: Joi.string().alter({
create: schema => schema.required(),
update: schema => schema.forbidden()
}),
nested: Joi.object({
forbiddenKey: Joi.string().alter({
create: schema => schema.required(),
update: schema => schema.forbidden()
})
}).tailor("update", { freeze: true })
}).tailor("create")
```

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.