Socket
Socket
Sign inDemoInstall

untyped

Package Overview
Dependencies
58
Maintainers
2
Versions
36
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    untyped

[![npm version][npm-version-src]][npm-version-href] [![npm downloads][npm-downloads-src]][npm-downloads-href] [![Github Actions][github-actions-src]][github-actions-href] [![Codecov][codecov-src]][codecov-href] [![bundle][bundle-src]][bundle-href]


Version published
Weekly downloads
554K
increased by0.34%
Maintainers
2
Install size
34.7 MB
Created
Weekly downloads
 

Changelog

Source

v1.4.2

compare changes

📦 Build

  • Fix the default export module for cjs (#124)

❤️ Contributors

Readme

Source

untyped

npm version npm downloads Github Actions Codecov bundle

▶️ Check online playground

Install

# npm
npm i untyped

# yarn
yarn add untyped

# pnpm
pnpm add untyped

Usage

First we have to define a reference object that describes types, defaults, and a $resolve method (normalizer).

const defaultPlanet = {
  name: "earth",
  specs: {
    gravity: {
      $resolve: (val) => parseFloat(val),
      $default: "9.8",
    },
    moons: {
      $resolve: (val = ["moon"]) => [].concat(val),
      $schema: {
        title: "planet moons",
      },
    },
  },
};

API

resolveSchema

import { resolveSchema } from "untyped";

const schema = await resolveSchema(defaultPlanet);

Output:

{
  "properties": {
    "name": {
      "type": "string",
      "default": "earth"
    },
    "specs": {
      "properties": {
        "gravity": {
          "default": 9.8,
          "type": "number"
        },
        "moons": {
          "title": "planet moons",
          "default": ["moon"],
          "type": "array",
          "items": [
            {
              "type": "string"
            }
          ]
        }
      },
      "type": "object"
    }
  },
  "type": "object"
}

generateTypes

import { resolveSchema, generateTypes } from "untyped";

const types = generateTypes(await resolveSchema(defaultPlanet));

Output:

interface Untyped {
  /** @default "earth" */
  name: string;

  specs: {
    /** @default 9.8 */
    gravity: number;

    /**
     * planet moons
     * @default ["moon"]
     */
    moons: string[];
  };
}

generateMarkdown

import { resolveSchema, generateMarkdown } from "untyped";

const markdown = generateMarkdown(await resolveSchema(defaultPlanet));

Output:

# `name`

- **Type**: `string`
- **Default**: `"earth"`

# `specs`

## `gravity`

- **Type**: `number`
- **Default**: `9.8`

## `moons`

- **Type**: `array`
- **Default**: `["moon"]`

💻 Development

  • Clone this repository
  • Enable Corepack using corepack enable (use npm i -g corepack for Node.js < 16.10)
  • Install dependencies using pnpm install
  • Run interactive tests using pnpm dev
  • Use pnpm web to start playground website
  • Use pnpm test before push to ensure all tests and lint checks passing

License

MIT

Thanks to @dominikschreiber for donating package name.

FAQs

Last updated on 20 Jan 2024

Did you know?

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

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc