TJS-AJV


TJS-AJV is a handy tool that uses TJS to generate json schema from typescript interface, and uses AJV to validate a config file against the json schema.

How to use TJS-AJV?

  • Define your Interface file which will be used to dynamically generate the json schema. For example, the sample-schema.json schema file is generated from the typescript Interface sample-interface.ts by tjs-ajv.
  • Define your config file, from example sample-config.yml. This file will be validated against the above json schema by tjs-ajv.
  • Install tjs-ajv npm install tjs-ajv.

Sample code:

import * as path from "path";
import { loadConfig, validateConfig } from "tjs-ajv";

const INTERFACE_FILE = path.join(__dirname, "./sample-interface.ts");
const CONFIG_FILE = path.join(__dirname, "./sample-config.yml");

console.log("Validating config file, please wait...");
const loadResult = loadConfig(CONFIG_FILE);

if (!loadResult.success) {
  console.error(loadResult.message);
  process.exit(1);
} else {
  const validateResult = validateConfig(
    INTERFACE_FILE,
    "Config",
    loadResult.data
  );
  if (!validateResult.success) {
    console.error(validateResult.message);
    console.error(validateResult.data);
    process.exit(1);
  } else {
    console.log("The config file is valid!");
  }
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s