JSON Schema to Validators and TypeScript Interfaces
This module uses
json-schema-to-typescript to read a directory full of json schema documents and spit out associated validators and TypeScript interfaces.
This allows developers to define their data models that require validation once in json schema format while automatically getting access to performant validators and TypeScript interfaces for development.
One good use case for this module is serverless NodeJS development. The developer has json schema validators that check requests, and data models on the backend, and has TypeScript interfaces for data models that can be used both in Lambda code and front-end code.
npx json-schema-validators-typescript-interfaces -s <path to your json schema source>
yarn add json-schema-validators-typescript-interfaces -D
Command Line Usage
From the root of your project, you can invoke the module directly
json-schema-validators-typescript-interfaces -s <path to your json schema source>
Some json schema properties might use the same regex patterns. Client side checks might also need to use those same regex patterns for client side validation of individual fields. It can be convenient and less error prone to have a library or regex patterns for re-use across your project.
If you store your regex patterns in a separate module, this library can reference them and replace template placeholders in your json schema files.
NOTE: json schema does not support case-insensitive flags so any flags in the provided regex patterns are stripped off.
Here is a sample of a regex pattern module:
const patterns =EMAIL_ADDRESS: /@/URL_ANY: /^?\.*\/?$/URL_HTTPS: /^?\.*\/?$/;moduleexports = patterns;
Here is how these could be templated inside of your json schema:
In order to tell this library to use your regex module, simply provide the path via the
json-schema-validators-typescript-interfaces -s <path to your json schema source> -p <path to your regex patterns module>
Custom Interface and Validator Target Directories
By default the library will place validator and interface artifacts next to where your json schema files are located in directories
You can override either of them by providing command line arguments. For details see the help: