@divine/judgement-cli
TypeScript icon, indicating that this package has built-in type declarations

0.2.0 • Public • Published

The Divine Judgement

Divine Judgement is a simple, yet powerful tool for enforcing TypeScript types at runtime. It:

  1. Reads and parses interfaces, enums and other types directly from your TypeScript source files.
  2. Generates a single file that contains a JSON schema, a type map and two utility functions, as and is. You should include this file in your project.
  3. You may then use those utilities to validate untrusted data at runtime, according to the TypeScript types defined in your project.

Installation

Add @divine/judgement to dependencies and @divine/judgement-cli to devDependencies:

npm install @divine/judgement
npm install --save-dev @divine/judgement-cli

Usage

Generate the schema and validation utilities:

npm exec -- judgement-cli -p 'types/*.ts' -o schema.ts

Then, in your code:

import { as, is } from './schema';
import { readFileSync } from 'fs';

const configName = './config.json';
const configFile = as('MyConfigFile',
                      JSON.parse(readFileSync(configName, 'utf8')),
                      configName);

// configFile is typed as MyConfigFile and is guaranteed to be valid,
// or else a SchemaError would have been thrown.

const config: unknown = configFile;

if (is('MyConfigFile', config)) {
    // config is now typed as MyConfigFile and guaranteed to be valid.
}

Acknowledgements

This project is based on ts-json-schema-generator and uses Ajv for validation.

Readme

Keywords

none

Package Sidebar

Install

npm i @divine/judgement-cli

Weekly Downloads

2

Version

0.2.0

License

Apache-2.0

Unpacked Size

15.3 kB

Total Files

9

Last publish

Collaborators

  • leviticus