npm.io
0.14.4 • Published 3 months ago

schema-typescript

Licence
MIT
Version
0.14.4
Deps
5
Size
44 kB
Vulns
2
Weekly
0

schema-typescript


JSON Schema TypeScript utilities for the Interweb

Welcome to schema-typescript! This project provides robust tools for handling JSON schemas and converting them to TypeScript interfaces with ease and efficiency.

Features

  • JSON Schema to TypeScript: Convert JSON schemas into TypeScript interfaces automatically.

  • Modular: Designed to be reusable with minimal dependencies.

  • Supports $ref and $defs: Fully supports JSON Schema references, allowing you to define complex schemas modularly.

  • Multiple Entities Handling: Handles arrays of defined types, such as multiple dogs or cats, seamlessly in your schemas.

Getting Started

To get started with schema-typescript, simply run:

npm install schema-typescript

Usage

Here's a quick example to show you how to convert a JSON schema into TypeScript interfaces:

import { generateTypeScript } from 'schema-typescript';

const schema = {
    "$id": "https://example.com/person.schema.json",
    "$schema": "https://json-schema.org/draft-07/schema#",
    "title": "Person",
    "type": "object",
    "properties": {
        "firstName": { "type": "string" },
        "pets": {
            "type": "array",
            "items": { "$ref": "#/$defs/pet" }
        }
    },
    "required": ["firstName", "pets"],
    "$defs": {
        "pet": {
            "type": "object",
            "properties": {
                "name": { "type": "string" },
                "type": { "type": "string" } 
            },
            "required": ["name", "type"]
        }
    }
};

console.log(generateTypeScript(schema));
// OUTPUT:
interface Pet {
  name: string;
  type: string;
}
interface Person {
  firstName: string;
  pets: Pet[];
}

Development

Setup
  1. Clone the repository:
git clone https://github.com/constructive-io/dev-utils.git
  1. Install dependencies:
cd dev-utils
pnpm install
pnpm build
  1. Test the package of interest:
cd packages/<packagename>
pnpm test:watch

Credits

Built by the Constructive team — creators of modular Postgres tooling for secure, composable backends. If you like our work, contribute on GitHub.

Disclaimer

AS DESCRIBED IN THE LICENSES, THE SOFTWARE IS PROVIDED "AS IS", AT YOUR OWN RISK, AND WITHOUT WARRANTIES OF ANY KIND.

No developer or entity involved in creating this software will be liable for any claims or damages whatsoever associated with your use, inability to use, or your interaction with other users of the code, including any direct, indirect, incidental, special, exemplary, punitive or consequential damages, or loss of profits, cryptocurrencies, tokens, or anything else of value.

Keywords