1.1.0 • Published 8 months ago

@loopstack/schema-to-template v1.1.0

Weekly downloads
-
License
MIT
Repository
github
Last release
8 months ago

Schema to Template

A lightweight TypeScript utility for converting JSON schemas to markdown templates.

Related to loopstack-ai/markdown-parser.

Features

  • Convert JSON schemas to markdown templates
  • Support for nested objects and arrays
  • Great for use in llm prompts to provide a response template
  • Use in combination with loopstack-ai/markdown-parser for parsing the response back to a structured object.

Installation

npm install @loopstack/schema-to-template

Usage

import { SchemaToTemplateBuilder, JSONSchemaConfigType } from '@loopstack/schema-to-template';

// Create an instance of the builder
const builder = new SchemaToTemplateBuilder();

// Define your JSON schema
const schema: JSONSchemaConfigType = {
  type: 'object',
  properties: {
    user: {
      type: 'object',
      properties: {
        name: {
          type: 'string',
          description: 'The name of the user'
        },
        email: {
          type: 'string',
          description: 'The email address of the user'
        },
        roles: {
          type: 'array',
          items: {
            type: 'string',
            description: 'A user role'
          }
        }
      }
    }
  }
};

// Convert the schema to a markdown template
const markdownTemplate = builder.createTemplateFromSchema(schema);
console.log(markdownTemplate);

Output:

# user

## name

The name of the user

## email

The email address of the user

## roles

### item 1

A user role

### item 2

A user role

... repeat for each role

API

SchemaToTemplateBuilder

The main class that handles conversion of JSON schemas to markdown templates.

Methods

  • createTemplateFromSchema(schema: JSONSchemaConfigType | null | undefined): string
    Converts a JSON schema to a markdown template

  • schemaToMarkdown(schema: JSONSchemaConfigType | null | undefined, level: number, parentKey?: string): string
    Internal method that handles the conversion recursively

Development

# Install dependencies
npm install

# Run tests
npm test

# Run tests with watch mode
npm run test:watch

# Build the package
npm run build

Author

Jakob Klippel
LinkedIn: Jakob Klippel

License

MIT

1.1.0

8 months ago

1.0.4

10 months ago

1.0.3

11 months ago

1.0.2

11 months ago

1.0.1

11 months ago