Extracting types from a Joi schema
- 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*: >= 16.14.0
* *module version*: joi -> 17.6.0
* *environment* (e.g. node, browser, native): all
* *used with* (e.g. hapi application, another framework, standalone, ...): standalone
* *any other relevant information*: no
#### What problem are you trying to solve?
As we are already creating the Joi schema we don't have to write the interface for it's value type. It can be inferred from the schema itself.
```js
// the type of the schema at this point is Joi.ObjectSchema
const schema = Joi.object({
username: Joi.string().required(),
password: Joi.string().required(),
});
// we'd like to be able to extract the type from the schema
// as Joi already knows what we expect the type to be
type schema = Joi.extractType;
// schema type would look like the following
// schema: {
// username: string;
// password: string
// }
```
#### Do you have a new or modified API suggestion to solve the problem?
[Joi extract type](https://www.npmjs.com/package/joi-extract-type) plugin has solved this in the past, but hasn't kept up with the latest releases of Joi, and it does feel reliable for a long term use. Can't that logic be part of the hapi/joi library instead?
Contributor guide
Assessment
This issue has not been assessed yet.