Socket
Book a DemoInstallSign in
Socket

json-schema-codec-generator

Package Overview
Dependencies
Maintainers
1
Versions
17
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

json-schema-codec-generator

Generate typed codec validator code from collections of json-schema

latest
Source
npmnpm
Version
1.0.1
Version published
Maintainers
1
Created
Source

JSON-Schema Codec Generator

Generate typed codec validator code from collections of inter-related json-schema

Generate typed codecs from collections of json-schema using the included library or the bundled CLI.

  • Supports collections of schemas having $references between one-another.
  • Produces type-safe validator functions via ajv.
  • Designed for code-generation to maximize runtime performance.

Installation

To install the CLI, the recommended approach is to install it globally.

npm install -g json-schema-codec-generator

Alternatively, you can use npx to install and run it on-demand.

npx json-schema-codec-generator --help

CLI Usage

Given a folder full of .json files containing JSON-Schema definitions in ./path/to/my/schema/dir, the following will produce:

  • ./codecs/index.js -- JavaScript code that exports a ValidatorFunction<T> function for each discovered schema. These functions have no runtime dependencies and thus can be used in any JavaScript runtime.

    Each Schema passed in will have a corresponding ValidatorFunction generated.

  • ./codecs/index.d.ts -- TypeScript definition files for the generated codecs. These type definitions ensure that code consuming the codecs will benefit from compile-time and edit-time type hinting.

json-schema-codec-generator --input ./path/to/my/schema/dir --output ./codecs/

ValidatorFunction

export interface ErrorObject {
  instancePath: string;
  message?: string;
  data?: unknown;
}

export interface ValidatorFunction<T> {
  (obj: unknown): obj is T;
  errors?: ErrorObject[];
}

For a given schema named Bookmark, a validator function named validateBookmark of type ValidatorFunction<Bookmark> will be exported as well as a Bookmark type. These functions can be used as type assertions. Immediately after calling a validator function and seeing a validation failure, the validation errors can be recovered from validateBookmark.errors.

Keywords

json-schema

FAQs

Package last updated on 16 Apr 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts