@divine/judgement-cli
v0.2.0
Published
The Divine Judgement: Schema/Code generator CLI
Downloads
2
Readme
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
andis
. 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);
// 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.