npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

joi-extract-type

v15.0.8

Published

Provides native type extraction from Joi schemas for Typescript

Downloads

207,566

Readme

joi-extract-type

Provides native type extraction from 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 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:

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

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