Skip to content

Latest commit

 

History

History

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

README.md

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[];
}