Skip to content
A TypeScript custom transformer to obtain JSON schema from interface.
Branch: master
Clone or download
Pull request Compare This branch is 89 commits ahead of kimamula:master.
Latest commit 51ee164 Jul 16, 2019
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
test Added edge case - top-level intersection Jul 16, 2019
.editorconfig
.gitignore Updated package.json Jun 14, 2019
.travis.yml Setup Apr 22, 2017
LICENSE Add LICENSE Apr 22, 2017
README.md Update README.md Jul 2, 2019
index.ts exporting predefined from index.ts Jun 29, 2019
jest.config.js Updated package.json Jun 14, 2019
package-lock.json Added fastest-validator Jul 16, 2019
package.json Version bump Jul 16, 2019
predefined.d.ts Handling predefined types Jun 19, 2019
transformer.ts Added edge case - top-level intersection Jul 16, 2019
tsconfig.json Remove suppressImplicitAnyIndexErrors Jun 26, 2019
tsconfig.test.json Added tests and ability to disable additional properties Jun 19, 2019

README.md

Build Status Downloads

ts-transformer-json-schema NPM version

A TypeScript custom transformer to obtain json schema for fastest-validator from TypeScript interface

$ npm install ts-transformer-json-schema --save

Requirement

TypeScript >= 2.4.1 TTypeScript

How to use directly with fastest-validator

import { schema } from 'ts-transformer-json-schema';
import Validator from 'fastest';

interface IExample {
  str: string;
}

const v = new Validator();
v.validate({ str: 'string' }, schema<IExample>());

How to use with Moleculer

import { schema } from 'ts-transformer-json-schema';

interface IUser {
	name: string;
}

const GreeterService: ServiceSchema = {
  actions: {
    welcome: {
      params: schema<IUser>(),
      handler({ params: user }: Context<IUser>) {
        return `Welcome, ${user.name}`;
      }
    }
  }
}

There is moleculer template that comes with this transformer and configure compiler to use it: https://github.com/ipetrovic11/moleculer-template-typescript

How to use the custom transformer

Unfortunately, TypeScript itself does not currently provide any easy way to use custom transformers (See https://github.com/Microsoft/TypeScript/issues/14419).

For ttypescript

See ttypescript's README for how to use this with module bundlers such as webpack or Rollup.

// tsconfig.json
{
  "compilerOptions": {
    // ...
    "plugins": [
      { "transform": "ts-transformer-json-schema/transformer" }
    ]
  },
  // ...
}

What can be transformed

Currently transformer can handle:

  • Interfaces

  • Neasted interfaces

  • Extended interfaces

  • Intersections and Unions

  • Enums

  • Emails - Predefined - IEmail

  • Dates - Predefined - IDate

  • UUID - Predefined - IUUID

  • Forbidden - Predefined - IForbidden

  • Additional properties

Take a look at tests for all possibilities. All cases from fastest-validator should be covered, if not please report the issue.

License

MIT

You can’t perform that action at this time.