GithubHelp home page GithubHelp logo

coreycollins / joi-extract-type Goto Github PK

View Code? Open in Web Editor NEW

This project forked from tcmiranda/joi-extract-type

0.0 0.0 0.0 52 KB

Provides native type extraction from Joi schemas for Typescript

License: MIT License

TypeScript 100.00%

joi-extract-type's Introduction

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. 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:

import * as Joi from '@hapi/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;
//         }>>;
//     }>>;
// }>

joi-extract-type's People

Contributors

guyellis avatar tcmiranda avatar typeofweb avatar winhillsen avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.