hapijs / hapijs/joi

Question: Is there a way to create a subschema without some fields of base schema dynamically?

Open
#2,794 2 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

#### Context

* *node version*: 12+
* *module version*: 17.6.0
* *environment* (e.g. node, browser, native): node

#### How can we help?

I have a case where I need to create a function which accepts joi schema as the first argument and array of string(object's keys) as the second argument. The main idea here is to return a new schema which consists of a base schema without keys which was mentioned as the second argument. Also, it would be cool to know how to do it with nested schemas.

I have tried different approaches(with different Joi methods), but I did not get the right behavior. Could someone help me to resolve this case?

```js
const schema = Joi.object({
a: Joi.any(),
b: Joi.any()
});

exclude(schema, ['b']) // should return new schema, like Joi.object({ a: Joi.any() })
```

Also, it will be cool to be able do like this:

```js
const schema = Joi.object({
a: Joi.any(),
b: Joi.any(),
c: Joi.object({
d: Joi.any(),
e: Joi.any()
})
});

exclude(schema, ['c.d']) // should return new schema, like
Joi.object({
a: Joi.any(),
b: Joi.any(),
c: Joi.object({
e: Joi.any()
})
});
```

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.