npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@krakerxyz/json-schema-transformer

v0.4.4

Published

TypeScript transformer that will generate a json schema for a given type at build time.

Downloads

3

Readme

json-schema-transformer

This TypeScript transformer will look for references to jsonSchema<SomeType>() in code and replace it with the JSON Schema for SomeType.

I created this project after experimenting with Fastify. I learned that it has built in Ajv validator support which utilizes JSON Schema for the validation rules.

There's something about writing a TypeScript interface then also manually writing JSON Schema that doesn't seem DRY to me. I wanted to be able to write my interfaces then have the JSON Schema generated automatically, igitur, json-schema-transformer.

Example TypeScript

import { jsonSchema } from '@krakerxyz/json-schema-transformer';

export interface User {
    readonly id: string;
    name: string;
    description?: string | null;
    items: UserItem[];
}

export interface UserItem {
    foo: string;
    bar: string | number;
}

const test = jsonSchema<User>();

Compiled JS

const test = { 
    type: "object", 
    properties: { 
        id: { type: ["string"] }, 
        name: { type: ["string"] }, 
        description: { type: ["string", "null"] }, 
        items: { 
            type: "array", 
            items: { 
                type: "object", 
                properties: { 
                    foo: { type: ["string"] }, 
                    bar: { type: ["string", "number"] } 
                }, 
                required: ["foo", "bar"], 
                additionalProperties: false 
            } 
        } 
    }, 
    required: ["id", "name", "items"], 
    additionalProperties: false 
};

Warning

This library was developed as a proof-of-concept and type support is not robust enough for general public to use. Expect bugs.

If you have interest in something like this for your project, adding a watch or star on GitHub will motivate me to polish it for general release.

Installation

npm install @krakerxyz/json-schema-transformer

We now need to tell TypeScript it needs to run it. Unfortunately, registering the transformer through tsconfig.json is not natively supported. ttypescript to the rescue.

npm install ttypescript --save-dev

TTypescript will read the transformers to use from tsconfig.json

{
    "compilerOptions": {
        ...
        "plugins": [
            {
                "transform": "@krakerxyz/json-schema-transformer/dist/cjs/transformer/transformer"
            }
        ]
    }
}

We now use ttypescript to invoke our builds. In our package.json, we'll use a script similar to

{
    "scripts": {
        "build": "ttsc"
    }
}

Usage

Simply import jsonSchema from @krakerxyz/json-schema-transformer and then make a call to it, passing your interface/type in as the type argument. The transformer will find calls to jsonSchema() and replace "jsonSchema()" with the JSON Schema markup.

Example usage within a Fastify handler

import { RouteOptions } from 'fastify';
import { MyPostBody } from '../types';
import { jsonSchema } from '@krakerxyz/json-schema-transformer';

export const apiHandler: RouteOptions = {
    method: 'POST',
    url: '/api',
    schema: {
        body: jsonSchema<MyPostBody>())
    },
    handler: async (req, res) => {
        const postBody = req.body as MyPostBody;
    }
}