Socket
Book a DemoInstallSign in
Socket

@musicglue/joi-ts-generator

Package Overview
Dependencies
Maintainers
8
Versions
53
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@musicglue/joi-ts-generator

Generate Typescript Types from Joi schemas

latest
Source
npmnpm
Version
6.0.2
Version published
Maintainers
8
Created
Source

joi-ts-generator

Generate Typescript Types from Joi Schemae

Installing

yarn add --dev @musicglue/joi-ts-generator

Then you should add a script to your package json that you can use to regenerate your types, such as the following:

{
  "scripts": {
    "regenerate-joi-types": "joi-ts-gen"
  },
  "joiTsGenerator": {
    "input": "./src/schemas/index.ts",
    "outputs": {
      "types": "./src/schemas/types.ts",
      "utils": "./src/schemas/utils.ts"
    }
  }
}

You can then rebuild your types by running yarn regenerate-joi-types.

Defining

If you define Joi schemae that look like the following:

import joi = require("joi");

export const Country = joi.string().notes("type:Country");
export const Currency = joi.string().notes("type:Currency");
export const Package = joi.string().valid(["gold", "silver", "bronze"]).notes("type:Package");

export const Purchase = joi.object().keys({
  id: joi.string().guid().required(),
  country: Country,
  currency: Currency.required(),
  cents: joi.number().required(),
  notes: joi.array().items(joi.string()),
  package: Package.required(),
}).notes("type:Purchase");

Then this project will generate you Typescript typings as follows:

export type Uuid = string;
export type Country = string;
export type Currency = string;
export type Package = "gold" | "silver" | "bronze";

export interface Purchase {
  id: Uuid;
  country?: Country;
  currency: Currency;
  cents: number;
  notes?: string[];
  package: Package;
}

Troubleshooting

If you are defining string types and find that you get unexpected results, please follow the following:

import * as joi from "joi";
import cloneSchema from "@musicglue/joi-ts-generator"

export const Country = cloneSchema(joi.string());
export const Currency = cloneSchema(joi.string());

This prevents our internals from overwriting themselves as we parse the schemae. Joi objects are immutable, other than the entrypoint...

FAQs

Package last updated on 08 Aug 2017

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

About

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.

  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc

U.S. Patent No. 12,346,443 & 12,314,394. Other pending.