1.0.1 • Published 7 months ago

json-schema-codec-generator v1.0.1

Weekly downloads
5
License
MIT
Repository
github
Last release
7 months ago

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:

  1. ./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.

  2. ./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.

1.0.1

7 months ago

1.0.0

7 months ago

0.8.1

7 months ago

0.8.0

1 year ago

0.7.2

2 years ago

0.7.1

2 years ago

0.7.0

2 years ago

0.7.0-0

2 years ago

0.5.0

4 years ago

0.6.1

4 years ago

0.6.0

4 years ago

0.4.5

4 years ago

0.4.4

4 years ago

0.3.2

4 years ago

0.4.0

4 years ago

0.4.2

4 years ago

0.1.1

4 years ago

0.1.0

4 years ago