TypedGate - a JSON Type Checker & Validator without schemas
TypedGate is a JSON checker & validator tool without using any schemas but only requires typescript interface definitions and simple comments.
TypedGate compares typescript interface/class definition to JSON then output validation results.
Installation
Try now without installation (npx)
You can use npx to use TypedGate without installation:
npx typedgate --tsconfig ./tsconfig.json --src ./src/index.ts --json ./some-json-file.json
Global installation
npm install -g typedgate
typedgate --tsconfig ./tsconfig.json --src ./src/index.ts --json ./some-json-file.json
Checking your TypeScript codes of Interfaces / Classes with TypedGate
Add ControlComment to your Interface/Class definitions
In this example, control-comment @TG:path .engine
is added to Engine
interface.
TypedGate recognize that JSON's engine
property should follow Engine
interface.
;;; // This is a control comment// @TG:path .engine
Note that control-comments should be placed above the interface/class declaration.
TypedGate compares Engine
interface to following json:
Since this JSON has valid type of Engine
interface, TypedGate will print following output:
Loading project...
Validating...
=== Validation Result ===
Valid
TypedGate also recognize multiple control-comment like this:
// @TG:path .login.providers.twitter// @TG:path .login.providers.facebook// @TG:path .login.providers.line
JSON shown below satisfies above interface definition.
Run TypedGate
To run TypedGate, you should specify path to project's tsconfig.json, path to source file, path to json to validate.
-t
or--tsconfig
: path totsconfig.json
-s
or--src
: path to source file to validate-j
or--json
: path to json file to validate- Since TypedGate is not a JSON validator, JSON with wrong syntax will cause application error.
-v
or--verbose
: TypedGate will print verbose outputs
typedgate --tsconfig ./test/fixtures/car-types/tsconfig.json --src ./test/fixtures/car-types/engine.ts --json ./test/fixtures/car-types/engine.json
(You can use npx
without installation as described in 'Installation' section)
TypedGate will exit with code 0 when validation is success. Exit code 1 will be returned when validation failure.
Hint
Validating multiple files
--src
option can be take one single file. To validate multiple files, we recommend that you put a file like this:
;;;;// other exports ...
We assume this file is named src/index.ts
. To validate these exported files, you can run this command:
typedgate --tsconfig path/to/tsconfig.json --src ./src/index.ts --json /path/to/something.json
Array path
Assume a JSON of array like this:
An array is represented as []
in TypedGate. So below interface and ControlComment can validate above JSON.
// @TG:path .[]
An array in object like this:
is represented as:
// @TG:path .array[]
API
You can use TypedGate in your Node program.
npm install typedgate
tg.validateJsonFile'./test/fixtures/crayon-types/crayon-type.json'.then // You can also pass object directly rather than pass file path to JSON.tg.validateObjectobject.then/*...*/
Below is an example of failed validation.
TypedGate.validationResult
property contains source file name of typescript, position where error occurs, etc.
Methods & Properties
constructor(private tsConfigFilePath: string, private sourceFilePath: string)
tsConfigFilePath
: path totsconfig.json
sourceFilePath
: path to typescript source file (e.g. project's rootindex.ts
)
validateJsonFile(fileName: string)
fileName
: full path to JSON file to validate
validateObject(targetData: any): boolean
targetData
: object to validate (rather than pass file path to JSON by usingvalidateJsonFile()
.)
get validationResult: TypedGateResult[] | undefined
objectPathIdentifier
: indicates location of failed objectobjectValue
:objectPathIdentifier
's valuesourceFileName
: path to TypeScript source code where validation failedtext
: TypeScript source raw text that validation failedsourceFilePos
: position of failed TypeScript source codevalid
: whether validation success or failed
Development
Run TypedGate
In test/
directory, you can find various useful test cases to develop the TypedGate code.
npm run build && node dist/cli.js -v --tsconfig ./test/fixtures/car-types/tsconfig.json --src ./test/fixtures/car-types/engine.ts --json ./test/fixtures/car-types/engine.json
-v
(--verbose
) switch will help your development.
Run tests
npm run test