The Divine Judgement
Divine Judgement is a simple, yet powerful tool for enforcing TypeScript types at runtime. It:
- Reads and parses interfaces, enums and other types directly from your TypeScript source files.
- 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. - 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);
const config: unknown = configFile;
if (is('MyConfigFile', config)) {
}
Acknowledgements
This project is based on ts-json-schema-generator and uses
Ajv for validation.