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

json-lint-d-ts

v1.2.2

Published

Lint JSON files with Typescript declaration files

Downloads

17

Readme

json-lint-d-ts

npm Node.js CI Dependency Status Dev Dependency Status

image

Write type safe json files in your project with Typescript.

Installation

yarn -D json-lint-d-ts

Usage

Validation

Import validate function and pass all your JSON files paths with their type declarations.

import { validate } from "json-lint-d-ts";

const result = validate([
    ["./hello.json", "./hello.d.ts"],
]);

/*
[
  {
    jsonPath: './hello.json',
    jsonErrors: [
      `hello.json (13,3): Type '"World"' is not assignable to type '"world"'.`
    ]
  }
]
*/
console.log(result);

Typescript declaration example:

⚠️ it is important to have type Root in your d.ts file ⚠️

interface HelloLowerCase {
    hello: "world";
}
interface HelloUpperCase {
    HELLO: "WORLD";
}

type HelloType = HelloLowerCase | HelloUpperCase;

type Root = HelloType

The result of the validation:

[
  {
    jsonPath: './hello.json',
    jsonErrors: [
      `hello.json (13,3): Type '"World"' is not assignable to type '"world"'.`
    ]
  }
]

The usage example could be found in demo/hello-world folder.

Generation

It is possible to generate typescript files automatically for existing jsons:

const name = "LintRule";
generate(
    {
        id: "rule-semi",
        name: "semi",
        description: "Rule to describe usage of semicolons",
        level: "warning",
        isLevel: false,
    },
    {
        name: name,
        shouldOutput: true,
    }
);

It will output the file: LintRule.d.ts with the following contents:

export interface LintRule {
    id:          string;
    name:        string;
    description: string;
    level:       string;
    isLevel:     boolean;
}

interface Root extends LintRule {}

Advanced usage:

In case you want to generate schemas from endpoint, json-lint-d-ts supports url samples:

const name = "UsersEndpoint";
const result = await generateAsync(["https://yourwebsite.com/api/users?page=1"],
    {
        name: name,
        shouldOutput: true,
    }
);

In case API returns such data back:

{
    "page": 1,
    "per_page": 6,
    "total": 12,
    "total_pages": 2,
    "data": [{
        "id": 1,
        "email": "[email protected]",
        "first_name": "George",
        "last_name": "Bluth",
        "avatar": "https://s3.amazonaws.com/avatar.jpg"
    }, {
        "id": 2,
        "email": "[email protected]",
        "first_name": "Janet",
        "last_name": "Weaver",
        "avatar": "https://s3.amazonaws.com/avatar.jpg"
    }, {
        "id": 3,
        "email": "[email protected]",
        "first_name": "Emma",
        "last_name": "Wong",
        "avatar": "https://s3.amazonaws.com/avatar.jpg"
    }, {
        "id": 4,
        "email": "[email protected]",
        "first_name": "Eve",
        "last_name": "Holt",
        "avatar": "https://s3.amazonaws.com/avatar.jpg"
    }, {
        "id": 5,
        "email": "[email protected]",
        "first_name": "Charles",
        "last_name": "Morris",
        "avatar": "https://s3.amazonaws.com/avatar.jpg"
    }, {
        "id": 6,
        "email": "[email protected]",
        "first_name": "Tracey",
        "last_name": "Ramos",
        "avatar": "https://s3.amazonaws.com/avatar.jpg"
    }],
    "ad": {
        "company": "Weekly News",
        "url": "http://news.org/",
        "text": "A weekly newsletter focusing on development"
    }
    }

the following TS file would be generated:

// UsersEndpoint.d.ts

export interface UsersEndpoint {
    page:        number;
    per_page:    number;
    total:       number;
    total_pages: number;
    data:        Datum[];
    ad:          Ad;
}

export interface Ad {
    company: string;
    url:     string;
    text:    string;
}

export interface Datum {
    id:         number;
    email:      string;
    first_name: string;
    last_name:  string;
    avatar:     string;
}

interface Root extends UsersEndpoint {}

Troubleshooting

You could pass boolean value isDiagnosticsFileCreated in the object as a second argument to the validate function

import { validate } from "json-lint-d-ts";

const result = validate([
    ["./hello.json", "./hello.d.ts"],
], {
    isDiagnosticsFileCreated: true,
});

This will generate a joined file near your json document hello.json.ts with your types and json content where you could manually compare the difference.

How does all this work?

JSON validation:

Under the hood json-lint-d-ts uses Typescript Compiler by extending compiler host. JSON objects are loaded from filesystem and compared against passed .d.ts files. The result is extracted from diagnostics.

Typescript Schema generation:

For Typescript files generation from raw JSON quicktype is used

Contributing

Please do!

Alternatives

  • jsonschema: JSON Schema is a vocabulary that allows you to annotate and validate JSON documents http://json-schema.org/.
  • joi: schema description language and data validator for JavaScript.

Enjoy

🚀