🚀 Big News: Socket Acquires Coana to Bring Reachability Analysis to Every Appsec Team.Learn more
Socket
Book a DemoInstallSign in
Socket

yup-type-support

Package Overview
Dependencies
Maintainers
1
Versions
7
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

yup-type-support

A typescript support for yup. Able to infer the yup schema from a typescript interface.

1.0.6
latest
npm
Version published
Weekly downloads
10
400%
Maintainers
1
Weekly downloads
 
Created
Source

Yup Type Support

A typescript compiler plugin that infers yup schema object from a typescript type or interface.

Installation

Use the package manager npm to install Yup Type Support.

npm i yup-type-support -D

Usage

Yup Type Support requires ttypescript. You can install the ttypescript using:

npm i ttypescript -D

Next you need to edit the tsconfig file to add the plugin to the compiler options.

{
  "compilerOptions": {
    "plugins": [{ "transform": "yup-type-support", "type": "program" }],
  }
}

To use the yup type support to generate yup schema object at the compile time automatically, you simply need to put a comment annotation in the yup.object() function call.

Example:

import * as yup from 'yup'

export interface simpleAccount {
    id: number;
    name: string;
    checking: boolean;
    notes?: string;
}

/* this will generate the following schema at compile time
 * exports.simpleAccountSchema = yup.object({
 *    id: yup.number().required(),
 *    name: yup.string().required(),
 *    checking: yup.boolean().required(),
 *    note: yup.string().optional()
 * });
 */
export const simpleAccountSchema: yup.SchemaOf<simpleAccount> = yup.object(/* guess schema */);

Currently, the plugin only supports number and string type, as well as the optional mark. More features are still under development. You also need to follow the format in the example closely (mainly how you declare the schema that will get changed in compile time), more flexible format support is also under development.

Now you can compile your typescript file using (or you can add the command to your package.json file):

ttsc

Supported functions

1. Types

Currently, the yup-type-support supports all three primitive types in typescript. Support for array, enum and other types are still under development.

1.1. string

interface myInterface {
    key: string
}

// The interface above generates the following schema
const myInterfaceSchema: yup.SchemaOf<myInterface> = yup.object({
    key: yup.string().required(),
});

1.2. number

interface myInterface {
    key: number
}

// The interface above generates the following schema
const myInterfaceSchema: yup.SchemaOf<myInterface> = yup.object({
    key: yup.number().required(),
});

1.3. boolean

interface myInterface {
    key: boolean
}

// The interface above generates the following schema
const myInterfaceSchema: yup.SchemaOf<myInterface> = yup.object({
    key: yup.boolean().required(),
});

2. Properties

Currently, the yup-type-support supports requried and optional property of yup. Support for nullable is still under development.

2.1. optional

interface myInterface {
    key?: string
}

// The interface above generates the following schema
const myInterfaceSchema: yup.SchemaOf<myInterface> = yup.object({
    key: yup.string().optional(),
});

3. Roadmap for more functions

The development for now will focus on supporting more types, including array, enum, etc. For properties, nullable will be supported soon. The next major goal is to support nested types and convert them to the nested schema for yup.

Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

Please make sure to update tests as appropriate.

License

MIT

FAQs

Package last updated on 31 Jul 2022

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