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-schema-validators-typescript-interfaces

v1.0.8

Published

Accepts a directory full of json-schema files and produces associated validators, TypeScript interfaces, and optional Yup validators. Great for validating server side with NodeJS and improving TypeScript client side dev.

Downloads

5

Readme

JSON Schema to Validators and TypeScript Interfaces

This module uses ajv, ajv-pack, and json-schema-to-typescript to read a directory full of json schema documents and spit out associated validators and TypeScript interfaces.

This allows developers to define their data models that require validation once in json schema format while automatically getting access to performant validators and TypeScript interfaces for development.

One good use case for this module is serverless NodeJS development. The developer has json schema validators that check requests, and data models on the backend, and has TypeScript interfaces for data models that can be used both in Lambda code and front-end code.

Quick Start

npx json-schema-validators-typescript-interfaces -s <path to your json schema source>

Installation

yarn add json-schema-validators-typescript-interfaces -D

Command Line Usage

From the root of your project, you can invoke the module directly

json-schema-validators-typescript-interfaces -s <path to your json schema source>

Build Integration

//todo

Regex Patterns

Some json schema properties might use the same regex patterns. Client side checks might also need to use those same regex patterns for client side validation of individual fields. It can be convenient and less error prone to have a library or regex patterns for re-use across your project.

If you store your regex patterns in a separate module, this library can reference them and replace template placeholders in your json schema files.

NOTE: json schema does not support case-insensitive flags so any flags in the provided regex patterns are stripped off.

Here is a sample of a regex pattern module:

const patterns = {

  EMAIL_ADDRESS: /(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*|"(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21\x23-\x5b\x5d-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])*")@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\[(?:(?:(2(5[0-5]|[0-4][0-9])|1[0-9][0-9]|[1-9]?[0-9]))\.){3}(?:(2(5[0-5]|[0-4][0-9])|1[0-9][0-9]|[1-9]?[0-9])|[a-z0-9-]*[a-z0-9]:(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21-\x5a\x53-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])+)\])/,

  URL_ANY: /^((https?|ftp|file):\/\/)?([\da-z\.-]+)\.([a-z\.]{2,6})([\/\w \.-]*)*\/?$/,

  URL_HTTPS: /^((https):\/\/)?([\da-z\.-]+)\.([a-z\.]{2,6})([\/\w \.-]*)*\/?$/,
};

module.exports = patterns;

Here is how these could be templated inside of your json schema:

{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "type": "object",
  "properties": {
    "name": {
      "minLength": 3,
      "maxLength": 100,
      "type": "string"
    },
    "description": {
      "minLength": 10,
      "maxLength": 500,
      "type": "string"
    },
    "url": {
      "pattern": "${PATTERN URL_HTTPS}",
      "type": "string"
    },
    "emailAddress": {
      "pattern": "${PATTERN EMAIL_ADDRESS}",
      "type": "string"
    },
    "phone": {
      "type": "string"
    }
  },
  "required": [
    "description",
    "name"
  ]
}

In order to tell this library to use your regex module, simply provide the path via the --patterns or -p parameter

json-schema-validators-typescript-interfaces -s <path to your json schema source> -p <path to your regex patterns module>

Custom Interface and Validator Target Directories

By default the library will place validator and interface artifacts next to where your json schema files are located in directories json-schema-validators and json-schema-interfaces.

You can override either of them by providing command line arguments. For details see the help:

json-schema-validators-typescript-interfaces -h