GithubHelp home page GithubHelp logo

keletes / ts-transformer-json-schema Goto Github PK

View Code? Open in Web Editor NEW

This project forked from ipetrovic11/ts-transformer-json-schema

0.0 0.0 0.0 268 KB

A TypeScript custom transformer to obtain JSON schema from interface.

License: MIT License

JavaScript 32.23% TypeScript 67.77%

ts-transformer-json-schema's Introduction

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 microsoft/TypeScript#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

ts-transformer-json-schema's People

Contributors

kimamula avatar ipetrovic11 avatar danieledraganti avatar geomaster avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.