Skip to content

mmiszy/joi-extract-type

 
 

Repository files navigation

joi-extract-type

Provides native type extraction from Joi Schemas for Typescript.


Joi Schemas are great. But if you use Typescrypt to define your entities, it is easy to dupplicate 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 dupplication from Joi Schemas and application interfaces
  • Port javascript applications using Joi to typescript easly
  • 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 occour with just static analisys.
  • This is experimental and a work in progress.

Usage

Import the library and patch Joi's typings:

import * as Joi from 'joi';
import './index';

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

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.

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 extactedArray: 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;
//         }>>;
//     }>>;
// }>

About

Provides native type extraction from Joi schemas for Typescript

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • TypeScript 100.0%