Wrapping of nested schema properties
- 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*: n.a
* *module version*: `17.4.0 `
* *environment* (e.g. node, browser, native): node
* *used with* (e.g. hapi application, another framework, standalone, ...): hapi
* *any other relevant information*:
#### How can we help?
Let's say I am using a library that performs Joi Schema validation:
```
const configSchema = Joi.object().keys({
foo: Joi.object().keys({
bar: Joi.number().min(0)
})
})
```
Now let's pretend that I want to be able to write a "dynamic" (interpreted) configuration using environement variables:
```js
const config = {
foo: {
bar: "{ env.FOO_BAR }"
}
}
```
How should I write a "schema transform" function that will transform the initial `configSchema` into something like:
```
const configSchemaWithEnv = Joi.object().keys({
foo: Joi.object().keys({
bar: Joi.alternatives(
Joi.number().min(0), // Initial value
Joi.string().regexp(ENV_VAR_REXEXP)
)
})
})
```
(of course any input would need to be interpreted before being actually used, but that's not the point)
In other words, how can I write a `addEnvVarSupportToJoiSchema` that transforms a basic schema by wrapping every/some parts of it ?
```js
const addEnvVarSupportToJoiSchema = (schema) => {
//
return alteredSchema
}
const configSchemaWithEnv = addEnvVarSupportToJoiSchema(configSchema)
```
Alternatively, if needed, I could also use something that allows passing a custom Joi object:
```js
const configSchemaCreator = (joi) => joi.object().keys({
foo: joi.object().keys({
bar: joi.number().min(0)
})
})
const alteredSchema = addEnvVarSupportToJoiSchemaUsingCustomJoi(configSchemaCreator)
```
Could you point me how to do this.
Contributor guide
Assessment
This issue has not been assessed yet.