schema-typescript
TypeScript icon, indicating that this package has built-in type declarations

0.12.0 • Public • Published

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

Contributing 🤝

Contributions are what make the open-source community such an amazing place to learn, inspire, and create. Any contributions you make are greatly appreciated.

Need Help?

schema-typescript might not work perfectly for all JSON schemas yet. We value your feedback and contributions to make it better. If you encounter any issues or have suggestions for improvements, please let us know.

License 📜

Distributed under the MIT License. See `LICENSE` for more information.

Package Sidebar

Install

npm i schema-typescript

Weekly Downloads

174

Version

0.12.0

License

SEE LICENSE IN LICENSE

Unpacked Size

74.7 kB

Total Files

28

Last publish

Collaborators

  • pyramation