# joi-extract-type

> Provides native type extraction from Joi schemas for Typescript

Latest version **15.0.8** (published 2020-07-13) · MIT license · 0 weekly downloads

## Install

```sh
npm install joi-extract-type
pnpm add joi-extract-type
yarn add joi-extract-type
bun add joi-extract-type
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 15.0.8 |
| Published | 2020-07-13 |
| First published | 2018-10-06 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 2 |
| Unpacked size | 55 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 152 |
| Author | Tiago de Carvalho Miranda |
| Maintainers | tgcvmr |
| Keywords | Joi, Typescript, Types, Extraction |

## Links

- npm: https://www.npmjs.com/package/joi-extract-type
- Repository: https://github.com/TCMiranda/joi-extract-type
- Homepage: https://github.com/TCMiranda/joi-extract-type#readme
- Issues: https://github.com/TCMiranda/joi-extract-type/issues
- npm.io page: https://npm.io/package/joi-extract-type

## Dependencies (2)

- [@hapi/joi](https://npm.io/package/@hapi/joi.md) ~15
- [@types/hapi__joi](https://npm.io/package/@types/hapi__joi.md) ~15

## Alternatives

- [@openai/codex-sdk](https://npm.io/package/@openai/codex-sdk.md) — 731.4K weekly downloads
- [babel-plugin-transform-react-jsx](https://npm.io/package/babel-plugin-transform-react-jsx.md) — 565.0K weekly downloads
- [babel-helper-remove-or-void](https://npm.io/package/babel-helper-remove-or-void.md) — 508.5K weekly downloads
- [@pnpm/store-controller-types](https://npm.io/package/@pnpm/store-controller-types.md) — 186.9K weekly downloads
- [react-native-signature-canvas](https://npm.io/package/react-native-signature-canvas.md) — 155.6K weekly downloads

## Recent versions

- 15.0.8 (latest) — 2020-07-13
- 0.0.6 (ts2-1) — 2018-10-31
- 15.0.7 — 2020-07-13
- 15.0.2 — 2019-11-12
- 15.0.1 — 2019-09-19
- 15.0.0 — 2019-06-03
- 0.1.8 — 2019-02-24
- 0.1.7 — 2019-02-24
- 0.1.6 — 2019-02-03
- 0.1.5 — 2019-01-12
- 0.1.4 — 2018-12-31
- 0.1.3 — 2018-11-16
- 0.1.2 — 2018-10-31
- 0.1.1 — 2018-10-31
- 0.1.0 — 2018-10-31
- … 5 more at https://npm.io/package/joi-extract-type/versions

## README

<!-- @format -->

# joi-extract-type

Provides native type extraction from [Joi](https://github.com/hapijs/joi) Schemas for Typescript.

---

Joi Schemas are great. But if you use Typescript to define your entities, it is easy to duplicate the definition inside the Schemas and all application interfaces.

This library enhances Joi interfaces to provides an utility to infer the type from a Schema.

**Why should you use it**

- Avoid duplication from Joi Schemas and application interfaces
- Port javascript applications using [Joi](https://github.com/hapijs/joi) to typescript easily
- Does not requires changes to already defined Schemas

**Limitation**

- Joi is probably a superset of typescript in terms of validation, branching, conditional types, etc. This library is probably suitable for most simples cases, but wont ever prevent every validation error to occur with just static analyses.
- This is ~experimental~ and a work in progress. Although I use it in some projects and works for 99% of my schemas.

## Installation

```
npm i --save joi-extract-type
```

**Built for typescript@^3**

For typescript 2.9 support checkout branch /typescript@2 or install with:

```
npm i --save joi-extract-type@ts2-1
```

## Usage

Import the library and patch Joi's typings:

```ts
import * as Joi from '@hapi/joi';
import 'joi-extract-type';
```

Create the schemas and use `Joi.extractType` to infer the type:

```ts
const is_enabled = Joi.boolean();
type extractBoolean = Joi.extractType<typeof is_enabled>;
export const extractedBoolean: extractBoolean = true;
```

**Examples**

The following code is copied from this library spec, which compiles the code with `tsc` that should not output any errors.

```ts
const is_enabled = Joi.boolean();
type extractBoolean = Joi.extractType<typeof is_enabled>;
export const extractedBoolean: extractBoolean = true;

const full_name = Joi.string();
type extractString = Joi.extractType<typeof full_name>;
export const extractedString: extractString = 'string';

const user = Joi.object({ full_name, is_enabled });
type extractObject = Joi.extractType<typeof user>;
export const extractedObject: extractObject = {
  full_name: extractedString,
  is_enabled: extractedBoolean,
};

const roles = Joi.array().items(Joi.string());
type extractArray = Joi.extractType<typeof roles>;
export const extractedArray: extractArray = ['admin'];

const apply = Joi.array().items(Joi.object({ id: Joi.string() }));
type extractApply = Joi.extractType<typeof apply>;
export const extractedApply: extractApply = [{ id: '3' }, { id: '4' }];

const rule = Joi.object({ apply });
type extractRule = Joi.extractType<typeof rule>;
export const extractedRule: extractRule = { apply: extractedApply };

export const jobOperatorRoleSchema = Joi.object({
  id: Joi.string().required(),
  user_id: Joi.string().required(),
  job_id: Joi.string().required(),
  role: Joi.string().valid(['recruiter', 'requester']),
  pipeline_rules: Joi.array().items(rule),
});
type extractComplexType = Joi.extractType<typeof jobOperatorRoleSchema>;
export const extractedComplexType: extractComplexType = {
  id: '2015',
  user_id: '102',
  job_id: '52',
  role: 'admin',
  pipeline_rules: [extractedRule],
};

// typeof extractedComplexType
// const extractedComplexType: Joi.extractMap<{
//     id: Joi.StringSchema;
//     user_id: Joi.StringSchema;
//     job_id: Joi.StringSchema;
//     role: Joi.StringSchema;
//     pipeline_rules: Joi.ArraySchema<Joi.extractMap<{
//         apply: Joi.ArraySchema<Joi.extractMap<{
//             id: Joi.StringSchema;
//         }>>;
//     }>>;
// }>
```

---
_Source: https://npm.io/package/joi-extract-type · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
